diff --git a/lib/src/client.dart b/lib/src/client.dart index c79a3a50..cea838e5 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -37,7 +37,6 @@ import 'package:matrix/src/models/timeline_chunk.dart'; import 'package:matrix/src/utils/cached_stream_controller.dart'; import 'package:matrix/src/utils/client_init_exception.dart'; import 'package:matrix/src/utils/multilock.dart'; -import 'package:matrix/src/utils/multipart_request_progress.dart'; import 'package:matrix/src/utils/run_benchmarked.dart'; import 'package:matrix/src/utils/run_in_root.dart'; import 'package:matrix/src/utils/sync_update_item_count.dart'; @@ -1524,10 +1523,6 @@ class Client extends MatrixApi { Uint8List file, { String? filename, String? contentType, - - /// Callback which gets triggered on progress containing the amount of - /// uploaded bytes. - void Function(int)? onProgress, }) async { final mediaConfig = await getConfig(); final maxMediaSize = mediaConfig.mUploadSize; @@ -1536,31 +1531,8 @@ class Client extends MatrixApi { } contentType ??= lookupMimeType(filename ?? '', headerBytes: file); - - final requestUri = Uri( - path: '_matrix/media/v3/upload', - queryParameters: { - if (filename != null) 'filename': filename, - }, - ); - final request = MultipartRequest( - 'POST', - baseUri!.resolveUri(requestUri), - onProgress: onProgress, - ); - request.headers['authorization'] = 'Bearer ${bearerToken!}'; - if (contentType != null) request.headers['content-type'] = contentType; - request.files.add( - http.MultipartFile.fromBytes('file', file, filename: filename), - ); - final response = await httpClient.send(request); - final responseBody = await response.stream.toBytes(); - if (response.statusCode != 200) unexpectedResponse(response, responseBody); - final responseString = utf8.decode(responseBody); - final json = jsonDecode(responseString); - final mxc = ((json['content_uri'] as String).startsWith('mxc://') - ? Uri.parse(json['content_uri'] as String) - : throw Exception('Uri not an mxc URI')); + final mxc = await super + .uploadContent(file, filename: filename, contentType: contentType); final database = this.database; if (file.length <= database.maxFileSize) { @@ -1634,10 +1606,7 @@ class Client extends MatrixApi { /// Uploads a new user avatar for this user. Leave file null to remove the /// current avatar. - Future setAvatar( - MatrixFile? file, { - void Function(int)? onUploadProgress, - }) async { + Future setAvatar(MatrixFile? file) async { if (file == null) { // We send an empty String to remove the avatar. Sending Null **should** // work but it doesn't with Synapse. See: @@ -1648,7 +1617,6 @@ class Client extends MatrixApi { file.bytes, filename: file.name, contentType: file.mimeType, - onProgress: onUploadProgress, ); await setAvatarUrl(userID!, uploadResp); return; diff --git a/lib/src/room.dart b/lib/src/room.dart index 0238db48..0c61e8f0 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -960,14 +960,12 @@ class Room { uploadFile.bytes, filename: uploadFile.name, contentType: uploadFile.mimeType, - onProgress: onUploadProgress, ); thumbnailUploadResp = uploadThumbnail != null ? await client.uploadContent( uploadThumbnail.bytes, filename: uploadThumbnail.name, contentType: uploadThumbnail.mimeType, - onProgress: onThumbnailUploadProgress, ) : null; } on MatrixException catch (_) { @@ -2120,7 +2118,6 @@ class Room { : await client.uploadContent( file.bytes, filename: file.name, - onProgress: onUploadProgress, ); return await client.setRoomStateWithKey( id, diff --git a/lib/src/utils/multipart_request_progress.dart b/lib/src/utils/multipart_request_progress.dart index 7fb9335d..0b05a230 100644 --- a/lib/src/utils/multipart_request_progress.dart +++ b/lib/src/utils/multipart_request_progress.dart @@ -4,37 +4,6 @@ import 'dart:typed_data'; import 'package:http/http.dart' as http; -class MultipartRequest extends http.MultipartRequest { - MultipartRequest( - super.method, - super.url, { - this.onProgress, - }); - - final void Function(int bytes)? onProgress; - - @override - http.ByteStream finalize() { - final byteStream = super.finalize(); - if (onProgress == null) return byteStream; - - final total = contentLength; - int bytes = 0; - - final t = StreamTransformer.fromHandlers( - handleData: (List data, EventSink> sink) { - bytes += data.length; - onProgress?.call(bytes); - if (total >= bytes) { - sink.add(data); - } - }, - ); - final stream = byteStream.transform(t); - return http.ByteStream(stream); - } -} - extension ToBytesWithProgress on http.ByteStream { /// Collects the data of this stream in a [Uint8List]. Future toBytesWithProgress(void Function(int)? onProgress) { diff --git a/test/client_test.dart b/test/client_test.dart index 7bc747ff..2db2b1d9 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -1488,18 +1488,15 @@ void main() { }); test('upload', () async { final client = await getClient(); - final onProgressMap = []; final response = await client.uploadContent( Uint8List(0), filename: 'file.jpeg', - onProgress: onProgressMap.add, ); expect(response.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'); expect( await client.database.getFile(response) != null, client.database.supportsFileStoring, ); - expect(onProgressMap, [74, 183, 183, 185, 261]); await client.dispose(closeDatabase: true); });