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