diff --git a/mstickerlib/src/matrix/mod.rs b/mstickerlib/src/matrix/mod.rs index 4c39acc..ed82026 100644 --- a/mstickerlib/src/matrix/mod.rs +++ b/mstickerlib/src/matrix/mod.rs @@ -13,6 +13,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::{ fmt::{Debug, Display}, ops::Deref, + path::Path, sync::Arc }; use stickerpicker::StickerWidget; @@ -135,7 +136,7 @@ pub async fn set_widget(matrix: &Config, sender: String, url: String) -> Result< let answer = CLIENT .get() .put(format!( - "{}/_matrix/client/r0/user/{}/account_data/m.widgets", + "{}/_matrix/client/v3/user/{}/account_data/m.widgets", matrix.homeserver_url, matrix.user )) .query(&[("access_token", &matrix.access_token)]) @@ -159,7 +160,7 @@ pub async fn whoami(matrix: &Config) -> Result { Url::parse(&matrix.homeserver_url)?; //check if homeserver_url is a valid url let answer = CLIENT .get() - .get(format!("{}/_matrix/client/r0/account/whoami", matrix.homeserver_url)) + .get(format!("{}/_matrix/client/v3/account/whoami", matrix.homeserver_url)) .query(&[("access_token", &matrix.access_token)]) .send() .await?; @@ -176,17 +177,30 @@ pub async fn whoami(matrix: &Config) -> Result { } } -pub(crate) async fn upload(matrix: &Config, filename: &String, data: Arc>, mimetype: &str) -> Result { - let mut mxc = upload_ref(matrix, filename, data.as_slice(), mimetype).await?; +pub(crate) async fn upload

(matrix: &Config, path: P, data: Arc>, mimetype: &str) -> Result +where + P: AsRef +{ + let mut mxc = upload_ref(matrix, path, data.as_slice(), mimetype).await?; mxc.data = Some(data); Ok(mxc) } -pub(crate) async fn upload_ref(matrix: &Config, filename: &String, data: &[u8], mimetype: &str) -> Result { +pub(crate) async fn upload_ref

(matrix: &Config, path: P, data: &[u8], mimetype: &str) -> Result +where + P: AsRef +{ + let path = path.as_ref(); + let filename = path + .file_name() + .unwrap_or_else(|| path.as_os_str()) + .to_str() + .expect("Not valid UTF-8"); + let slash = if matrix.homeserver_url.ends_with('/') { "" } else { "/" }; let answer = CLIENT .get() - .post(&format!("{}/_matrix/media/r0/upload", matrix.homeserver_url)) - .query(&[("access_token", &matrix.access_token), ("filename", filename)]) + .post(&format!("{}{slash}_matrix/media/v3/upload", matrix.homeserver_url)) + .query(&[("access_token", matrix.access_token.as_str()), ("filename", filename)]) .header("Content-Type", mimetype) .body(data.to_owned()) //TODO check for better solution .send()