[Client] Enable file encryption

This commit is contained in:
Christian Pauly 2020-03-23 09:37:51 +00:00
parent 7146be2ca9
commit 2c1caf9072
5 changed files with 16 additions and 9 deletions

View File

@ -135,7 +135,7 @@ class Client {
bool get encryptionEnabled => _olmAccount != null; bool get encryptionEnabled => _olmAccount != null;
/// Whether this client is able to encrypt and decrypt files. /// 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! /// Warning! This endpoint is for testing only!
set rooms(List<Room> newList) { set rooms(List<Room> newList) {
@ -877,15 +877,15 @@ class Client {
/// Uploads a file with the name [fileName] as base64 encoded to the server /// Uploads a file with the name [fileName] as base64 encoded to the server
/// and returns the mxc url as a string. /// and returns the mxc url as a string.
Future<String> upload(MatrixFile file) async { Future<String> upload(MatrixFile file, {String contentType}) async {
// For testing // For testing
if (this.homeserver.toLowerCase() == "https://fakeserver.notexisting") { if (this.homeserver.toLowerCase() == "https://fakeserver.notexisting") {
return "mxc://example.com/AQwafuaFswefuhsfAFAgsw"; return "mxc://example.com/AQwafuaFswefuhsfAFAgsw";
} }
Map<String, String> headers = {}; Map<String, String> headers = {};
headers["Authorization"] = "Bearer $accessToken"; headers["Authorization"] = "Bearer $accessToken";
headers["Content-Type"] = mime(file.path); headers["Content-Type"] = contentType ?? mime(file.path);
String fileName = file.path.split("/").last.toLowerCase(); String fileName = Uri.encodeFull(file.path.split("/").last.toLowerCase());
final url = "$homeserver/_matrix/media/r0/upload?filename=$fileName"; final url = "$homeserver/_matrix/media/r0/upload?filename=$fileName";
final streamedRequest = http.StreamedRequest('POST', Uri.parse(url)) final streamedRequest = http.StreamedRequest('POST', Uri.parse(url))
..headers.addAll(headers); ..headers.addAll(headers);
@ -897,6 +897,10 @@ class Client {
Map<String, dynamic> jsonResponse = json.decode( Map<String, dynamic> jsonResponse = json.decode(
String.fromCharCodes(await streamedResponse.stream.first), 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"]; return jsonResponse["content_uri"];
} }

View File

@ -481,7 +481,10 @@ class Room {
if (sendEncrypted) { if (sendEncrypted) {
encryptedFile = await file.encrypt(); 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 // Send event
Map<String, dynamic> content = { Map<String, dynamic> content = {

View File

@ -8,10 +8,10 @@ class MatrixFile {
Uint8List bytes; Uint8List bytes;
String path; String path;
/// Encrypts this file, changes the [bytes] and returns the
/// encryption information as an [EncryptedFile].
Future<EncryptedFile> encrypt() async { Future<EncryptedFile> encrypt() async {
print("[Matrix] Encrypt file with a size of ${bytes.length} bytes");
final EncryptedFile encryptedFile = await encryptFile(bytes); final EncryptedFile encryptedFile = await encryptFile(bytes);
print("[Matrix] File encryption successfull");
this.bytes = encryptedFile.data; this.bytes = encryptedFile.data;
return encryptedFile; return encryptedFile;
} }

View File

@ -257,7 +257,7 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
path: "." path: "."
ref: "1.x.y" ref: HEAD
resolved-ref: b043fcc29031979dc65e5b08e10ebb9b8d2fae30 resolved-ref: b043fcc29031979dc65e5b08e10ebb9b8d2fae30
url: "https://gitlab.com/famedly/libraries/matrix_file_e2ee.git" url: "https://gitlab.com/famedly/libraries/matrix_file_e2ee.git"
source: git source: git

View File

@ -20,7 +20,7 @@ dependencies:
matrix_file_e2ee: matrix_file_e2ee:
git: git:
url: https://gitlab.com/famedly/libraries/matrix_file_e2ee.git url: https://gitlab.com/famedly/libraries/matrix_file_e2ee.git
ref: 1.x.y #ref: 1.x.y
dev_dependencies: dev_dependencies:
test: ^1.0.0 test: ^1.0.0