[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;
/// 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<Room> 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<String> upload(MatrixFile file) async {
Future<String> upload(MatrixFile file, {String contentType}) async {
// For testing
if (this.homeserver.toLowerCase() == "https://fakeserver.notexisting") {
return "mxc://example.com/AQwafuaFswefuhsfAFAgsw";
}
Map<String, String> 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<String, dynamic> 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"];
}

View file

@ -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<String, dynamic> content = {

View file

@ -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<EncryptedFile> 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;
}

View file

@ -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

View file

@ -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