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/"
# 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

View File

@ -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<Url, LemmyError> {
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());
}

View File

@ -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<String> {
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<DbUrl> {
// 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<DbUrl> {
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

View File

@ -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()
}
}
}

View File

@ -78,9 +78,15 @@ pub struct PictrsConfig {
#[default(None)]
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.
#[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,