From 32492afedf478e1c9b21b12c7e604cadfc8617f9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 15:56:47 +0100 Subject: [PATCH] Add api to request thumbnails of specific sizes Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 30 +++++++++++++++++++++++++--- mediarepo-api/src/client_api/mod.rs | 5 +---- mediarepo-api/src/types/files.rs | 7 ++----- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 42c9029..0ab3d9d 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.4.2" +version = "0.5.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 51904be..0e361a6 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -1,8 +1,9 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ - FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, - SortKey, TagQuery, ThumbnailMetadataResponse, UpdateFileNameRequest, + FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailOfSizeRequest, + GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, ThumbnailFullResponse, + ThumbnailMetadataResponse, UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -95,7 +96,30 @@ where #[tracing::instrument(level = "debug", skip(self))] pub async fn read_thumbnail(&self, hash: String) -> ApiResult> { let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?; - Ok(payload.to_payload_bytes()?) + Ok(payload.into_inner()) + } + + /// Returns a thumbnail of size that is within the specified range + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_thumbnail_of_size( + &self, + file_id: FileIdentifier, + min_size: (u32, u32), + max_size: (u32, u32), + ) -> ApiResult<(ThumbnailMetadataResponse, Vec)> { + let payload: ThumbnailFullResponse = self + .emit_and_get( + "get_thumbnail_of_size", + GetFileThumbnailOfSizeRequest { + id: file_id, + min_size, + max_size, + }, + ) + .await?; + let (metadata, bytes) = payload.into_inner(); + + Ok((metadata, bytes.into_inner())) } /// Updates a files name diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 219acca..36e4f66 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -35,10 +35,7 @@ pub trait IPCApi { Ok(meta) } - async fn emit_and_get< - T: EventSendPayload + Debug + Send, - R: EventReceivePayload + Debug + Send, - >( + async fn emit_and_get( &self, event_name: &str, data: T, diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index c812672..2bcf8f8 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,5 +1,6 @@ use crate::types::identifier::FileIdentifier; use chrono::NaiveDateTime; +use rmp_ipc::payload::{BytePayload, TandemPayload}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] @@ -101,8 +102,4 @@ pub struct UpdateFileNameRequest { pub name: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ThumbnailFullResponse { - pub metadata: ThumbnailMetadataResponse, - pub data: Vec, -} +pub type ThumbnailFullResponse = TandemPayload;