Skip to content

Commit

Permalink
Adding blurhash to image_details.
Browse files Browse the repository at this point in the history
- Fixes #5142
  • Loading branch information
dessalines committed Nov 25, 2024
1 parent ba3d574 commit c4a88e5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
9 changes: 6 additions & 3 deletions crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use futures::StreamExt;
use lemmy_db_schema::{
newtypes::DbUrl,
source::{
images::{ImageDetailsForm, LocalImage, LocalImageForm},
images::{ImageDetailsInsertForm, LocalImage, LocalImageForm},
post::{Post, PostUpdateForm},
site::Site,
},
Expand Down Expand Up @@ -271,17 +271,20 @@ pub struct PictrsFileDetails {
pub height: u16,
pub content_type: String,
pub created_at: DateTime<Utc>,
// TODO this can get changed to String on future versions of pictrs
pub blurhash: Option<String>,
}

impl PictrsFileDetails {
/// Builds the image form. This should always use the thumbnail_url,
/// Because the post_view joins to it
pub fn build_image_details_form(&self, thumbnail_url: &Url) -> ImageDetailsForm {
ImageDetailsForm {
pub fn build_image_details_form(&self, thumbnail_url: &Url) -> ImageDetailsInsertForm {
ImageDetailsInsertForm {
link: thumbnail_url.clone().into(),
width: self.width.into(),
height: self.height.into(),
content_type: self.content_type.clone(),
blurhash: self.blurhash.clone(),
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/db_schema/src/impls/images.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
newtypes::DbUrl,
schema::{image_details, local_image, remote_image},
source::images::{ImageDetails, ImageDetailsForm, LocalImage, LocalImageForm, RemoteImage},
source::images::{ImageDetails, ImageDetailsInsertForm, LocalImage, LocalImageForm, RemoteImage},
utils::{get_conn, DbPool},
};
use diesel::{
Expand All @@ -20,7 +20,7 @@ impl LocalImage {
pub async fn create(
pool: &mut DbPool<'_>,
form: &LocalImageForm,
image_details_form: &ImageDetailsForm,
image_details_form: &ImageDetailsInsertForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
conn
Expand Down Expand Up @@ -84,7 +84,10 @@ impl RemoteImage {
}

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

insert_into(image_details::table)
Expand Down
2 changes: 2 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ diesel::table! {
width -> Int4,
height -> Int4,
content_type -> Text,
#[max_length = 50]
blurhash -> Nullable<Varchar>,
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/db_schema/src/source/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ pub struct ImageDetails {
pub width: i32,
pub height: i32,
pub content_type: String,
pub blurhash: Option<String>,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = image_details))]
pub struct ImageDetailsForm {
pub struct ImageDetailsInsertForm {
pub link: DbUrl,
pub width: i32,
pub height: i32,
pub content_type: String,
pub blurhash: Option<String>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE image_details
DROP COLUMN blurhash;

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Add a blurhash column for image_details
ALTER TABLE image_details
-- Supposed to be 20-30 chars, use 50 to be safe
-- TODO this should be made not null for future versions of pictrs
ADD COLUMN blurhash varchar(50);

0 comments on commit c4a88e5

Please sign in to comment.