Skip to content

Commit cbb52f7

Browse files
chrisbobbegnprice
authored andcommitted
api: Add mimeType param to uploadFile binding
1 parent 57b2bbd commit cbb52f7

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

lib/api/route/messages.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,10 @@ Future<UploadFileResult> uploadFile(
262262
required Stream<List<int>> content,
263263
required int length,
264264
required String filename,
265+
required String? contentType,
265266
}) {
266267
return connection.postFileFromStream('uploadFile', UploadFileResult.fromJson, 'user_uploads',
267-
content, length, filename: filename);
268+
content, length, filename: filename, contentType: contentType);
268269
}
269270

270271
@JsonSerializable(fieldRename: FieldRename.snake)

lib/widgets/compose_box.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,11 @@ Future<void> _uploadFiles({
476476
Uri? url;
477477
try {
478478
final result = await uploadFile(store.connection,
479-
content: content, length: length, filename: filename);
479+
content: content,
480+
length: length,
481+
filename: filename,
482+
contentType: null, // TODO(#829)
483+
);
480484
url = Uri.parse(result.uri);
481485
} catch (e) {
482486
if (!context.mounted) return;

test/api/route/messages_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,55 @@ void main() {
414414
});
415415
});
416416

417+
group('uploadFile', () {
418+
Future<void> checkUploadFile(FakeApiConnection connection, {
419+
required List<List<int>> content,
420+
required int length,
421+
required String filename,
422+
required String? contentType,
423+
}) async {
424+
connection.prepare(json:
425+
UploadFileResult(uri: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/$filename').toJson());
426+
await uploadFile(connection,
427+
content: Stream.fromIterable(content),
428+
length: length,
429+
filename: filename,
430+
contentType: contentType);
431+
check(connection.lastRequest).isA<http.MultipartRequest>()
432+
..method.equals('POST')
433+
..url.path.equals('/api/v1/user_uploads')
434+
..files.single.which((it) => it
435+
..field.equals('file')
436+
..length.equals(length)
437+
..filename.equals(filename)
438+
..contentType.asString.equals(contentType ?? 'application/octet-stream')
439+
..has<Future<List<int>>>((f) => f.finalize().toBytes(), 'contents')
440+
.completes((it) => it.deepEquals(content.expand((l) => l))));
441+
}
442+
443+
test('with mime type', () {
444+
return FakeApiConnection.with_((connection) async {
445+
await checkUploadFile(connection,
446+
content: ['asdf'.codeUnits],
447+
length: 4,
448+
filename: 'image.jpg',
449+
contentType: 'image/jpeg',
450+
);
451+
});
452+
});
453+
454+
test('without mime type', () {
455+
return FakeApiConnection.with_((connection) async {
456+
await checkUploadFile(connection,
457+
content: ['asdf'.codeUnits],
458+
length: 4,
459+
filename: 'some_file',
460+
contentType: null,
461+
);
462+
});
463+
});
464+
});
465+
417466
group('addReaction', () {
418467
Future<void> checkAddReaction(FakeApiConnection connection, {
419468
required int messageId,

0 commit comments

Comments
 (0)