Merge branch 'room-fix-detect-mimetype' into 'master'
[Room] Detect mimetype automatically See merge request famedly/famedlysdk!138
This commit is contained in:
commit
735c9e6b21
|
@ -282,19 +282,15 @@ class Room {
|
||||||
Future<String> sendTextEvent(String message, {String txid = null}) =>
|
Future<String> sendTextEvent(String message, {String txid = null}) =>
|
||||||
sendEvent({"msgtype": "m.text", "body": message}, txid: txid);
|
sendEvent({"msgtype": "m.text", "body": message}, txid: txid);
|
||||||
|
|
||||||
Future<String> sendFileEvent(MatrixFile file, String msgType,
|
/// Sends a [file] to this room after uploading it. The [msgType] is optional
|
||||||
{String txid = null}) async {
|
/// and will be detected by the mimetype of the file.
|
||||||
|
Future<String> sendFileEvent(MatrixFile file,
|
||||||
|
{String msgType = "m.file", String txid = null}) async {
|
||||||
|
if (msgType == "m.image") return sendImageEvent(file);
|
||||||
|
if (msgType == "m.audio") return sendVideoEvent(file);
|
||||||
|
if (msgType == "m.video") return sendAudioEvent(file);
|
||||||
String fileName = file.path.split("/").last;
|
String fileName = file.path.split("/").last;
|
||||||
// Try to get the size of the file
|
|
||||||
int size;
|
|
||||||
try {
|
|
||||||
size = file.size;
|
|
||||||
} catch (e) {
|
|
||||||
print("[UPLOAD] Could not get size. Reason: ${e.toString()}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload file
|
|
||||||
String mimeType = mime(file.path);
|
|
||||||
final dynamic uploadResp = await client.connection.upload(file);
|
final dynamic uploadResp = await client.connection.upload(file);
|
||||||
if (uploadResp is ErrorResponse) return null;
|
if (uploadResp is ErrorResponse) return null;
|
||||||
|
|
||||||
|
@ -305,13 +301,27 @@ class Room {
|
||||||
"filename": fileName,
|
"filename": fileName,
|
||||||
"url": uploadResp,
|
"url": uploadResp,
|
||||||
"info": {
|
"info": {
|
||||||
"mimetype": mimeType,
|
"mimetype": mime(file.path),
|
||||||
|
"size": file.size,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (size != null)
|
return await sendEvent(content, txid: txid);
|
||||||
content["info"] = {
|
}
|
||||||
"size": size,
|
|
||||||
"mimetype": mimeType,
|
Future<String> sendAudioEvent(MatrixFile file,
|
||||||
|
{String txid = null, int width, int height}) async {
|
||||||
|
String fileName = file.path.split("/").last;
|
||||||
|
final dynamic uploadResp = await client.connection.upload(file);
|
||||||
|
if (uploadResp is ErrorResponse) return null;
|
||||||
|
Map<String, dynamic> content = {
|
||||||
|
"msgtype": "m.audio",
|
||||||
|
"body": fileName,
|
||||||
|
"filename": fileName,
|
||||||
|
"url": uploadResp,
|
||||||
|
"info": {
|
||||||
|
"mimetype": mime(fileName),
|
||||||
|
"size": file.size,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return await sendEvent(content, txid: txid);
|
return await sendEvent(content, txid: txid);
|
||||||
}
|
}
|
||||||
|
@ -335,6 +345,54 @@ class Room {
|
||||||
return await sendEvent(content, txid: txid);
|
return await sendEvent(content, txid: txid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String> sendVideoEvent(MatrixFile file,
|
||||||
|
{String txid = null,
|
||||||
|
int videoWidth,
|
||||||
|
int videoHeight,
|
||||||
|
int duration,
|
||||||
|
MatrixFile thumbnail,
|
||||||
|
int thumbnailWidth,
|
||||||
|
int thumbnailHeight}) async {
|
||||||
|
String fileName = file.path.split("/").last;
|
||||||
|
final dynamic uploadResp = await client.connection.upload(file);
|
||||||
|
if (uploadResp is ErrorResponse) return null;
|
||||||
|
Map<String, dynamic> content = {
|
||||||
|
"msgtype": "m.video",
|
||||||
|
"body": fileName,
|
||||||
|
"url": uploadResp,
|
||||||
|
"info": {
|
||||||
|
"size": file.size,
|
||||||
|
"mimetype": mime(fileName),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (videoWidth != null) {
|
||||||
|
content["info"]["w"] = videoWidth;
|
||||||
|
}
|
||||||
|
if (thumbnailHeight != null) {
|
||||||
|
content["info"]["h"] = thumbnailHeight;
|
||||||
|
}
|
||||||
|
if (duration != null) {
|
||||||
|
content["info"]["duration"] = duration;
|
||||||
|
}
|
||||||
|
if (thumbnail != null) {
|
||||||
|
String thumbnailName = file.path.split("/").last;
|
||||||
|
final dynamic thumbnailUploadResp = await client.connection.upload(file);
|
||||||
|
if (thumbnailUploadResp is ErrorResponse) return null;
|
||||||
|
content["info"]["thumbnail_url"] = thumbnailUploadResp;
|
||||||
|
content["info"]["thumbnail_info"] = {
|
||||||
|
"size": thumbnail.size,
|
||||||
|
"mimetype": mime(thumbnailName),
|
||||||
|
};
|
||||||
|
if (thumbnailWidth != null) {
|
||||||
|
content["info"]["thumbnail_info"]["w"] = thumbnailWidth;
|
||||||
|
}
|
||||||
|
if (thumbnailHeight != null) {
|
||||||
|
content["info"]["thumbnail_info"]["h"] = thumbnailHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return await sendEvent(content, txid: txid);
|
||||||
|
}
|
||||||
|
|
||||||
Future<String> sendEvent(Map<String, dynamic> content,
|
Future<String> sendEvent(Map<String, dynamic> content,
|
||||||
{String txid = null}) async {
|
{String txid = null}) async {
|
||||||
final String type = "m.room.message";
|
final String type = "m.room.message";
|
||||||
|
|
|
@ -331,8 +331,8 @@ void main() {
|
||||||
test('sendFileEvent', () async {
|
test('sendFileEvent', () async {
|
||||||
final MatrixFile testFile =
|
final MatrixFile testFile =
|
||||||
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
|
||||||
final dynamic resp =
|
final dynamic resp = await room.sendFileEvent(testFile,
|
||||||
await room.sendFileEvent(testFile, "m.file", txid: "testtxid");
|
msgType: "m.file", txid: "testtxid");
|
||||||
expect(resp, "42");
|
expect(resp, "42");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue