Skip to content

Commit 99a9dc4

Browse files
committed
strip directories before uploading media
1 parent c95b247 commit 99a9dc4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

mstickerlib/src/matrix/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
1313
use std::{
1414
fmt::{Debug, Display},
1515
ops::Deref,
16+
path::Path,
1617
sync::Arc
1718
};
1819
use stickerpicker::StickerWidget;
@@ -176,17 +177,29 @@ pub async fn whoami(matrix: &Config) -> Result<Whoami, Error> {
176177
}
177178
}
178179

179-
pub(crate) async fn upload(matrix: &Config, filename: &String, data: Arc<Vec<u8>>, mimetype: &str) -> Result<Mxc, Error> {
180-
let mut mxc = upload_ref(matrix, filename, data.as_slice(), mimetype).await?;
180+
pub(crate) async fn upload<P>(matrix: &Config, path: P, data: Arc<Vec<u8>>, mimetype: &str) -> Result<Mxc, Error>
181+
where
182+
P: AsRef<Path>
183+
{
184+
let mut mxc = upload_ref(matrix, path, data.as_slice(), mimetype).await?;
181185
mxc.data = Some(data);
182186
Ok(mxc)
183187
}
184188

185-
pub(crate) async fn upload_ref(matrix: &Config, filename: &String, data: &[u8], mimetype: &str) -> Result<Mxc, Error> {
189+
pub(crate) async fn upload_ref<P>(matrix: &Config, path: P, data: &[u8], mimetype: &str) -> Result<Mxc, Error>
190+
where
191+
P: AsRef<Path>
192+
{
193+
let path = path.as_ref();
194+
let filename = path
195+
.file_name()
196+
.unwrap_or_else(|| path.as_os_str())
197+
.to_str()
198+
.expect("Not valid UTF-8");
186199
let answer = CLIENT
187200
.get()
188201
.post(&format!("{}/_matrix/media/v3/upload", matrix.homeserver_url))
189-
.query(&[("access_token", &matrix.access_token), ("filename", filename)])
202+
.query(&[("access_token", matrix.access_token.as_str()), ("filename", filename)])
190203
.header("Content-Type", mimetype)
191204
.body(data.to_owned()) //TODO check for better solution
192205
.send()

0 commit comments

Comments
 (0)