From 81359e2df2388f40487621d5c2008f2fb684767b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 10 Jan 2024 15:55:35 +0100 Subject: [PATCH] add config backwards compat --- config/defaults.hjson | 5 +++++ crates/api_common/src/request.rs | 4 ++-- crates/api_common/src/utils.rs | 14 +++++++------- crates/utils/src/settings/mod.rs | 15 +++++++++++++++ crates/utils/src/settings/structs.rs | 12 +++++++++--- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/config/defaults.hjson b/config/defaults.hjson index 538125104..c52f9055e 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -42,6 +42,11 @@ url: "http://localhost:8080/" # Set a custom pictrs API key. ( Required for deleting images ) api_key: "string" + # Backwards compatibility with 0.18.1. False is equivalent to `image_mode: None`, true is + # equivalent to `image_mode: StoreLinkPreviews`. + # + # To be removed in 0.20 + cache_external_link_previews: true # Specifies how to handle remote images, so that users don't have to connect directly to remote servers. image_mode: # Leave images unchanged, don't generate any local thumbnails for post urls. Instead the the diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index bf6e71596..b9ab42f7c 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -7,7 +7,7 @@ use encoding::{all::encodings, DecoderTrap}; use lemmy_db_schema::newtypes::DbUrl; use lemmy_utils::{ error::{LemmyError, LemmyErrorType}, - settings::structs::{ImageProxyMode, Settings}, + settings::structs::{PictrsImageMode, Settings}, version::VERSION, REQWEST_TIMEOUT, }; @@ -258,7 +258,7 @@ async fn generate_pictrs_thumbnail( ) -> Result { let pictrs_config = context.settings().pictrs_config()?; - if pictrs_config.image_mode == ImageProxyMode::ProxyAllImages { + if pictrs_config.image_mode() == PictrsImageMode::ProxyAllImages { return Ok(proxy_image_link(image_url.clone(), context).await?.into()); } diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index b5e52f433..5fbe26841 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -35,7 +35,7 @@ use lemmy_utils::{ email::{send_email, translations::Lang}, error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}, rate_limit::{ActionType, BucketConfig}, - settings::structs::{ImageProxyMode, Settings}, + settings::structs::{PictrsImageMode, Settings}, utils::{ markdown::markdown_rewrite_image_links, slurs::{build_slur_regex, remove_slurs}, @@ -846,7 +846,7 @@ pub async fn process_markdown( context: &LemmyContext, ) -> LemmyResult { let text = remove_slurs(text, slur_regex); - if context.settings().pictrs_config()?.image_mode == ImageProxyMode::ProxyAllImages { + if context.settings().pictrs_config()?.image_mode() == PictrsImageMode::ProxyAllImages { let (text, links) = markdown_rewrite_image_links(text); RemoteImage::create(&mut context.pool(), links).await?; Ok(text) @@ -872,13 +872,13 @@ pub async fn process_markdown_opt( /// as separate parameter so it can be changed in tests. async fn proxy_image_link_internal( link: Url, - image_proxy_mode: ImageProxyMode, + image_mode: PictrsImageMode, context: &LemmyContext, ) -> LemmyResult { // Dont rewrite links pointing to local domain. if link.domain() == Some(&context.settings().hostname) { Ok(link.into()) - } else if image_proxy_mode == ImageProxyMode::ProxyAllImages { + } else if image_mode == PictrsImageMode::ProxyAllImages { let proxied = format!( "{}/api/v3/image_proxy?url={}", context.settings().get_protocol_and_hostname(), @@ -896,7 +896,7 @@ async fn proxy_image_link_internal( pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult { proxy_image_link_internal( link, - context.settings().pictrs_config()?.image_mode, + context.settings().pictrs_config()?.image_mode(), context, ) .await @@ -996,7 +996,7 @@ mod tests { // image from local domain is unchanged let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap(); let proxied = - proxy_image_link_internal(local_url.clone(), ImageProxyMode::ProxyAllImages, &context) + proxy_image_link_internal(local_url.clone(), PictrsImageMode::ProxyAllImages, &context) .await .unwrap(); assert_eq!(&local_url, proxied.inner()); @@ -1005,7 +1005,7 @@ mod tests { let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap(); let proxied = proxy_image_link_internal( remote_image.clone(), - ImageProxyMode::ProxyAllImages, + PictrsImageMode::ProxyAllImages, &context, ) .await diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index ca320164b..b68bc62f6 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -12,6 +12,7 @@ use urlencoding::encode; pub mod structs; +use crate::settings::structs::PictrsImageMode; use structs::DatabaseConnection; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; @@ -112,3 +113,17 @@ impl Settings { .ok_or_else(|| anyhow!("images_disabled").into()) } } + +impl PictrsConfig { + pub fn image_mode(&self) -> PictrsImageMode { + if let Some(cache_external_link_previews) = self.cache_external_link_previews { + return if cache_external_link_previews { + PictrsImageMode::StoreLinkPreviews + } else { + PictrsImageMode::None + }; + } else { + self.image_mode.clone() + } + } +} diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index c0dd5a010..46e9b747c 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -78,9 +78,15 @@ pub struct PictrsConfig { #[default(None)] pub api_key: Option, + /// Backwards compatibility with 0.18.1. False is equivalent to `image_mode: None`, true is + /// equivalent to `image_mode: StoreLinkPreviews`. + /// + /// To be removed in 0.20 + pub(super) cache_external_link_previews: Option, + /// Specifies how to handle remote images, so that users don't have to connect directly to remote servers. - #[default(ImageProxyMode::StoreLinkPreviews)] - pub image_mode: ImageProxyMode, + #[default(PictrsImageMode::StoreLinkPreviews)] + pub(super) image_mode: PictrsImageMode, /// Timeout for uploading images to pictrs (in seconds) #[default(30)] @@ -89,7 +95,7 @@ pub struct PictrsConfig { #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document, PartialEq)] #[serde(deny_unknown_fields)] -pub enum ImageProxyMode { +pub enum PictrsImageMode { /// Leave images unchanged, don't generate any local thumbnails for post urls. Instead the the /// Opengraph image is directly returned as thumbnail None,