Commit c59632e
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 #211 parent a5ecda0 commit c59632e
1 file changed
+28
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
77 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
78 | 64 | | |
79 | 65 | | |
80 | 66 | | |
| |||
0 commit comments