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/cached_stream_controller.dart';
|
||||||
import 'package:matrix/src/utils/client_init_exception.dart';
|
import 'package:matrix/src/utils/client_init_exception.dart';
|
||||||
import 'package:matrix/src/utils/multilock.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_benchmarked.dart';
|
||||||
import 'package:matrix/src/utils/run_in_root.dart';
|
import 'package:matrix/src/utils/run_in_root.dart';
|
||||||
import 'package:matrix/src/utils/sync_update_item_count.dart';
|
import 'package:matrix/src/utils/sync_update_item_count.dart';
|
||||||
|
|
@ -1524,10 +1523,6 @@ class Client extends MatrixApi {
|
||||||
Uint8List file, {
|
Uint8List file, {
|
||||||
String? filename,
|
String? filename,
|
||||||
String? contentType,
|
String? contentType,
|
||||||
|
|
||||||
/// Callback which gets triggered on progress containing the amount of
|
|
||||||
/// uploaded bytes.
|
|
||||||
void Function(int)? onProgress,
|
|
||||||
}) async {
|
}) async {
|
||||||
final mediaConfig = await getConfig();
|
final mediaConfig = await getConfig();
|
||||||
final maxMediaSize = mediaConfig.mUploadSize;
|
final maxMediaSize = mediaConfig.mUploadSize;
|
||||||
|
|
@ -1536,31 +1531,8 @@ class Client extends MatrixApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType ??= lookupMimeType(filename ?? '', headerBytes: file);
|
contentType ??= lookupMimeType(filename ?? '', headerBytes: file);
|
||||||
|
final mxc = await super
|
||||||
final requestUri = Uri(
|
.uploadContent(file, filename: filename, contentType: contentType);
|
||||||
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 database = this.database;
|
final database = this.database;
|
||||||
if (file.length <= database.maxFileSize) {
|
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
|
/// Uploads a new user avatar for this user. Leave file null to remove the
|
||||||
/// current avatar.
|
/// current avatar.
|
||||||
Future<void> setAvatar(
|
Future<void> setAvatar(MatrixFile? file) async {
|
||||||
MatrixFile? file, {
|
|
||||||
void Function(int)? onUploadProgress,
|
|
||||||
}) async {
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
// We send an empty String to remove the avatar. Sending Null **should**
|
// We send an empty String to remove the avatar. Sending Null **should**
|
||||||
// work but it doesn't with Synapse. See:
|
// work but it doesn't with Synapse. See:
|
||||||
|
|
@ -1648,7 +1617,6 @@ class Client extends MatrixApi {
|
||||||
file.bytes,
|
file.bytes,
|
||||||
filename: file.name,
|
filename: file.name,
|
||||||
contentType: file.mimeType,
|
contentType: file.mimeType,
|
||||||
onProgress: onUploadProgress,
|
|
||||||
);
|
);
|
||||||
await setAvatarUrl(userID!, uploadResp);
|
await setAvatarUrl(userID!, uploadResp);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -960,14 +960,12 @@ class Room {
|
||||||
uploadFile.bytes,
|
uploadFile.bytes,
|
||||||
filename: uploadFile.name,
|
filename: uploadFile.name,
|
||||||
contentType: uploadFile.mimeType,
|
contentType: uploadFile.mimeType,
|
||||||
onProgress: onUploadProgress,
|
|
||||||
);
|
);
|
||||||
thumbnailUploadResp = uploadThumbnail != null
|
thumbnailUploadResp = uploadThumbnail != null
|
||||||
? await client.uploadContent(
|
? await client.uploadContent(
|
||||||
uploadThumbnail.bytes,
|
uploadThumbnail.bytes,
|
||||||
filename: uploadThumbnail.name,
|
filename: uploadThumbnail.name,
|
||||||
contentType: uploadThumbnail.mimeType,
|
contentType: uploadThumbnail.mimeType,
|
||||||
onProgress: onThumbnailUploadProgress,
|
|
||||||
)
|
)
|
||||||
: null;
|
: null;
|
||||||
} on MatrixException catch (_) {
|
} on MatrixException catch (_) {
|
||||||
|
|
@ -2120,7 +2118,6 @@ class Room {
|
||||||
: await client.uploadContent(
|
: await client.uploadContent(
|
||||||
file.bytes,
|
file.bytes,
|
||||||
filename: file.name,
|
filename: file.name,
|
||||||
onProgress: onUploadProgress,
|
|
||||||
);
|
);
|
||||||
return await client.setRoomStateWithKey(
|
return await client.setRoomStateWithKey(
|
||||||
id,
|
id,
|
||||||
|
|
|
||||||
|
|
@ -4,37 +4,6 @@ import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
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 {
|
extension ToBytesWithProgress on http.ByteStream {
|
||||||
/// Collects the data of this stream in a [Uint8List].
|
/// Collects the data of this stream in a [Uint8List].
|
||||||
Future<Uint8List> toBytesWithProgress(void Function(int)? onProgress) {
|
Future<Uint8List> toBytesWithProgress(void Function(int)? onProgress) {
|
||||||
|
|
|
||||||
|
|
@ -1488,18 +1488,15 @@ void main() {
|
||||||
});
|
});
|
||||||
test('upload', () async {
|
test('upload', () async {
|
||||||
final client = await getClient();
|
final client = await getClient();
|
||||||
final onProgressMap = <int>[];
|
|
||||||
final response = await client.uploadContent(
|
final response = await client.uploadContent(
|
||||||
Uint8List(0),
|
Uint8List(0),
|
||||||
filename: 'file.jpeg',
|
filename: 'file.jpeg',
|
||||||
onProgress: onProgressMap.add,
|
|
||||||
);
|
);
|
||||||
expect(response.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw');
|
expect(response.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw');
|
||||||
expect(
|
expect(
|
||||||
await client.database.getFile(response) != null,
|
await client.database.getFile(response) != null,
|
||||||
client.database.supportsFileStoring,
|
client.database.supportsFileStoring,
|
||||||
);
|
);
|
||||||
expect(onProgressMap, [74, 183, 183, 185, 261]);
|
|
||||||
await client.dispose(closeDatabase: true);
|
await client.dispose(closeDatabase: true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue