Store thumbnails in db table local_image (#4512)

* Store thumbnails in db table local_image

* fmt
This commit is contained in:
Nutomic 2024-03-08 16:17:26 +01:00 committed by GitHub
parent bc2e75d5a3
commit 00f7778485
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 22 additions and 9 deletions

View file

@ -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))?

View file

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

View file

@ -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<LocalUserId>,
pub pictrs_alias: String,
pub pictrs_delete_token: String,
pub published: DateTime<Utc>,
@ -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<LocalUserId>,
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))]

View file

@ -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(),
};

View file

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

View file

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