[LLC] Refactor attachment_file_uploader usage in client and channel
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -1,89 +0,0 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:stream_chat/src/models/attachment_file.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.
|
||||
///
|
||||
/// Optionally, access upload progress using [onSendProgress]
|
||||
/// and cancel the request using [cancelToken]
|
||||
Future<String> uploadImage(
|
||||
AttachmentFile file,
|
||||
String channelId,
|
||||
String channelType, {
|
||||
ProgressCallback onSendProgress,
|
||||
CancelToken cancelToken,
|
||||
});
|
||||
|
||||
/// Uploads a [file] to the given channel.
|
||||
/// Returns [file] URL once sent successfully.
|
||||
///
|
||||
/// Optionally, access upload progress using [onSendProgress]
|
||||
/// and cancel the request using [cancelToken]
|
||||
Future<String> uploadFile(
|
||||
AttachmentFile file,
|
||||
String channelId,
|
||||
String channelType, {
|
||||
ProgressCallback onSendProgress,
|
||||
CancelToken cancelToken,
|
||||
});
|
||||
}
|
||||
|
||||
/// Stream's default implementation of [AttachmentUploader]
|
||||
class StreamAttachmentUploader implements AttachmentUploader {
|
||||
final StreamChatClient _client;
|
||||
|
||||
/// Creates a new [StreamAttachmentUploader] instance.
|
||||
const StreamAttachmentUploader(this._client);
|
||||
|
||||
@override
|
||||
Future<String> uploadImage(
|
||||
AttachmentFile file,
|
||||
String channelId,
|
||||
String channelType, {
|
||||
ProgressCallback onSendProgress,
|
||||
CancelToken cancelToken,
|
||||
}) 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,
|
||||
onSendProgress: onSendProgress,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
return res.file;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadFile(
|
||||
AttachmentFile file,
|
||||
String channelId,
|
||||
String channelType, {
|
||||
ProgressCallback onSendProgress,
|
||||
CancelToken cancelToken,
|
||||
}) 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,
|
||||
onSendProgress: onSendProgress,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
return res.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,
|
||||
|
||||
Reference in New Issue
Block a user