From d3ba1152ac8d33a8c6230a558a8114262ca33862 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 16 Oct 2021 10:26:32 +0200 Subject: [PATCH] Change thumbnail response to include more information Signed-off-by: trivernis --- mediarepo-daemon/mediarepo-model/src/file.rs | 14 ++++++++++--- mediarepo-daemon/mediarepo-model/src/repo.rs | 3 +-- .../mediarepo-socket/src/namespaces/files.rs | 13 ++++++++---- .../mediarepo-socket/src/types/responses.rs | 20 +++++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/mediarepo-daemon/mediarepo-model/src/file.rs b/mediarepo-daemon/mediarepo-model/src/file.rs index 42844f2..41e74d7 100644 --- a/mediarepo-daemon/mediarepo-model/src/file.rs +++ b/mediarepo-daemon/mediarepo-model/src/file.rs @@ -3,6 +3,7 @@ use crate::storage::Storage; use crate::thumbnail::Thumbnail; use chrono::{Local, NaiveDateTime}; use mediarepo_core::error::{RepoError, RepoResult}; +use mediarepo_core::image::GenericImageView; use mediarepo_core::image_processing::{ create_thumbnail, get_image_bytes_png, read_image, ThumbnailSize, }; @@ -205,7 +206,10 @@ impl File { } /// Creates a thumbnail for the file - pub async fn create_thumbnail(&self, size: ThumbnailSize) -> RepoResult<(Vec, Mime)> { + pub async fn create_thumbnail( + &self, + size: ThumbnailSize, + ) -> RepoResult<(Vec, Mime, (u32, u32))> { match self.file_type() { FileType::Image => self.create_image_thumbnail(size).await, _ => Err(RepoError::from( @@ -215,13 +219,17 @@ impl File { } /// Creates a thumbnail for an image - async fn create_image_thumbnail(&self, size: ThumbnailSize) -> RepoResult<(Vec, Mime)> { + async fn create_image_thumbnail( + &self, + size: ThumbnailSize, + ) -> RepoResult<(Vec, Mime, (u32, u32))> { let mut reader = self.get_reader().await?; let image = read_image(&mut reader).await?; let thumb_image = create_thumbnail(image, size); + let actual_size = (thumb_image.height(), thumb_image.width()); let bytes = get_image_bytes_png(thumb_image)?; - Ok((bytes, mime::IMAGE_PNG)) + Ok((bytes, mime::IMAGE_PNG, actual_size)) } /// Returns the active model of the file with only the id set diff --git a/mediarepo-daemon/mediarepo-model/src/repo.rs b/mediarepo-daemon/mediarepo-model/src/repo.rs index 441d5d0..2b8a36f 100644 --- a/mediarepo-daemon/mediarepo-model/src/repo.rs +++ b/mediarepo-daemon/mediarepo-model/src/repo.rs @@ -115,9 +115,8 @@ impl Repo { ThumbnailSize::Medium, ThumbnailSize::Large, ] { - let (bytes, mime) = file.create_thumbnail(size).await?; + let (bytes, mime, (height, width)) = file.create_thumbnail(size).await?; let hash = thumb_storage.store_entry(Cursor::new(bytes)).await?; - let (height, width) = size.dimensions(); Thumbnail::add( self.db.clone(), hash.id(), diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs index b8a945f..6fccea4 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs @@ -1,7 +1,7 @@ use crate::types::requests::{ AddFileRequest, FileIdentifier, GetFileThumbnailsRequest, ReadFileRequest, }; -use crate::types::responses::FileResponse; +use crate::types::responses::{FileResponse, ThumbnailResponse}; use mediarepo_core::error::{RepoError, RepoResult}; use mediarepo_model::file::File; use mediarepo_model::repo::Repo; @@ -88,12 +88,17 @@ async fn get_file_thumbnails(ctx: &Context, event: Event) -> Result<()> { .await? .ok_or_else(|| RepoError::from("File not found"))?; let thumbnails = file.thumbnails().await?; - let thumb_hashes: Vec = thumbnails + let thumb_responses: Vec = thumbnails .into_iter() - .map(|thumb| thumb.hash().clone()) + .map(ThumbnailResponse::from) .collect(); ctx.emitter - .emit_response_to(event.id(), FILES_NAMESPACE, "get_thumbnails", thumb_hashes) + .emit_response_to( + event.id(), + FILES_NAMESPACE, + "get_thumbnails", + thumb_responses, + ) .await?; Ok(()) diff --git a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs index f84fa13..cef433c 100644 --- a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs +++ b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs @@ -1,6 +1,7 @@ use chrono::NaiveDateTime; use mediarepo_model::file::File; use mediarepo_model::file_type::FileType; +use mediarepo_model::thumbnail::Thumbnail; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize)] @@ -30,6 +31,25 @@ impl From for FileResponse { } } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ThumbnailResponse { + hash: String, + height: i32, + width: i32, + mime: Option, +} + +impl From for ThumbnailResponse { + fn from(thumb: Thumbnail) -> Self { + Self { + hash: thumb.hash().to_owned(), + height: thumb.height(), + width: thumb.width(), + mime: thumb.mime_type().to_owned(), + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InfoResponse { pub name: String,