From 00f7778485168039d038e2596323b833f229d33b Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 8 Mar 2024 16:17:26 +0100 Subject: [PATCH] Store thumbnails in db table local_image (#4512) * Store thumbnails in db table local_image * fmt --- crates/api_common/src/request.rs | 14 ++++++++++++-- crates/db_schema/src/schema.rs | 2 +- crates/db_schema/src/source/images.rs | 7 ++----- crates/routes/src/images.rs | 2 +- .../down.sql | 3 +++ .../2024-03-06-104706_local_image_user_opt/up.sql | 3 +++ 6 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 migrations/2024-03-06-104706_local_image_user_opt/down.sql create mode 100644 migrations/2024-03-06-104706_local_image_user_opt/up.sql diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index d3e307da5..aaae7f866 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -4,7 +4,10 @@ use crate::{ utils::proxy_image_link, }; use encoding::{all::encodings, DecoderTrap}; -use lemmy_db_schema::newtypes::DbUrl; +use lemmy_db_schema::{ + newtypes::DbUrl, + source::images::{LocalImage, LocalImageForm}, +}; use lemmy_utils::{ error::{LemmyError, LemmyErrorType}, settings::structs::{PictrsImageMode, Settings}, @@ -184,7 +187,6 @@ struct PictrsResponse { #[derive(Deserialize, Debug)] struct PictrsFile { file: String, - #[allow(dead_code)] delete_token: String, } @@ -287,6 +289,14 @@ async fn generate_pictrs_thumbnail( context.settings().get_protocol_and_hostname(), response.files.first().expect("missing pictrs file").file ))?; + for uploaded_image in response.files { + let form = LocalImageForm { + local_user_id: None, + pictrs_alias: uploaded_image.file.to_string(), + pictrs_delete_token: uploaded_image.delete_token.to_string(), + }; + LocalImage::create(&mut context.pool(), &form).await?; + } Ok(thumbnail_url) } else { Err(LemmyErrorType::PictrsResponseError(response.msg))? diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 739ff8482..632f7d0bb 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -342,7 +342,7 @@ diesel::table! { diesel::table! { local_image (pictrs_alias) { - local_user_id -> Int4, + local_user_id -> Nullable, pictrs_alias -> Text, pictrs_delete_token -> Text, published -> Timestamptz, diff --git a/crates/db_schema/src/source/images.rs b/crates/db_schema/src/source/images.rs index f8befb856..3bf2a5bb8 100644 --- a/crates/db_schema/src/source/images.rs +++ b/crates/db_schema/src/source/images.rs @@ -2,11 +2,9 @@ use crate::newtypes::{DbUrl, LocalUserId}; #[cfg(feature = "full")] use crate::schema::{local_image, remote_image}; use chrono::{DateTime, Utc}; -use serde_with::skip_serializing_none; use std::fmt::Debug; use typed_builder::TypedBuilder; -#[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone)] #[cfg_attr(feature = "full", derive(Queryable, Associations))] #[cfg_attr(feature = "full", diesel(table_name = local_image))] @@ -15,7 +13,7 @@ use typed_builder::TypedBuilder; diesel(belongs_to(crate::source::local_user::LocalUser)) )] pub struct LocalImage { - pub local_user_id: LocalUserId, + pub local_user_id: Option, pub pictrs_alias: String, pub pictrs_delete_token: String, pub published: DateTime, @@ -25,14 +23,13 @@ pub struct LocalImage { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = local_image))] pub struct LocalImageForm { - pub local_user_id: LocalUserId, + pub local_user_id: Option, pub pictrs_alias: String, pub pictrs_delete_token: String, } /// Stores all images which are hosted on remote domains. When attempting to proxy an image, it /// is checked against this table to avoid Lemmy being used as a general purpose proxy. -#[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone)] #[cfg_attr(feature = "full", derive(Queryable, Identifiable))] #[cfg_attr(feature = "full", diesel(table_name = remote_image))] diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index f40b3c10c..58ca9c8ef 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -114,7 +114,7 @@ async fn upload( if let Some(images) = &images.files { for uploaded_image in images { let form = LocalImageForm { - local_user_id: local_user_view.local_user.id, + local_user_id: Some(local_user_view.local_user.id), pictrs_alias: uploaded_image.file.to_string(), pictrs_delete_token: uploaded_image.delete_token.to_string(), }; diff --git a/migrations/2024-03-06-104706_local_image_user_opt/down.sql b/migrations/2024-03-06-104706_local_image_user_opt/down.sql new file mode 100644 index 000000000..45d890467 --- /dev/null +++ b/migrations/2024-03-06-104706_local_image_user_opt/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_image + ALTER COLUMN local_user_id SET NOT NULL; + diff --git a/migrations/2024-03-06-104706_local_image_user_opt/up.sql b/migrations/2024-03-06-104706_local_image_user_opt/up.sql new file mode 100644 index 000000000..b80098161 --- /dev/null +++ b/migrations/2024-03-06-104706_local_image_user_opt/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE local_image + ALTER COLUMN local_user_id DROP NOT NULL; +