[LLC] Refactor attachment_file_uploader usage in client and channel

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-02-17 20:14:01 +05:30
parent 517c7e489d
commit b29f673e91
5 changed files with 144 additions and 71 deletions
+13 -8
View File
@@ -245,15 +245,20 @@ class Channel {
}
final isImage = it.type == 'image';
final uploader = _client.attachmentUploader;
final cancelToken = CancelToken();
Future<String> future;
if (isImage) {
future = uploader.uploadImage(it.file, id, type,
onSendProgress: onSendProgress, cancelToken: cancelToken);
future = sendImage(
it.file,
onSendProgress: onSendProgress,
cancelToken: cancelToken,
).then((it) => it.file);
} else {
future = uploader.uploadFile(it.file, id, type,
onSendProgress: onSendProgress, cancelToken: cancelToken);
future = sendFile(
it.file,
onSendProgress: onSendProgress,
cancelToken: cancelToken,
).then((it) => it.file);
}
_cancelableAttachmentUploadRequest[it.id] = cancelToken;
return future.then((url) {
@@ -446,7 +451,7 @@ class Channel {
/// Send a file to this channel
Future<SendFileResponse> sendFile(
MultipartFile file, {
AttachmentFile file, {
ProgressCallback onSendProgress,
CancelToken cancelToken,
}) {
@@ -461,12 +466,12 @@ class Channel {
/// Send an image to this channel
Future<SendImageResponse> sendImage(
MultipartFile image, {
AttachmentFile file, {
ProgressCallback onSendProgress,
CancelToken cancelToken,
}) {
return _client.sendImage(
image,
file,
id,
type,
onSendProgress: onSendProgress,
@@ -1,17 +1,18 @@
import 'package:dio/dio.dart';
import 'package:stream_chat/src/api/responses.dart';
import 'package:stream_chat/src/models/attachment_file.dart';
import '../client.dart';
import '../extensions/string_extension.dart';
import 'client.dart';
import 'extensions/string_extension.dart';
/// Class responsible for uploading images and files from a given channel
abstract class AttachmentUploader {
/// Uploads a image [file] to the given channel.
/// Returns image [file] URL once sent successfully.
/// Class responsible for uploading images and files to a given channel
abstract class AttachmentFileUploader {
/// Uploads a [image] to the given channel.
/// Returns [SendImageResponse] once sent successfully.
///
/// Optionally, access upload progress using [onSendProgress]
/// and cancel the request using [cancelToken]
Future<String> uploadImage(
AttachmentFile file,
Future<SendImageResponse> sendImage(
AttachmentFile image,
String channelId,
String channelType, {
ProgressCallback onSendProgress,
@@ -19,11 +20,11 @@ abstract class AttachmentUploader {
});
/// Uploads a [file] to the given channel.
/// Returns [file] URL once sent successfully.
/// Returns [SendFileResponse] once sent successfully.
///
/// Optionally, access upload progress using [onSendProgress]
/// and cancel the request using [cancelToken]
Future<String> uploadFile(
Future<SendFileResponse> sendFile(
AttachmentFile file,
String channelId,
String channelType, {
@@ -32,15 +33,15 @@ abstract class AttachmentUploader {
});
}
/// Stream's default implementation of [AttachmentUploader]
class StreamAttachmentUploader implements AttachmentUploader {
/// Stream's default implementation of [AttachmentFileUploader]
class StreamAttachmentUploader implements AttachmentFileUploader {
final StreamChatClient _client;
/// Creates a new [StreamAttachmentUploader] instance.
const StreamAttachmentUploader(this._client);
@override
Future<String> uploadImage(
Future<SendImageResponse> sendImage(
AttachmentFile file,
String channelId,
String channelType, {
@@ -49,22 +50,23 @@ class StreamAttachmentUploader implements AttachmentUploader {
}) async {
final filename = file.path?.split('/')?.last;
final mimeType = filename.mimeType;
final res = await _client.sendImage(
await MultipartFile.fromFile(
file.path,
filename: filename,
contentType: mimeType,
),
channelId,
channelType,
final response = await _client.post(
'/channels/$channelType/$channelId/image',
data: FormData.fromMap({
'file': await MultipartFile.fromFile(
file.path,
filename: filename,
contentType: mimeType,
),
}),
onSendProgress: onSendProgress,
cancelToken: cancelToken,
);
return res.file;
return _client.decode(response.data, SendImageResponse.fromJson);
}
@override
Future<String> uploadFile(
Future<SendFileResponse> sendFile(
AttachmentFile file,
String channelId,
String channelType, {
@@ -73,17 +75,18 @@ class StreamAttachmentUploader implements AttachmentUploader {
}) async {
final filename = file.path?.split('/')?.last;
final mimeType = filename.mimeType;
final res = await _client.sendFile(
await MultipartFile.fromFile(
file.path,
filename: filename,
contentType: mimeType,
),
channelId,
channelType,
final response = await _client.post(
'/channels/$channelType/$channelId/file',
data: FormData.fromMap({
'file': await MultipartFile.fromFile(
file.path,
filename: filename,
contentType: mimeType,
),
}),
onSendProgress: onSendProgress,
cancelToken: cancelToken,
);
return res.file;
return _client.decode(response.data, SendFileResponse.fromJson);
}
}
+15 -14
View File
@@ -8,11 +8,12 @@ import 'package:pedantic/pedantic.dart' show unawaited;
import 'package:rxdart/rxdart.dart';
import 'package:stream_chat/src/api/retry_policy.dart';
import 'package:stream_chat/src/event_type.dart';
import 'package:stream_chat/src/models/attachment_file.dart';
import 'package:stream_chat/src/models/own_user.dart';
import 'package:stream_chat/version.dart';
import 'package:uuid/uuid.dart';
import 'api/attachment_uploader.dart';
import 'attachment_file_uploader.dart';
import 'api/channel.dart';
import 'api/connection_status.dart';
import 'api/requests.dart';
@@ -102,7 +103,7 @@ class StreamChatClient {
ChatPersistenceClient chatPersistenceClient;
/// Attachment uploader
AttachmentUploader attachmentUploader;
AttachmentFileUploader attachmentUploader;
/// Whether the chat persistence is available or not
bool get persistenceEnabled => chatPersistenceClient != null;
@@ -1043,36 +1044,36 @@ class StreamChatClient {
/// Send a [file] to the [channelId] of type [channelType]
Future<SendFileResponse> sendFile(
MultipartFile file,
AttachmentFile file,
String channelId,
String channelType, {
ProgressCallback onSendProgress,
CancelToken cancelToken,
}) async {
final response = await post(
'/channels/$channelType/$channelId/file',
data: FormData.fromMap({'file': file}),
}) {
return attachmentUploader.sendFile(
file,
channelId,
channelType,
onSendProgress: onSendProgress,
cancelToken: cancelToken,
);
return decode(response.data, SendFileResponse.fromJson);
}
/// Send a [image] to the [channelId] of type [channelType]
Future<SendImageResponse> sendImage(
MultipartFile image,
AttachmentFile image,
String channelId,
String channelType, {
ProgressCallback onSendProgress,
CancelToken cancelToken,
}) async {
final response = await post(
'/channels/$channelType/$channelId/image',
data: FormData.fromMap({'file': image}),
}) {
return attachmentUploader.sendImage(
image,
channelId,
channelType,
onSendProgress: onSendProgress,
cancelToken: cancelToken,
);
return decode(response.data, SendImageResponse.fromJson);
}
/// Add a device for Push Notifications.