diff --git a/src/api/client/media.rs b/src/api/client/media.rs index 2440c571e..181093e69 100644 --- a/src/api/client/media.rs +++ b/src/api/client/media.rs @@ -9,7 +9,7 @@ use conduwuit::{ use conduwuit_core::error; use conduwuit_service::{ Services, - media::{CACHE_CONTROL_IMMUTABLE, CORP_CROSS_ORIGIN, Dim, FileMeta, MXC_LENGTH}, + media::{Dim, FileMeta, MXC_LENGTH}, }; use reqwest::Url; use ruma::{ @@ -21,7 +21,9 @@ use ruma::{ }, media::create_content, }, + assign, }; +use service::media::mxc::Mxc; use crate::Ruma; @@ -30,9 +32,9 @@ pub(crate) async fn get_media_config_route( State(services): State, _body: Ruma, ) -> Result { - Ok(get_media_config::v1::Response { - upload_size: ruma_from_usize(services.server.config.max_request_size), - }) + Ok(get_media_config::v1::Response::new(ruma_from_usize( + services.server.config.max_request_size, + ))) } /// # `POST /_matrix/media/v3/upload` @@ -82,10 +84,9 @@ pub(crate) async fn create_content_route( .flatten() }); - Ok(create_content::v3::Response { - content_uri: mxc.to_string().into(), + Ok(assign!(create_content::v3::Response::new(mxc.to_string().into()), { blurhash: blurhash.flatten(), - }) + })) } /// # `GET /_matrix/client/v1/media/thumbnail/{serverName}/{mediaId}` @@ -114,7 +115,7 @@ pub(crate) async fn get_content_thumbnail_route( content, content_type, 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, | Err(conduwuit::Error::Io(e)) => match e.kind() { | 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."))), }; - Ok(get_content_thumbnail::v1::Response { - file: content.expect("entire file contents"), - content_type: content_type.map(Into::into), - cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), - cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), + let content_disposition = + make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None); + + Ok(get_content_thumbnail::v1::Response::new( + content.expect("entire file contents"), + content_type.unwrap_or_default(), content_disposition, - }) + )) } /// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}` @@ -161,7 +163,7 @@ pub(crate) async fn get_content_route( content, content_type, 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, | Err(conduwuit::Error::Io(e)) => match e.kind() { | 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."))), }; - Ok(get_content::v1::Response { - file: content.expect("entire file contents"), - content_type: content_type.map(Into::into), - cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), - cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), + let content_disposition = + make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None); + + Ok(get_content::v1::Response::new( + content.expect("entire file contents"), + content_type.unwrap_or_default(), content_disposition, - }) + )) } /// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}/{fileName}` @@ -208,7 +211,7 @@ pub(crate) async fn get_content_as_filename_route( content, content_type, 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, | Err(conduwuit::Error::Io(e)) => match e.kind() { | 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."))), }; - Ok(get_content_as_filename::v1::Response { - file: content.expect("entire file contents"), - content_type: content_type.map(Into::into), - cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()), - cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()), + let content_disposition = make_content_disposition( + content_disposition.as_ref(), + content_type.as_deref(), + Some(&body.filename), + ); + + Ok(get_content_as_filename::v1::Response::new( + content.expect("entire file contents"), + content_type.unwrap_or_default(), content_disposition, - }) + )) } /// # `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 { - 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 { - 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( services: &Services, mxc: &Mxc<'_>,