Merge branch 'room-enhance-send-file' into 'master'

[Room] Improved sendFileEvent

See merge request famedly/famedlysdk!251
This commit is contained in:
Christian Pauly 2020-03-30 10:06:16 +00:00
commit 26a534f465
2 changed files with 37 additions and 11 deletions

View file

@ -37,7 +37,6 @@ import 'package:famedlysdk/src/utils/session_key.dart';
import 'package:matrix_file_e2ee/matrix_file_e2ee.dart'; import 'package:matrix_file_e2ee/matrix_file_e2ee.dart';
import 'package:mime_type/mime_type.dart'; import 'package:mime_type/mime_type.dart';
import 'package:olm/olm.dart' as olm; import 'package:olm/olm.dart' as olm;
import 'package:pedantic/pedantic.dart';
import './user.dart'; import './user.dart';
import 'timeline.dart'; import 'timeline.dart';
@ -468,12 +467,17 @@ class Room {
/// Sends a [file] to this room after uploading it. The [msgType] is optional /// 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 /// and will be detected by the mimetype of the file. Returns the mxc uri of
/// the uploaded file. /// the uploaded file. If [waitUntilSent] is true, the future will wait until
Future<String> sendFileEvent(MatrixFile file, /// the message event has received the server. Otherwise the future will only
{String msgType = 'm.file', /// wait until the file has been uploaded.
Future<String> sendFileEvent(
MatrixFile file, {
String msgType,
String txid, String txid,
Event inReplyTo, Event inReplyTo,
Map<String, dynamic> info}) async { Map<String, dynamic> info,
bool waitUntilSent = false,
}) async {
var fileName = file.path.split('/').last; var fileName = file.path.split('/').last;
final sendEncrypted = encrypted && client.fileEncryptionEnabled; final sendEncrypted = encrypted && client.fileEncryptionEnabled;
EncryptedFile encryptedFile; EncryptedFile encryptedFile;
@ -485,6 +489,21 @@ class Room {
contentType: sendEncrypted ? 'application/octet-stream' : null, 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 // Send event
var content = <String, dynamic>{ var content = <String, dynamic>{
'msgtype': msgType, 'msgtype': msgType,
@ -494,7 +513,7 @@ class Room {
if (sendEncrypted) if (sendEncrypted)
'file': { 'file': {
'url': uploadResp, 'url': uploadResp,
'mimetype': mime(file.path), 'mimetype': mimeType,
'v': 'v2', 'v': 'v2',
'key': { 'key': {
'alg': 'A256CTR', 'alg': 'A256CTR',
@ -508,11 +527,18 @@ class Room {
}, },
'info': info ?? 'info': info ??
{ {
'mimetype': mime(file.path), 'mimetype': mimeType,
'size': file.size, 'size': file.size,
} }
}; };
unawaited(sendEvent(content, txid: txid, inReplyTo: inReplyTo)); final sendResponse = sendEvent(
content,
txid: txid,
inReplyTo: inReplyTo,
);
if (waitUntilSent) {
await sendResponse;
}
return uploadResp; return uploadResp;
} }

View file

@ -333,7 +333,7 @@ packages:
name: pedantic name: pedantic
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.9.0" version: "1.8.0+1"
pointycastle: pointycastle:
dependency: transitive dependency: transitive
description: description: