From e632cdf8e30e9b20811814db9c089184bf5a2a97 Mon Sep 17 00:00:00 2001 From: sunaurus Date: Fri, 28 Jun 2024 22:51:58 +0300 Subject: [PATCH] Always save remote image data --- crates/api_common/src/utils.rs | 9 ++++--- crates/db_schema/src/impls/images.rs | 40 +++++++++++----------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 97b12cc5b..6e05ffdb1 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::{ community::{Community, CommunityModerator, CommunityUpdateForm}, community_block::CommunityBlock, email_verification::{EmailVerification, EmailVerificationForm}, - images::RemoteImage, + images::{ImageDetails, RemoteImage}, instance::Instance, instance_block::InstanceBlock, local_site::LocalSite, @@ -953,6 +953,7 @@ pub async fn process_markdown( if context.settings().pictrs_config()?.image_mode() == PictrsImageMode::ProxyAllImages { let (text, links) = markdown_rewrite_image_links(text); + RemoteImage::create(&mut context.pool(), links.clone()).await?; // Create images and image detail rows for link in links { @@ -962,7 +963,7 @@ pub async fn process_markdown( let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?; let details_form = details.build_image_details_form(&proxied); - RemoteImage::create(&mut context.pool(), &details_form).await?; + ImageDetails::create(&mut context.pool(), &details_form).await?; } } Ok(text) @@ -998,13 +999,15 @@ async fn proxy_image_link_internal( if link.domain() == Some(&context.settings().hostname) { Ok(link.into()) } else if image_mode == PictrsImageMode::ProxyAllImages { + RemoteImage::create(&mut context.pool(), vec![link.clone()]).await?; + let proxied = build_proxied_image_url(&link, &context.settings().get_protocol_and_hostname())?; // This should fail softly, since pictrs might not even be running let details_res = fetch_pictrs_proxied_image_details(&link, context).await; if let Ok(details) = details_res { let details_form = details.build_image_details_form(&proxied); - RemoteImage::create(&mut context.pool(), &details_form).await?; + ImageDetails::create(&mut context.pool(), &details_form).await?; }; Ok(proxied.into()) diff --git a/crates/db_schema/src/impls/images.rs b/crates/db_schema/src/impls/images.rs index 547bfc4e2..48dc57595 100644 --- a/crates/db_schema/src/impls/images.rs +++ b/crates/db_schema/src/impls/images.rs @@ -20,7 +20,8 @@ use diesel::{ NotFound, QueryDsl, }; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; +use url::Url; impl LocalImage { pub async fn create( @@ -38,7 +39,7 @@ impl LocalImage { .get_result::(conn) .await; - ImageDetails::create(conn, image_details_form).await?; + ImageDetails::create(&mut conn.into(), image_details_form).await?; local_insert }) as _ @@ -60,26 +61,16 @@ impl LocalImage { } impl RemoteImage { - pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result { + pub async fn create(pool: &mut DbPool<'_>, links: Vec) -> Result { let conn = &mut get_conn(pool).await?; - conn - .build_transaction() - .run(|conn| { - Box::pin(async move { - let remote_image_form = RemoteImageForm { - link: form.link.clone(), - }; - let remote_insert = insert_into(remote_image::table) - .values(remote_image_form) - .on_conflict_do_nothing() - .execute(conn) - .await; - - ImageDetails::create(conn, form).await?; - - remote_insert - }) as _ - }) + let forms = links + .into_iter() + .map(|url| RemoteImageForm { link: url.into() }) + .collect::>(); + insert_into(remote_image::table) + .values(forms) + .on_conflict_do_nothing() + .execute(conn) .await } @@ -100,10 +91,9 @@ impl RemoteImage { } impl ImageDetails { - pub(crate) async fn create( - conn: &mut AsyncPgConnection, - form: &ImageDetailsForm, - ) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result { + let conn = &mut get_conn(pool).await?; + insert_into(image_details::table) .values(form) .on_conflict_do_nothing()