Store thumbnails in db table local_image

This commit is contained in:
Felix Ableitner 2024-03-06 11:51:30 +01:00
parent 7eec8714d7
commit 10164c52d9
6 changed files with 18 additions and 9 deletions

View file

@ -4,7 +4,10 @@ use crate::{
utils::proxy_image_link, utils::proxy_image_link,
}; };
use encoding::{all::encodings, DecoderTrap}; use encoding::{all::encodings, DecoderTrap};
use lemmy_db_schema::newtypes::DbUrl; use lemmy_db_schema::{
newtypes::DbUrl,
source::images::{LocalImage, LocalImageForm},
};
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType}, error::{LemmyError, LemmyErrorType},
settings::structs::{PictrsImageMode, Settings}, settings::structs::{PictrsImageMode, Settings},
@ -184,7 +187,6 @@ struct PictrsResponse {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct PictrsFile { struct PictrsFile {
file: String, file: String,
#[allow(dead_code)]
delete_token: String, delete_token: String,
} }
@ -287,6 +289,14 @@ async fn generate_pictrs_thumbnail(
context.settings().get_protocol_and_hostname(), context.settings().get_protocol_and_hostname(),
response.files.first().expect("missing pictrs file").file 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) Ok(thumbnail_url)
} else { } else {
Err(LemmyErrorType::PictrsResponseError(response.msg))? Err(LemmyErrorType::PictrsResponseError(response.msg))?

View file

@ -342,7 +342,7 @@ diesel::table! {
diesel::table! { diesel::table! {
local_image (pictrs_alias) { local_image (pictrs_alias) {
local_user_id -> Int4, local_user_id -> Nullable<Int4>,
pictrs_alias -> Text, pictrs_alias -> Text,
pictrs_delete_token -> Text, pictrs_delete_token -> Text,
published -> Timestamptz, published -> Timestamptz,

View file

@ -2,11 +2,9 @@ use crate::newtypes::{DbUrl, LocalUserId};
#[cfg(feature = "full")] #[cfg(feature = "full")]
use crate::schema::{local_image, remote_image}; use crate::schema::{local_image, remote_image};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde_with::skip_serializing_none;
use std::fmt::Debug; use std::fmt::Debug;
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
#[skip_serializing_none]
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Debug, Clone)]
#[cfg_attr(feature = "full", derive(Queryable, Associations))] #[cfg_attr(feature = "full", derive(Queryable, Associations))]
#[cfg_attr(feature = "full", diesel(table_name = local_image))] #[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)) diesel(belongs_to(crate::source::local_user::LocalUser))
)] )]
pub struct LocalImage { pub struct LocalImage {
pub local_user_id: LocalUserId, pub local_user_id: Option<LocalUserId>,
pub pictrs_alias: String, pub pictrs_alias: String,
pub pictrs_delete_token: String, pub pictrs_delete_token: String,
pub published: DateTime<Utc>, pub published: DateTime<Utc>,
@ -25,14 +23,13 @@ pub struct LocalImage {
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = local_image))] #[cfg_attr(feature = "full", diesel(table_name = local_image))]
pub struct LocalImageForm { pub struct LocalImageForm {
pub local_user_id: LocalUserId, pub local_user_id: Option<LocalUserId>,
pub pictrs_alias: String, pub pictrs_alias: String,
pub pictrs_delete_token: String, pub pictrs_delete_token: String,
} }
/// Stores all images which are hosted on remote domains. When attempting to proxy an image, it /// 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. /// is checked against this table to avoid Lemmy being used as a general purpose proxy.
#[skip_serializing_none]
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Debug, Clone)]
#[cfg_attr(feature = "full", derive(Queryable, Identifiable))] #[cfg_attr(feature = "full", derive(Queryable, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = remote_image))] #[cfg_attr(feature = "full", diesel(table_name = remote_image))]

View file

@ -114,7 +114,7 @@ async fn upload(
if let Some(images) = &images.files { if let Some(images) = &images.files {
for uploaded_image in images { for uploaded_image in images {
let form = LocalImageForm { 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_alias: uploaded_image.file.to_string(),
pictrs_delete_token: uploaded_image.delete_token.to_string(), pictrs_delete_token: uploaded_image.delete_token.to_string(),
}; };

View file

@ -0,0 +1 @@
ALTER TABLE local_image ALTER COLUMN local_user_id SET NOT NULL;

View file

@ -0,0 +1 @@
ALTER TABLE local_image ALTER COLUMN local_user_id DROP NOT NULL;