Skip to content

Commit c59632e

Browse files
committed
media: always store media to temporary dir
plain media are no longer accessible easily through non-authentified links, just always download the thing Note: the following switch to get_media_file was also implemented, but not kept because get_media_file uses get_media_content + tokio async write internally which is strictly what we are doing... ``` async fn to_uri(&self, client: &Client, body: &str, mimetype: Option<&str>) -> Result<String> { let Some(dir_path) = &args().media_dir else { return Err(Error::msg("<no media dir set>")); }; let media_request = MediaRequestParameters { source: self.clone(), format: MediaFormat::File, }; let mime = mimetype .and_then(|m| m.parse().ok()) .unwrap_or(mime::APPLICATION_OCTET_STREAM); let content = client .media() .get_media_file(&media_request, None, &mime, false, Some(dir_path.into())) .await .context("Could not get decrypted data")?; let filename = body.rsplit_once('/').map(|(_, f)| f).unwrap_or(body); let dir = PathBuf::from(dir_path); if !dir.is_dir() { fs::DirBuilder::new() .mode(0o700) .recursive(true) .create(&dir) .await? } let file = dir.join(filename); content.persist(&file).map_err(|e| e.error)?; let url = args().media_url.as_ref().unwrap_or(dir_path); Ok(format!( "{}/{}", url, utf8_percent_encode(filename, FRAGMENT) )) } ``` Called with: ``` .to_uri( matrirc.matrix(), file_content.filename(), file_content .info .as_ref() .and_then(|i| i.mimetype.as_deref()), ) ``` Fixes #21
1 parent a5ecda0 commit c59632e

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

src/matrix/sync_room_message.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,35 @@ pub trait SourceUri {
3232
#[async_trait]
3333
impl SourceUri for MediaSource {
3434
async fn to_uri(&self, client: &Client, body: &str) -> Result<String> {
35-
match self {
36-
MediaSource::Plain(uri) => {
37-
let homeserver = client.homeserver();
38-
Ok(uri.as_str().replace(
39-
"mxc://",
40-
&format!(
41-
"{}/_matrix/media/r0/download/",
42-
homeserver.as_str().trim_end_matches('/')
43-
),
44-
))
45-
}
46-
_ => {
47-
let Some(dir_path) = &args().media_dir else {
48-
return Err(Error::msg("<encrypted, no media dir set>"));
49-
};
50-
let media_request = MediaRequestParameters {
51-
source: self.clone(),
52-
format: MediaFormat::File,
53-
};
54-
let content = client
55-
.media()
56-
.get_media_content(&media_request, false)
57-
.await
58-
.context("Could not get decrypted data")?;
59-
let filename = body.rsplit_once('/').map(|(_, f)| f).unwrap_or(body);
60-
let dir = PathBuf::from(dir_path);
61-
if !dir.is_dir() {
62-
fs::DirBuilder::new()
63-
.mode(0o700)
64-
.recursive(true)
65-
.create(&dir)
66-
.await?
67-
}
68-
let file = dir.join(filename);
69-
fs::File::create(file).await?.write_all(&content).await?;
70-
let url = args().media_url.as_ref().unwrap_or(dir_path);
71-
Ok(format!(
72-
"{}/{}",
73-
url,
74-
utf8_percent_encode(filename, FRAGMENT)
75-
))
76-
}
35+
let Some(dir_path) = &args().media_dir else {
36+
return Err(Error::msg("<no media dir set>"));
37+
};
38+
let media_request = MediaRequestParameters {
39+
source: self.clone(),
40+
format: MediaFormat::File,
41+
};
42+
let content = client
43+
.media()
44+
.get_media_content(&media_request, false)
45+
.await
46+
.context("Could not get decrypted data")?;
47+
let filename = body.rsplit_once('/').map(|(_, f)| f).unwrap_or(body);
48+
let dir = PathBuf::from(dir_path);
49+
if !dir.is_dir() {
50+
fs::DirBuilder::new()
51+
.mode(0o700)
52+
.recursive(true)
53+
.create(&dir)
54+
.await?
7755
}
56+
let file = dir.join(filename);
57+
fs::File::create(file).await?.write_all(&content).await?;
58+
let url = args().media_url.as_ref().unwrap_or(dir_path);
59+
Ok(format!(
60+
"{}/{}",
61+
url,
62+
utf8_percent_encode(filename, FRAGMENT)
63+
))
7864
}
7965
}
8066

0 commit comments

Comments
 (0)