refactor: Fix errors in api/client/media.rs

This commit is contained in:
Ginger
2026-04-28 09:16:52 -04:00
parent 34b992fc40
commit 496ca80393
+35 -80
View File
@@ -9,7 +9,7 @@ use conduwuit::{
use conduwuit_core::error; use conduwuit_core::error;
use conduwuit_service::{ use conduwuit_service::{
Services, Services,
media::{CACHE_CONTROL_IMMUTABLE, CORP_CROSS_ORIGIN, Dim, FileMeta, MXC_LENGTH}, media::{Dim, FileMeta, MXC_LENGTH},
}; };
use reqwest::Url; use reqwest::Url;
use ruma::{ use ruma::{
@@ -21,7 +21,9 @@ use ruma::{
}, },
media::create_content, media::create_content,
}, },
assign,
}; };
use service::media::mxc::Mxc;
use crate::Ruma; use crate::Ruma;
@@ -30,9 +32,9 @@ pub(crate) async fn get_media_config_route(
State(services): State<crate::State>, State(services): State<crate::State>,
_body: Ruma<get_media_config::v1::Request>, _body: Ruma<get_media_config::v1::Request>,
) -> Result<get_media_config::v1::Response> { ) -> Result<get_media_config::v1::Response> {
Ok(get_media_config::v1::Response { Ok(get_media_config::v1::Response::new(ruma_from_usize(
upload_size: ruma_from_usize(services.server.config.max_request_size), services.server.config.max_request_size,
}) )))
} }
/// # `POST /_matrix/media/v3/upload` /// # `POST /_matrix/media/v3/upload`
@@ -82,10 +84,9 @@ pub(crate) async fn create_content_route(
.flatten() .flatten()
}); });
Ok(create_content::v3::Response { Ok(assign!(create_content::v3::Response::new(mxc.to_string().into()), {
content_uri: mxc.to_string().into(),
blurhash: blurhash.flatten(), blurhash: blurhash.flatten(),
}) }))
} }
/// # `GET /_matrix/client/v1/media/thumbnail/{serverName}/{mediaId}` /// # `GET /_matrix/client/v1/media/thumbnail/{serverName}/{mediaId}`
@@ -114,7 +115,7 @@ pub(crate) async fn get_content_thumbnail_route(
content, content,
content_type, content_type,
content_disposition, content_disposition,
} = match fetch_thumbnail(&services, &mxc, user, body.timeout_ms, &dim).await { } = match fetch_thumbnail_meta(&services, &mxc, user, body.timeout_ms, &dim).await {
| Ok(meta) => meta, | Ok(meta) => meta,
| Err(conduwuit::Error::Io(e)) => match e.kind() { | Err(conduwuit::Error::Io(e)) => match e.kind() {
| std::io::ErrorKind::NotFound => | std::io::ErrorKind::NotFound =>
@@ -128,13 +129,14 @@ pub(crate) async fn get_content_thumbnail_route(
| Err(_) => return Err!(Request(Unknown("Unknown error when fetching thumbnail."))), | Err(_) => return Err!(Request(Unknown("Unknown error when fetching thumbnail."))),
}; };
Ok(get_content_thumbnail::v1::Response { let content_disposition =
file: content.expect("entire file contents"), make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
content_type: content_type.map(Into::into),
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), Ok(get_content_thumbnail::v1::Response::new(
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), content.expect("entire file contents"),
content_type.unwrap_or_default(),
content_disposition, content_disposition,
}) ))
} }
/// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}` /// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}`
@@ -161,7 +163,7 @@ pub(crate) async fn get_content_route(
content, content,
content_type, content_type,
content_disposition, content_disposition,
} = match fetch_file(&services, &mxc, user, body.timeout_ms, None).await { } = match fetch_file_meta(&services, &mxc, user, body.timeout_ms).await {
| Ok(meta) => meta, | Ok(meta) => meta,
| Err(conduwuit::Error::Io(e)) => match e.kind() { | Err(conduwuit::Error::Io(e)) => match e.kind() {
| std::io::ErrorKind::NotFound => return Err!(Request(NotFound("Media not found."))), | std::io::ErrorKind::NotFound => return Err!(Request(NotFound("Media not found."))),
@@ -174,13 +176,14 @@ pub(crate) async fn get_content_route(
| Err(_) => return Err!(Request(Unknown("Unknown error when fetching file."))), | Err(_) => return Err!(Request(Unknown("Unknown error when fetching file."))),
}; };
Ok(get_content::v1::Response { let content_disposition =
file: content.expect("entire file contents"), make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
content_type: content_type.map(Into::into),
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), Ok(get_content::v1::Response::new(
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), content.expect("entire file contents"),
content_type.unwrap_or_default(),
content_disposition, content_disposition,
}) ))
} }
/// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}/{fileName}` /// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}/{fileName}`
@@ -208,7 +211,7 @@ pub(crate) async fn get_content_as_filename_route(
content, content,
content_type, content_type,
content_disposition, content_disposition,
} = match fetch_file(&services, &mxc, user, body.timeout_ms, None).await { } = match fetch_file_meta(&services, &mxc, user, body.timeout_ms).await {
| Ok(meta) => meta, | Ok(meta) => meta,
| Err(conduwuit::Error::Io(e)) => match e.kind() { | Err(conduwuit::Error::Io(e)) => match e.kind() {
| std::io::ErrorKind::NotFound => return Err!(Request(NotFound("Media not found."))), | std::io::ErrorKind::NotFound => return Err!(Request(NotFound("Media not found."))),
@@ -221,13 +224,17 @@ pub(crate) async fn get_content_as_filename_route(
| Err(_) => return Err!(Request(Unknown("Unknown error when fetching file."))), | Err(_) => return Err!(Request(Unknown("Unknown error when fetching file."))),
}; };
Ok(get_content_as_filename::v1::Response { let content_disposition = make_content_disposition(
file: content.expect("entire file contents"), content_disposition.as_ref(),
content_type: content_type.map(Into::into), content_type.as_deref(),
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), Some(&body.filename),
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), );
Ok(get_content_as_filename::v1::Response::new(
content.expect("entire file contents"),
content_type.unwrap_or_default(),
content_disposition, content_disposition,
}) ))
} }
/// # `GET /_matrix/client/v1/media/preview_url` /// # `GET /_matrix/client/v1/media/preview_url`
@@ -278,58 +285,6 @@ pub(crate) async fn get_media_preview_route(
}) })
} }
async fn fetch_thumbnail(
services: &Services,
mxc: &Mxc<'_>,
user: &UserId,
timeout_ms: Duration,
dim: &Dim,
) -> Result<FileMeta> {
let FileMeta {
content,
content_type,
content_disposition,
} = fetch_thumbnail_meta(services, mxc, user, timeout_ms, dim).await?;
let content_disposition = Some(make_content_disposition(
content_disposition.as_ref(),
content_type.as_deref(),
None,
));
Ok(FileMeta {
content,
content_type,
content_disposition,
})
}
async fn fetch_file(
services: &Services,
mxc: &Mxc<'_>,
user: &UserId,
timeout_ms: Duration,
filename: Option<&str>,
) -> Result<FileMeta> {
let FileMeta {
content,
content_type,
content_disposition,
} = fetch_file_meta(services, mxc, user, timeout_ms).await?;
let content_disposition = Some(make_content_disposition(
content_disposition.as_ref(),
content_type.as_deref(),
filename,
));
Ok(FileMeta {
content,
content_type,
content_disposition,
})
}
async fn fetch_thumbnail_meta( async fn fetch_thumbnail_meta(
services: &Services, services: &Services,
mxc: &Mxc<'_>, mxc: &Mxc<'_>,