From 86d95518be02c76db0e3bd822083e824bbfef757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 15 Sep 2020 19:10:26 +0200 Subject: [PATCH] matrix-sdk: Fix the case where the encryption feature is disabled. --- matrix_sdk/src/client.rs | 54 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index f9e4edbf..aaf9c433 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -1140,57 +1140,59 @@ impl Client { room_id: &RoomId, body: &str, content_type: &str, - reader: &mut R, + mut reader: &mut R, txn_id: Option, ) -> Result { - let (new_content_type, reader, keys) = if self.is_room_encrypted(room_id).await { + let (response, encrypted_file) = if self.is_room_encrypted(room_id).await { #[cfg(feature = "encryption")] - { - let encryptor = AttachmentEncryptor::new(reader); - let keys = encryptor.finish(); + let mut reader = AttachmentEncryptor::new(reader); + #[cfg(feature = "encryption")] + let content_type = "application/octet-stream"; - ("application/octet-stream", reader, Some(keys)) - } + let response = self.upload(content_type, &mut reader).await?; + + #[cfg(feature = "encryption")] + let keys = { + let keys = reader.finish(); + Some(Box::new(EncryptedFile { + url: response.content_uri.clone(), + key: keys.web_key, + iv: keys.iv, + hashes: keys.hashes, + v: keys.version, + })) + }; #[cfg(not(feature = "encryption"))] - (content_type, reader, None) + let keys: Option> = None; + + (response, keys) } else { - (content_type, reader, None) + let response = self.upload(content_type, &mut reader).await?; + (response, None) }; - let upload = self.upload(new_content_type, reader).await?; - - let url = upload.content_uri.clone(); - - let encrypted_file = keys.map(move |k| { - Box::new(EncryptedFile { - url, - key: k.web_key, - iv: k.iv, - hashes: k.hashes, - v: k.version, - }) - }); + let url = response.content_uri; let content = if content_type.starts_with("image") { // TODO create a thumbnail using the image crate?. MessageEventContent::Image(ImageMessageEventContent { body: body.to_owned(), info: None, - url: Some(upload.content_uri), + url: Some(url), file: encrypted_file, }) } else if content_type.starts_with("audio") { MessageEventContent::Audio(AudioMessageEventContent { body: body.to_owned(), info: None, - url: Some(upload.content_uri), + url: Some(url), file: encrypted_file, }) } else if content_type.starts_with("video") { MessageEventContent::Video(VideoMessageEventContent { body: body.to_owned(), info: None, - url: Some(upload.content_uri), + url: Some(url), file: encrypted_file, }) } else { @@ -1198,7 +1200,7 @@ impl Client { filename: None, body: body.to_owned(), info: None, - url: Some(upload.content_uri), + url: Some(url), file: encrypted_file, }) };