Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always save remote image data #4875

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1012,6 +1012,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 {
Expand All @@ -1021,7 +1022,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)
Expand Down Expand Up @@ -1057,13 +1058,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())
Expand Down Expand Up @@ -1209,7 +1212,7 @@ mod tests {
assert!(
RemoteImage::validate(&mut context.pool(), remote_image.into())
.await
.is_err()
.is_ok()
);
}
}
49 changes: 16 additions & 33 deletions crates/db_schema/src/impls/images.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use crate::{
newtypes::DbUrl,
schema::{image_details, local_image, remote_image},
source::images::{
ImageDetails,
ImageDetailsForm,
LocalImage,
LocalImageForm,
RemoteImage,
RemoteImageForm,
},
source::images::{ImageDetails, ImageDetailsForm, LocalImage, LocalImageForm, RemoteImage},
utils::{get_conn, DbPool},
};
use diesel::{
Expand All @@ -20,7 +13,8 @@ use diesel::{
NotFound,
QueryDsl,
};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel_async::RunQueryDsl;
use url::Url;

impl LocalImage {
pub async fn create(
Expand All @@ -38,7 +32,7 @@ impl LocalImage {
.get_result::<Self>(conn)
.await;

ImageDetails::create(conn, image_details_form).await?;
ImageDetails::create(&mut conn.into(), image_details_form).await?;

local_insert
}) as _
Expand All @@ -60,26 +54,16 @@ impl LocalImage {
}

impl RemoteImage {
pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result<usize, Error> {
pub async fn create(pool: &mut DbPool<'_>, links: Vec<Url>) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
let remote_image_form = RemoteImageForm {
Nutomic marked this conversation as resolved.
Show resolved Hide resolved
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| remote_image::dsl::link.eq::<DbUrl>(url.into()))
.collect::<Vec<_>>();
insert_into(remote_image::table)
.values(forms)
.on_conflict_do_nothing()
.execute(conn)
.await
}

Expand All @@ -100,10 +84,9 @@ impl RemoteImage {
}

impl ImageDetails {
pub(crate) async fn create(
conn: &mut AsyncPgConnection,
form: &ImageDetailsForm,
) -> Result<usize, Error> {
pub async fn create(pool: &mut DbPool<'_>, form: &ImageDetailsForm) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;

insert_into(image_details::table)
.values(form)
.on_conflict_do_nothing()
Expand Down
7 changes: 0 additions & 7 deletions crates/db_schema/src/source/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ pub struct RemoteImage {
pub published: DateTime<Utc>,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = remote_image))]
pub struct RemoteImageForm {
pub link: DbUrl,
}

#[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
Expand Down