chore: Revert on upload progress
This is not working as expected. As far as I can see there is no way to do this with the http package for now. Only way would be to switch to dio. Not sure if we want to do this.
This commit is contained in:
parent
6e599c2a53
commit
9549270423
|
|
@ -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<void> setAvatar(
|
||||
MatrixFile? file, {
|
||||
void Function(int)? onUploadProgress,
|
||||
}) async {
|
||||
Future<void> 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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<int> data, EventSink<List<int>> 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<Uint8List> toBytesWithProgress(void Function(int)? onProgress) {
|
||||
|
|
|
|||
|
|
@ -1488,18 +1488,15 @@ void main() {
|
|||
});
|
||||
test('upload', () async {
|
||||
final client = await getClient();
|
||||
final onProgressMap = <int>[];
|
||||
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);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue