From 3fe0110649a339a143a9c9c5842b34e63b5b6065 Mon Sep 17 00:00:00 2001 From: Lambda Date: Sun, 11 Aug 2024 20:40:01 +0000 Subject: [PATCH] media: convert allow_remote to enum --- src/api/client_server/media.rs | 45 ++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs index 91c0fdfc..9b653944 100644 --- a/src/api/client_server/media.rs +++ b/src/api/client_server/media.rs @@ -178,6 +178,23 @@ pub(crate) async fn create_content_route( })) } +/// Whether or not to allow remote content to be loaded +#[derive(Clone, Copy, PartialEq, Eq)] +enum AllowRemote { + Yes, + No, +} + +impl From for AllowRemote { + fn from(allow: bool) -> Self { + if allow { + Self::Yes + } else { + Self::No + } + } +} + struct RemoteResponse { #[allow(unused)] metadata: authenticated_media_fed::ContentMetadata, @@ -387,7 +404,7 @@ pub(crate) async fn get_content_legacy_route( } } - let allow_remote = body.allow_remote; + let allow_remote = body.allow_remote.into(); get_content_route_ruma(body.map_body(convert_request), allow_remote) .await @@ -411,7 +428,7 @@ pub(crate) async fn get_content_legacy_route( pub(crate) async fn get_content_route( body: Ar, ) -> Result { - get_content_route_ruma(body, true).await.map(|x| { + get_content_route_ruma(body, AllowRemote::Yes).await.map(|x| { let mut r = Ra(x).into_response(); set_header_or_panic( @@ -426,7 +443,7 @@ pub(crate) async fn get_content_route( async fn get_content_route_ruma( body: Ar, - allow_remote: bool, + allow_remote: AllowRemote, ) -> Result { let mxc = MxcData::new(&body.server_name, &body.media_id)?; @@ -447,7 +464,7 @@ async fn get_content_route_ruma( content_type, }) } else if &*body.server_name != services().globals.server_name() - && allow_remote + && allow_remote == AllowRemote::Yes { let remote_response = get_remote_content(&mxc).await?; Ok(authenticated_media_client::get_content::v1::Response { @@ -511,7 +528,7 @@ pub(crate) async fn get_content_as_filename_legacy_route( } } - let allow_remote = body.allow_remote; + let allow_remote = body.allow_remote.into(); get_content_as_filename_route_ruma( body.map_body(convert_request), allow_remote, @@ -537,7 +554,7 @@ pub(crate) async fn get_content_as_filename_legacy_route( pub(crate) async fn get_content_as_filename_route( body: Ar, ) -> Result { - get_content_as_filename_route_ruma(body, true).await.map(|x| { + get_content_as_filename_route_ruma(body, AllowRemote::Yes).await.map(|x| { let mut r = Ra(x).into_response(); set_header_or_panic( @@ -550,9 +567,9 @@ pub(crate) async fn get_content_as_filename_route( }) } -pub(crate) async fn get_content_as_filename_route_ruma( +async fn get_content_as_filename_route_ruma( body: Ar, - allow_remote: bool, + allow_remote: AllowRemote, ) -> Result { let mxc = MxcData::new(&body.server_name, &body.media_id)?; @@ -573,7 +590,7 @@ pub(crate) async fn get_content_as_filename_route_ruma( content_type, }) } else if &*body.server_name != services().globals.server_name() - && allow_remote + && allow_remote == AllowRemote::Yes { let remote_response = get_remote_content(&mxc).await?; @@ -660,7 +677,7 @@ pub(crate) async fn get_content_thumbnail_legacy_route( } } - let allow_remote = body.allow_remote; + let allow_remote = body.allow_remote.into(); get_content_thumbnail_route_ruma( body.map_body(convert_request), @@ -683,7 +700,7 @@ pub(crate) async fn get_content_thumbnail_legacy_route( pub(crate) async fn get_content_thumbnail_route( body: Ar, ) -> Result { - get_content_thumbnail_route_ruma(body, true).await.map(|x| { + get_content_thumbnail_route_ruma(body, AllowRemote::Yes).await.map(|x| { let mut r = Ra(x).into_response(); fix_thumbnail_headers(&mut r); @@ -798,7 +815,7 @@ pub(crate) async fn get_remote_thumbnail( async fn get_content_thumbnail_route_ruma( body: Ar, - allow_remote: bool, + allow_remote: AllowRemote, ) -> Result { let mxc = MxcData::new(&body.server_name, &body.media_id)?; let width = body.width.try_into().map_err(|_| { @@ -827,7 +844,9 @@ async fn get_content_thumbnail_route_ruma( return Ok(make_response(file, content_type)); } - if &*body.server_name != services().globals.server_name() && allow_remote { + if &*body.server_name != services().globals.server_name() + && allow_remote == AllowRemote::Yes + { let get_thumbnail_response = get_remote_thumbnail( &body.server_name, authenticated_media_fed::get_content_thumbnail::v1::Request {