diff --git a/matrix_sdk/Cargo.toml b/matrix_sdk/Cargo.toml index 46372aa4..510a1b01 100644 --- a/matrix_sdk/Cargo.toml +++ b/matrix_sdk/Cargo.toml @@ -36,6 +36,7 @@ thiserror = "1.0.20" tracing = "0.1.19" url = "2.1.1" zeroize = "1.1.0" +mime = "0.3.16" matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } diff --git a/matrix_sdk/examples/image_bot.rs b/matrix_sdk/examples/image_bot.rs index 68099d89..68c394f7 100644 --- a/matrix_sdk/examples/image_bot.rs +++ b/matrix_sdk/examples/image_bot.rs @@ -1,3 +1,4 @@ +use mime; use std::{ env, fs::File, @@ -52,7 +53,7 @@ impl EventEmitter for ImageBot { let mut image = self.image.lock().await; self.client - .room_send_attachment(&room_id, "cat", "image/jpg", &mut *image, None) + .room_send_attachment(&room_id, "cat", &mime::IMAGE_JPEG, &mut *image, None) .await .unwrap(); diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index fb1dc752..43417658 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -30,6 +30,7 @@ use std::{ use dashmap::DashMap; use futures_timer::Delay as sleep; use http::HeaderValue; +use mime::{self, Mime}; use reqwest::header::InvalidHeaderValue; use url::Url; #[cfg(feature = "encryption")] @@ -1159,6 +1160,7 @@ impl Client { /// # use std::{path::PathBuf, fs::File, io::Read}; /// # use matrix_sdk::{Client, identifiers::room_id}; /// # use url::Url; + /// # use mime; /// # use futures::executor::block_on; /// # block_on(async { /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); @@ -1168,7 +1170,7 @@ impl Client { /// let mut image = File::open(path).unwrap(); /// /// let response = client - /// .room_send_attachment(&room_id, "My favorite cat", "image/jpg", &mut image, None) + /// .room_send_attachment(&room_id, "My favorite cat", &mime::IMAGE_JPEG, &mut image, None) /// .await /// .expect("Can't upload my cat."); /// # }); @@ -1177,7 +1179,7 @@ impl Client { &self, room_id: &RoomId, body: &str, - content_type: &str, + content_type: &Mime, mut reader: &mut R, txn_id: Option, ) -> Result { @@ -1185,9 +1187,9 @@ impl Client { #[cfg(feature = "encryption")] let mut reader = AttachmentEncryptor::new(reader); #[cfg(feature = "encryption")] - let content_type = "application/octet-stream"; + let content_type = mime::APPLICATION_OCTET_STREAM; - let response = self.upload(content_type, &mut reader).await?; + let response = self.upload(&content_type, &mut reader).await?; #[cfg(feature = "encryption")] let keys = { @@ -1205,42 +1207,41 @@ impl Client { (response, keys) } else { - let response = self.upload(content_type, &mut reader).await?; + let response = self.upload(&content_type, &mut reader).await?; (response, None) }; let url = response.content_uri; - let content = if content_type.starts_with("image") { - // TODO create a thumbnail using the image crate?. - MessageEventContent::Image(ImageMessageEventContent { + let content = match content_type.type_() { + mime::IMAGE => { + // TODO create a thumbnail using the image crate?. + MessageEventContent::Image(ImageMessageEventContent { + body: body.to_owned(), + info: None, + url: Some(url), + file: encrypted_file, + }) + } + mime::AUDIO => MessageEventContent::Audio(AudioMessageEventContent { body: body.to_owned(), info: None, url: Some(url), file: encrypted_file, - }) - } else if content_type.starts_with("audio") { - MessageEventContent::Audio(AudioMessageEventContent { + }), + mime::VIDEO => MessageEventContent::Video(VideoMessageEventContent { body: body.to_owned(), info: None, url: Some(url), file: encrypted_file, - }) - } else if content_type.starts_with("video") { - MessageEventContent::Video(VideoMessageEventContent { - body: body.to_owned(), - info: None, - url: Some(url), - file: encrypted_file, - }) - } else { - MessageEventContent::File(FileMessageEventContent { + }), + _ => MessageEventContent::File(FileMessageEventContent { filename: None, body: body.to_owned(), info: None, url: Some(url), file: encrypted_file, - }) + }), }; self.room_send( @@ -1268,6 +1269,7 @@ impl Client { /// # use matrix_sdk::{Client, identifiers::room_id}; /// # use url::Url; /// # use futures::executor::block_on; + /// # use mime; /// # block_on(async { /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); /// # let mut client = Client::new(homeserver).unwrap(); @@ -1275,7 +1277,7 @@ impl Client { /// let mut image = File::open(path).unwrap(); /// /// let response = client - /// .upload("image/jpg", &mut image) + /// .upload(&mime::IMAGE_JPEG, &mut image) /// .await /// .expect("Can't upload my cat."); /// @@ -1284,13 +1286,13 @@ impl Client { /// ``` pub async fn upload( &self, - content_type: &str, + content_type: &Mime, reader: &mut impl Read, ) -> Result { let mut data = Vec::new(); reader.read_to_end(&mut data)?; - let request = create_content::Request::new(content_type, data); + let request = create_content::Request::new(content_type.essence_str(), data); self.http_client.upload(request).await } @@ -1950,6 +1952,7 @@ mod test { thirdparty, }; use matrix_sdk_test::{test_json, EventBuilder, EventsJson}; + use mime; use mockito::{mock, Matcher}; use serde_json::json; use tempfile::tempdir; @@ -2521,7 +2524,7 @@ mod test { Matcher::Regex(r"^/_matrix/media/r0/upload".to_string()), ) .with_status(200) - .match_header("content-type", "image/jpg") + .match_header("content-type", "image/jpeg") .with_body( json!({ "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" @@ -2535,7 +2538,7 @@ mod test { let mut media = Cursor::new("Hello world"); let response = client - .room_send_attachment(&room_id, "image", "image/jpg", &mut media, None) + .room_send_attachment(&room_id, "image", &mime::IMAGE_JPEG, &mut media, None) .await .unwrap();