diff --git a/lib/src/Room.dart b/lib/src/Room.dart index e68d46a0..14950817 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -282,19 +282,15 @@ class Room { Future sendTextEvent(String message, {String txid = null}) => sendEvent({"msgtype": "m.text", "body": message}, txid: txid); - Future sendFileEvent(MatrixFile file, String msgType, - {String txid = null}) async { + /// Sends a [file] to this room after uploading it. The [msgType] is optional + /// and will be detected by the mimetype of the file. + Future 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; - // 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); if (uploadResp is ErrorResponse) return null; @@ -305,14 +301,28 @@ class Room { "filename": fileName, "url": uploadResp, "info": { - "mimetype": mimeType, + "mimetype": mime(file.path), + "size": file.size, + } + }; + return await sendEvent(content, txid: txid); + } + + Future 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 content = { + "msgtype": "m.audio", + "body": fileName, + "filename": fileName, + "url": uploadResp, + "info": { + "mimetype": mime(fileName), + "size": file.size, } }; - if (size != null) - content["info"] = { - "size": size, - "mimetype": mimeType, - }; return await sendEvent(content, txid: txid); } @@ -335,6 +345,54 @@ class Room { return await sendEvent(content, txid: txid); } + Future 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 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 sendEvent(Map content, {String txid = null}) async { final String type = "m.room.message"; diff --git a/test/Room_test.dart b/test/Room_test.dart index 0eaf4f85..d37dc390 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -331,8 +331,8 @@ void main() { test('sendFileEvent', () async { final MatrixFile testFile = MatrixFile(bytes: [], path: "fake/path/file.jpeg"); - final dynamic resp = - await room.sendFileEvent(testFile, "m.file", txid: "testtxid"); + final dynamic resp = await room.sendFileEvent(testFile, + msgType: "m.file", txid: "testtxid"); expect(resp, "42"); });