From 82affbc14fd22e2e792c6de3c59b9937248dc747 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 30 Mar 2020 11:59:24 +0200 Subject: [PATCH] [Room] Improved sendFileEvent --- lib/src/room.dart | 46 ++++++++++++++++++++++++++++++++++++---------- pubspec.lock | 2 +- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index ee665d4..ae8fac1 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -37,7 +37,6 @@ import 'package:famedlysdk/src/utils/session_key.dart'; import 'package:matrix_file_e2ee/matrix_file_e2ee.dart'; import 'package:mime_type/mime_type.dart'; import 'package:olm/olm.dart' as olm; -import 'package:pedantic/pedantic.dart'; import './user.dart'; import 'timeline.dart'; @@ -468,12 +467,17 @@ class Room { /// Sends a [file] to this room after uploading it. The [msgType] is optional /// and will be detected by the mimetype of the file. Returns the mxc uri of - /// the uploaded file. - Future sendFileEvent(MatrixFile file, - {String msgType = 'm.file', - String txid, - Event inReplyTo, - Map info}) async { + /// the uploaded file. If [waitUntilSent] is true, the future will wait until + /// the message event has received the server. Otherwise the future will only + /// wait until the file has been uploaded. + Future sendFileEvent( + MatrixFile file, { + String msgType, + String txid, + Event inReplyTo, + Map info, + bool waitUntilSent = false, + }) async { var fileName = file.path.split('/').last; final sendEncrypted = encrypted && client.fileEncryptionEnabled; EncryptedFile encryptedFile; @@ -485,6 +489,21 @@ class Room { contentType: sendEncrypted ? 'application/octet-stream' : null, ); + final mimeType = mime(file.path) ?? ''; + if (msgType == null) { + final metaType = (mimeType).split('/')[0]; + switch (metaType) { + case 'image': + case 'audio': + case 'video': + msgType = 'm.$metaType'; + break; + default: + msgType = 'm.file'; + break; + } + } + // Send event var content = { 'msgtype': msgType, @@ -494,7 +513,7 @@ class Room { if (sendEncrypted) 'file': { 'url': uploadResp, - 'mimetype': mime(file.path), + 'mimetype': mimeType, 'v': 'v2', 'key': { 'alg': 'A256CTR', @@ -508,11 +527,18 @@ class Room { }, 'info': info ?? { - 'mimetype': mime(file.path), + 'mimetype': mimeType, 'size': file.size, } }; - unawaited(sendEvent(content, txid: txid, inReplyTo: inReplyTo)); + final sendResponse = sendEvent( + content, + txid: txid, + inReplyTo: inReplyTo, + ); + if (waitUntilSent) { + await sendResponse; + } return uploadResp; } diff --git a/pubspec.lock b/pubspec.lock index 2e5d5f1..bbc0327 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -333,7 +333,7 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.9.0" + version: "1.8.0+1" pointycastle: dependency: transitive description: