Skip to content

Conversation

@martinetd
Copy link
Owner

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

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
@martinetd martinetd merged commit c59632e into master Mar 30, 2025
3 checks passed
@martinetd martinetd deleted the media_dl branch March 30, 2025 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

image/attachments in plain text - link is wrong

2 participants