diff --git a/lib/src/client.dart b/lib/src/client.dart index e463e5d..71035d6 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -135,7 +135,7 @@ class Client { bool get encryptionEnabled => _olmAccount != null; /// Whether this client is able to encrypt and decrypt files. - bool get fileEncryptionEnabled => false; + bool get fileEncryptionEnabled => true; /// Warning! This endpoint is for testing only! set rooms(List newList) { @@ -877,15 +877,15 @@ class Client { /// Uploads a file with the name [fileName] as base64 encoded to the server /// and returns the mxc url as a string. - Future upload(MatrixFile file) async { + Future upload(MatrixFile file, {String contentType}) async { // For testing if (this.homeserver.toLowerCase() == "https://fakeserver.notexisting") { return "mxc://example.com/AQwafuaFswefuhsfAFAgsw"; } Map headers = {}; headers["Authorization"] = "Bearer $accessToken"; - headers["Content-Type"] = mime(file.path); - String fileName = file.path.split("/").last.toLowerCase(); + headers["Content-Type"] = contentType ?? mime(file.path); + String fileName = Uri.encodeFull(file.path.split("/").last.toLowerCase()); final url = "$homeserver/_matrix/media/r0/upload?filename=$fileName"; final streamedRequest = http.StreamedRequest('POST', Uri.parse(url)) ..headers.addAll(headers); @@ -897,6 +897,10 @@ class Client { Map jsonResponse = json.decode( String.fromCharCodes(await streamedResponse.stream.first), ); + if (!(jsonResponse["content_uri"] is String && + jsonResponse["content_uri"].isNotEmpty)) { + throw ("Missing json key: 'content_uri' ${jsonResponse.toString()}"); + } return jsonResponse["content_uri"]; } diff --git a/lib/src/room.dart b/lib/src/room.dart index 651c5d0..9c309c4 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -481,7 +481,10 @@ class Room { if (sendEncrypted) { encryptedFile = await file.encrypt(); } - final String uploadResp = await client.upload(file); + final String uploadResp = await client.upload( + file, + contentType: sendEncrypted ? "application/octet-stream" : null, + ); // Send event Map content = { diff --git a/lib/src/utils/matrix_file.dart b/lib/src/utils/matrix_file.dart index 11b0ea7..a85fba0 100644 --- a/lib/src/utils/matrix_file.dart +++ b/lib/src/utils/matrix_file.dart @@ -8,10 +8,10 @@ class MatrixFile { Uint8List bytes; String path; + /// Encrypts this file, changes the [bytes] and returns the + /// encryption information as an [EncryptedFile]. Future encrypt() async { - print("[Matrix] Encrypt file with a size of ${bytes.length} bytes"); final EncryptedFile encryptedFile = await encryptFile(bytes); - print("[Matrix] File encryption successfull"); this.bytes = encryptedFile.data; return encryptedFile; } diff --git a/pubspec.lock b/pubspec.lock index 0b20c5d..30ef5f6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -257,7 +257,7 @@ packages: dependency: "direct main" description: path: "." - ref: "1.x.y" + ref: HEAD resolved-ref: b043fcc29031979dc65e5b08e10ebb9b8d2fae30 url: "https://gitlab.com/famedly/libraries/matrix_file_e2ee.git" source: git diff --git a/pubspec.yaml b/pubspec.yaml index 2d97306..e3c0281 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,7 +20,7 @@ dependencies: matrix_file_e2ee: git: url: https://gitlab.com/famedly/libraries/matrix_file_e2ee.git - ref: 1.x.y + #ref: 1.x.y dev_dependencies: test: ^1.0.0