get rid of cache_remote_thumbnails setting, instead automatically

take thumbnail from federation data if available.
This commit is contained in:
Felix Ableitner 2023-10-30 10:56:16 +01:00
parent ed3e2e0d8c
commit c2a763d6fb
5 changed files with 36 additions and 41 deletions

View file

@ -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.

View file

@ -233,37 +233,32 @@ async fn generate_pictrs_thumbnail(
) -> Result<Url, LemmyError> {
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))?
}
}

View file

@ -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,

View file

@ -168,10 +168,10 @@ impl Page {
}
impl Attachment {
pub(crate) fn new(url: DbUrl, content_type: Option<String>) -> Attachment {
pub(crate) fn new(url: DbUrl, media_type: Option<String>) -> Attachment {
Attachment::Link(Link {
href: url.into(),
content_type,
media_type,
r#type: Default::default(),
})
}

View file

@ -79,10 +79,6 @@ pub struct PictrsConfig {
#[default(None)]
pub api_key: Option<String>,
/// 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.