diff --git a/config/defaults.hjson b/config/defaults.hjson index 8e58f522a..188ca324d 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -43,8 +43,6 @@ url: "http://localhost:8080/" # Set a custom pictrs API key. ( Required for deleting images ) api_key: "string" - # Cache remote images - cache_remote_thumbnails: true # If enabled, all images from remote domains are rewritten to pass through `/api/v3/image_proxy`. # This improves privacy as users don't expose their IP to untrusted servers, and decreases load # on other servers. However it causes more load for the local server. diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index bc59dd91d..8b355c866 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -233,37 +233,32 @@ async fn generate_pictrs_thumbnail( ) -> Result { let pictrs_config = context.settings().pictrs_config()?; - if pictrs_config.cache_remote_thumbnails { - // fetch remote non-pictrs images for persistent thumbnail link - // TODO: should limit size once supported by pictrs - let fetch_url = format!( - "{}image/download?url={}", - pictrs_config.url, - encode(image_url.as_str()) - ); + // fetch remote non-pictrs images for persistent thumbnail link + // TODO: should limit size once supported by pictrs + let fetch_url = format!( + "{}image/download?url={}", + pictrs_config.url, + encode(image_url.as_str()) + ); - let response = context - .client() - .get(&fetch_url) - .timeout(REQWEST_TIMEOUT) - .send() - .await?; + let response = context + .client() + .get(&fetch_url) + .timeout(REQWEST_TIMEOUT) + .send() + .await?; - let response: PictrsResponse = response.json().await?; + let response: PictrsResponse = response.json().await?; - if response.msg == "ok" { - let thumbnail_url = Url::parse(&format!( - "{}/pictrs/image/{}", - context.settings().get_protocol_and_hostname(), - response.files.first().expect("missing pictrs file").file - ))?; - Ok(thumbnail_url) - } else { - Err(LemmyErrorType::PictrsResponseError(response.msg))? - } + if response.msg == "ok" { + let thumbnail_url = Url::parse(&format!( + "{}/pictrs/image/{}", + context.settings().get_protocol_and_hostname(), + response.files.first().expect("missing pictrs file").file + ))?; + Ok(thumbnail_url) } else { - // return the original image as "thumbnail" - Ok(image_url.clone()) + Err(LemmyErrorType::PictrsResponseError(response.msg))? } } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 204d49021..5a652747f 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -30,6 +30,7 @@ use lemmy_api_common::{ local_site_opt_to_sensitive, local_site_opt_to_slur_regex, process_markdown_opt, + proxy_image_link_opt_apub, }, }; use lemmy_db_schema::{ @@ -218,12 +219,17 @@ impl Object for ApubPost { let local_site = LocalSite::read(&mut context.pool()).await.ok(); let allow_sensitive = local_site_opt_to_sensitive(&local_site); let page_is_sensitive = page.sensitive.unwrap_or(false); - let generate_thumbnail = allow_sensitive || !page_is_sensitive; + let allow_generate_thumbnail = allow_sensitive || !page_is_sensitive; + let mut thumbnail_url = page.image.map(|i| i.url); + let do_generate_thumbnail = thumbnail_url.is_none() && allow_generate_thumbnail; + + // Generate local thumbnail only if no thumbnail was federated and 'sensitive' attributes allow it. + let metadata = fetch_link_metadata_opt(url.as_ref(), do_generate_thumbnail, context).await?; + if let Some(thumbnail_url_) = metadata.thumbnail { + thumbnail_url = Some(thumbnail_url_.into()); + } + let thumbnail_url = proxy_image_link_opt_apub(thumbnail_url, context).await?; - // Only fetch metadata if the post has a url and was not seen previously. We dont want to - // waste resources by fetching metadata for the same post multiple times. - // Additionally, only fetch image if content is not sensitive or is allowed on local site. - let metadata = fetch_link_metadata_opt(url.as_ref(), generate_thumbnail, context).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site); let body = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source); @@ -246,7 +252,7 @@ impl Object for ApubPost { embed_title: metadata.title, embed_description: metadata.description, embed_video_url: metadata.embed_video_url, - thumbnail_url: metadata.thumbnail, + thumbnail_url, ap_id: Some(page.id.clone().into()), local: Some(false), language_id, diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 0b5e6856c..85763dad4 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -168,10 +168,10 @@ impl Page { } impl Attachment { - pub(crate) fn new(url: DbUrl, content_type: Option) -> Attachment { + pub(crate) fn new(url: DbUrl, media_type: Option) -> Attachment { Attachment::Link(Link { href: url.into(), - content_type, + media_type, r#type: Default::default(), }) } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 6bd17e9af..d5dc8c8f6 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -79,10 +79,6 @@ pub struct PictrsConfig { #[default(None)] pub api_key: Option, - /// Cache remote images - #[default(true)] - pub cache_remote_thumbnails: bool, - /// If enabled, all images from remote domains are rewritten to pass through `/api/v3/image_proxy`. /// This improves privacy as users don't expose their IP to untrusted servers, and decreases load /// on other servers. However it causes more load for the local server.