add config backwards compat

markdown-link-rule
Felix Ableitner 2024-01-10 15:55:35 +01:00
parent 7e5e4557fa
commit 81359e2df2
5 changed files with 38 additions and 12 deletions

View File

@ -42,6 +42,11 @@
url: "http://localhost:8080/" url: "http://localhost:8080/"
# Set a custom pictrs API key. ( Required for deleting images ) # Set a custom pictrs API key. ( Required for deleting images )
api_key: "string" 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. # Specifies how to handle remote images, so that users don't have to connect directly to remote servers.
image_mode: image_mode:
# Leave images unchanged, don't generate any local thumbnails for post urls. Instead the the # Leave images unchanged, don't generate any local thumbnails for post urls. Instead the the

View File

@ -7,7 +7,7 @@ use encoding::{all::encodings, DecoderTrap};
use lemmy_db_schema::newtypes::DbUrl; use lemmy_db_schema::newtypes::DbUrl;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType}, error::{LemmyError, LemmyErrorType},
settings::structs::{ImageProxyMode, Settings}, settings::structs::{PictrsImageMode, Settings},
version::VERSION, version::VERSION,
REQWEST_TIMEOUT, REQWEST_TIMEOUT,
}; };
@ -258,7 +258,7 @@ async fn generate_pictrs_thumbnail(
) -> Result<Url, LemmyError> { ) -> Result<Url, LemmyError> {
let pictrs_config = context.settings().pictrs_config()?; 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()); return Ok(proxy_image_link(image_url.clone(), context).await?.into());
} }

View File

@ -35,7 +35,7 @@ use lemmy_utils::{
email::{send_email, translations::Lang}, email::{send_email, translations::Lang},
error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult},
rate_limit::{ActionType, BucketConfig}, rate_limit::{ActionType, BucketConfig},
settings::structs::{ImageProxyMode, Settings}, settings::structs::{PictrsImageMode, Settings},
utils::{ utils::{
markdown::markdown_rewrite_image_links, markdown::markdown_rewrite_image_links,
slurs::{build_slur_regex, remove_slurs}, slurs::{build_slur_regex, remove_slurs},
@ -846,7 +846,7 @@ pub async fn process_markdown(
context: &LemmyContext, context: &LemmyContext,
) -> LemmyResult<String> { ) -> LemmyResult<String> {
let text = remove_slurs(text, slur_regex); 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); let (text, links) = markdown_rewrite_image_links(text);
RemoteImage::create(&mut context.pool(), links).await?; RemoteImage::create(&mut context.pool(), links).await?;
Ok(text) Ok(text)
@ -872,13 +872,13 @@ pub async fn process_markdown_opt(
/// as separate parameter so it can be changed in tests. /// as separate parameter so it can be changed in tests.
async fn proxy_image_link_internal( async fn proxy_image_link_internal(
link: Url, link: Url,
image_proxy_mode: ImageProxyMode, image_mode: PictrsImageMode,
context: &LemmyContext, context: &LemmyContext,
) -> LemmyResult<DbUrl> { ) -> LemmyResult<DbUrl> {
// Dont rewrite links pointing to local domain. // Dont rewrite links pointing to local domain.
if link.domain() == Some(&context.settings().hostname) { if link.domain() == Some(&context.settings().hostname) {
Ok(link.into()) Ok(link.into())
} else if image_proxy_mode == ImageProxyMode::ProxyAllImages { } else if image_mode == PictrsImageMode::ProxyAllImages {
let proxied = format!( let proxied = format!(
"{}/api/v3/image_proxy?url={}", "{}/api/v3/image_proxy?url={}",
context.settings().get_protocol_and_hostname(), 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<DbUrl> { pub(crate) async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
proxy_image_link_internal( proxy_image_link_internal(
link, link,
context.settings().pictrs_config()?.image_mode, context.settings().pictrs_config()?.image_mode(),
context, context,
) )
.await .await
@ -996,7 +996,7 @@ mod tests {
// image from local domain is unchanged // image from local domain is unchanged
let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap(); let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap();
let proxied = let proxied =
proxy_image_link_internal(local_url.clone(), ImageProxyMode::ProxyAllImages, &context) proxy_image_link_internal(local_url.clone(), PictrsImageMode::ProxyAllImages, &context)
.await .await
.unwrap(); .unwrap();
assert_eq!(&local_url, proxied.inner()); assert_eq!(&local_url, proxied.inner());
@ -1005,7 +1005,7 @@ mod tests {
let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap(); let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap();
let proxied = proxy_image_link_internal( let proxied = proxy_image_link_internal(
remote_image.clone(), remote_image.clone(),
ImageProxyMode::ProxyAllImages, PictrsImageMode::ProxyAllImages,
&context, &context,
) )
.await .await

View File

@ -12,6 +12,7 @@ use urlencoding::encode;
pub mod structs; pub mod structs;
use crate::settings::structs::PictrsImageMode;
use structs::DatabaseConnection; use structs::DatabaseConnection;
static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
@ -112,3 +113,17 @@ impl Settings {
.ok_or_else(|| anyhow!("images_disabled").into()) .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()
}
}
}

View File

@ -78,9 +78,15 @@ pub struct PictrsConfig {
#[default(None)] #[default(None)]
pub api_key: Option<String>, pub api_key: Option<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
pub(super) cache_external_link_previews: Option<bool>,
/// Specifies how to handle remote images, so that users don't have to connect directly to remote servers. /// Specifies how to handle remote images, so that users don't have to connect directly to remote servers.
#[default(ImageProxyMode::StoreLinkPreviews)] #[default(PictrsImageMode::StoreLinkPreviews)]
pub image_mode: ImageProxyMode, pub(super) image_mode: PictrsImageMode,
/// Timeout for uploading images to pictrs (in seconds) /// Timeout for uploading images to pictrs (in seconds)
#[default(30)] #[default(30)]
@ -89,7 +95,7 @@ pub struct PictrsConfig {
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document, PartialEq)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document, PartialEq)]
#[serde(deny_unknown_fields)] #[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 /// Leave images unchanged, don't generate any local thumbnails for post urls. Instead the the
/// Opengraph image is directly returned as thumbnail /// Opengraph image is directly returned as thumbnail
None, None,