Change thumbnail response to include more information

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 216e5892f9
commit d3ba1152ac

@ -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<u8>, Mime)> {
pub async fn create_thumbnail(
&self,
size: ThumbnailSize,
) -> RepoResult<(Vec<u8>, 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<u8>, Mime)> {
async fn create_image_thumbnail(
&self,
size: ThumbnailSize,
) -> RepoResult<(Vec<u8>, 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

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

@ -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<String> = thumbnails
let thumb_responses: Vec<ThumbnailResponse> = 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(())

@ -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<File> for FileResponse {
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ThumbnailResponse {
hash: String,
height: i32,
width: i32,
mime: Option<String>,
}
impl From<Thumbnail> 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,

Loading…
Cancel
Save