markdown-link-rule-dess
Felix Ableitner 2023-12-21 13:25:12 +01:00
parent becf54c4c4
commit 06257f9e0d
3 changed files with 45 additions and 38 deletions

View File

@ -234,7 +234,15 @@ async fn generate_pictrs_thumbnail(
let pictrs_config = context.settings().pictrs_config()?;
if !pictrs_config.cache_external_link_previews {
return Ok(proxy_image_link(image_url.clone(), context).await?.into());
return Ok(
proxy_image_link(
image_url.clone(),
context.settings().pictrs_config()?.image_proxy,
context,
)
.await?
.into(),
);
}
// fetch remote non-pictrs images for persistent thumbnail link

View File

@ -862,11 +862,13 @@ pub async fn process_markdown_opt(
}
}
pub async fn proxy_image_link(link: Url, context: &LemmyContext) -> LemmyResult<DbUrl> {
pub(crate) async fn proxy_image_link(
link: Url,
image_proxy: bool,
context: &LemmyContext,
) -> LemmyResult<DbUrl> {
// Dont rewrite links pointing to local domain.
if link.domain() == Some(&context.settings().hostname)
|| !context.settings().pictrs_config()?.image_proxy
{
if link.domain() == Some(&context.settings().hostname) || !image_proxy {
return Ok(link.into());
}
@ -883,19 +885,29 @@ pub async fn proxy_image_link_opt_api(
link: &Option<String>,
context: &LemmyContext,
) -> LemmyResult<Option<Option<DbUrl>>> {
let link: Option<Option<DbUrl>> = match link.as_ref().map(String::as_str) {
proxy_image_link_api(link, context).await.map(Some)
}
pub async fn proxy_image_link_api(
link: &Option<String>,
context: &LemmyContext,
) -> LemmyResult<Option<DbUrl>> {
let link: Option<DbUrl> = match link.as_ref().map(String::as_str) {
// An empty string is an erase
Some("") => Some(None),
Some("") => None,
Some(str_url) => Url::parse(str_url)
.map(|u| Some(Some(u.into())))
.map(|u| Some(u.into()))
.with_lemmy_type(LemmyErrorType::InvalidUrl)?,
None => None,
};
if let Some(Some(l)) = link {
proxy_image_link(l.into(), context)
if let Some(l) = link {
proxy_image_link(
l.into(),
context.settings().pictrs_config()?.image_proxy,
context,
)
.await
.map(Some)
.map(Some)
} else {
Ok(link)
}
@ -906,7 +918,9 @@ pub async fn proxy_image_link_opt_apub(
context: &LemmyContext,
) -> LemmyResult<Option<DbUrl>> {
if let Some(l) = link {
proxy_image_link(l, context).await.map(Some)
proxy_image_link(l, context.settings().pictrs_config()?.image_proxy, context)
.await
.map(Some)
} else {
Ok(None)
}
@ -967,12 +981,14 @@ mod tests {
// image from local domain is unchanged
let local_url = Url::parse("http://lemmy-alpha/image.png").unwrap();
let proxied = proxy_image_link(local_url.clone(), &context).await.unwrap();
let proxied = proxy_image_link(local_url.clone(), true, &context)
.await
.unwrap();
assert_eq!(&local_url, proxied.inner());
// image from remote domain is proxied
let remote_image = Url::parse("http://lemmy-beta/image.png").unwrap();
let proxied = proxy_image_link(remote_image.clone(), &context)
let proxied = proxy_image_link(remote_image.clone(), true, &context)
.await
.unwrap();
assert_eq!(
@ -992,7 +1008,7 @@ mod tests {
let context = LemmyContext::init_test_context().await;
assert!(matches!(
proxy_image_link_opt_api(&None, &context).await,
proxy_image_link_api(&None, &context).await,
Ok(None)
));
assert!(matches!(

View File

@ -12,12 +12,12 @@ use lemmy_api_common::{
is_admin,
local_site_to_slur_regex,
process_markdown_opt,
proxy_image_link,
proxy_image_link_api,
proxy_image_link_opt_api,
EndpointType,
},
};
use lemmy_db_schema::{
newtypes::DbUrl,
source::{
actor_language::{CommunityLanguage, SiteLanguage},
community::{
@ -39,7 +39,6 @@ use lemmy_utils::{
validation::{is_valid_actor_name, is_valid_body_field},
},
};
use url::Url;
#[tracing::instrument(skip(context))]
pub async fn create_community(
@ -58,8 +57,8 @@ pub async fn create_community(
check_slurs(&data.name, &slur_regex)?;
check_slurs(&data.title, &slur_regex)?;
let description = process_markdown_opt(&data.description, &slur_regex, &context).await?;
let icon = proxy_image_link_create(&data.icon, &context).await?;
let banner = proxy_image_link_create(&data.banner, &context).await?;
let icon = proxy_image_link_api(&data.icon, &context).await?;
let banner = proxy_image_link_api(&data.banner, &context).await?;
is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
is_valid_body_field(&data.description, false)?;
@ -136,19 +135,3 @@ pub async fn create_community(
build_community_response(&context, local_user_view, community_id).await
}
async fn proxy_image_link_create(
opt: &Option<String>,
context: &LemmyContext,
) -> Result<Option<DbUrl>, LemmyError> {
match opt.as_ref().map(String::as_str) {
// An empty string is nothing
Some("") => Ok(None),
Some(str_url) => {
let url = Url::parse(str_url).with_lemmy_type(LemmyErrorType::InvalidUrl)?;
let url = proxy_image_link(url, context).await?;
Ok(Some(url))
}
None => Ok(None),
}
}