Change thumbnail response to not include the thumb id

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 5d510cbf15
commit abf28402e6

@ -1,6 +1,6 @@
[package] [package]
name = "mediarepo-api" name = "mediarepo-api"
version = "0.7.0" version = "0.8.0"
edition = "2018" edition = "2018"
license = "gpl-3" license = "gpl-3"

@ -97,13 +97,6 @@ where
.await .await
} }
/// Reads the thumbnail of the file and returns its contents in bytes
#[tracing::instrument(level = "debug", skip(self))]
pub async fn read_thumbnail(&self, hash: String) -> ApiResult<Vec<u8>> {
let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?;
Ok(payload.into_inner())
}
/// Returns a thumbnail of size that is within the specified range /// Returns a thumbnail of size that is within the specified range
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn get_thumbnail_of_size( pub async fn get_thumbnail_of_size(

@ -1,4 +1,4 @@
use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::commands::ApiAccess;
use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::error::PluginResult;
use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::tauri_plugin::utils::system_time_to_naive_date_time;
use crate::types::files::{ use crate::types::files::{
@ -66,25 +66,6 @@ pub async fn find_files(
Ok(files) Ok(files)
} }
#[tauri::command]
pub async fn read_file_by_hash(
api_state: ApiAccess<'_>,
buffer_state: BufferAccess<'_>,
id: i64,
hash: String,
mime_type: String,
) -> PluginResult<String> {
if buffer_state.reserve_entry(&hash) {
Ok(format!("once://{}", hash)) // entry has been cached
} else {
let api = api_state.api().await?;
let content = api.file.read_file_by_hash(FileIdentifier::ID(id)).await?;
let uri = add_once_buffer(buffer_state, hash, mime_type, content);
Ok(uri)
}
}
#[tauri::command] #[tauri::command]
pub async fn get_file_thumbnails( pub async fn get_file_thumbnails(
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,
@ -96,47 +77,6 @@ pub async fn get_file_thumbnails(
Ok(thumbs) Ok(thumbs)
} }
#[tauri::command]
pub async fn read_thumbnail(
hash: String,
mime_type: String,
api_state: ApiAccess<'_>,
buffer_state: BufferAccess<'_>,
) -> PluginResult<String> {
if buffer_state.reserve_entry(&hash) {
Ok(format!("once://{}", hash)) // entry has been cached
} else {
let api = api_state.api().await?;
let content = api.file.read_thumbnail(hash.clone()).await?;
let uri = add_once_buffer(buffer_state, hash, mime_type, content);
Ok(uri)
}
}
#[tauri::command]
pub async fn get_thumbnail_of_size(
api_state: ApiAccess<'_>,
buffer_state: BufferAccess<'_>,
file_id: i64,
min_size: (u32, u32),
max_size: (u32, u32),
) -> PluginResult<String> {
let api = api_state.api().await?;
let (thumb, data) = api
.file
.get_thumbnail_of_size(FileIdentifier::ID(file_id), min_size, max_size)
.await?;
let uri = add_once_buffer(
buffer_state,
thumb.hash,
thumb.mime_type.unwrap_or(String::from("image/png")),
data,
);
Ok(uri)
}
#[tauri::command] #[tauri::command]
pub async fn update_file_name( pub async fn update_file_name(
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,

@ -1,4 +1,3 @@
use parking_lot::lock_api::Mutex;
use tauri::State; use tauri::State;
pub use daemon::*; pub use daemon::*;
@ -6,7 +5,7 @@ pub use file::*;
pub use repo::*; pub use repo::*;
pub use tag::*; pub use tag::*;
use crate::tauri_plugin::state::{ApiState, AppState, BufferState, VolatileBuffer}; use crate::tauri_plugin::state::{ApiState, AppState};
pub mod daemon; pub mod daemon;
pub mod file; pub mod file;
@ -14,20 +13,4 @@ pub mod repo;
pub mod tag; pub mod tag;
pub type ApiAccess<'a> = State<'a, ApiState>; pub type ApiAccess<'a> = State<'a, ApiState>;
pub type BufferAccess<'a> = State<'a, BufferState>;
pub type AppAccess<'a> = State<'a, AppState>; pub type AppAccess<'a> = State<'a, AppState>;
/// Adds a once-buffer to the buffer store
fn add_once_buffer(
buffer_state: BufferAccess<'_>,
key: String,
mime: String,
buf: Vec<u8>,
) -> String {
let uri = format!("once://{}", key);
let once_buffer = VolatileBuffer::new(mime, buf);
let mut once_buffers = buffer_state.buffer.write();
once_buffers.insert(key, Mutex::new(once_buffer));
uri
}

@ -99,6 +99,7 @@ fn thumb_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Result<Res
))?; ))?;
let mime = thumb.mime_type.unwrap_or(String::from("image/png")); let mime = thumb.mime_type.unwrap_or(String::from("image/png"));
buf_state.add_entry(request.uri().to_string(), mime.clone(), bytes.clone()); buf_state.add_entry(request.uri().to_string(), mime.clone(), bytes.clone());
ResponseBuilder::new() ResponseBuilder::new()
.mimetype(&mime) .mimetype(&mime)
.status(200) .status(200)

@ -32,10 +32,7 @@ impl<R: Runtime> MediarepoPlugin<R> {
invoke_handler: Box::new(tauri::generate_handler![ invoke_handler: Box::new(tauri::generate_handler![
get_all_files, get_all_files,
find_files, find_files,
read_file_by_hash,
get_file_thumbnails, get_file_thumbnails,
read_thumbnail,
get_thumbnail_of_size,
get_repositories, get_repositories,
get_all_tags, get_all_tags,
get_tags_for_file, get_tags_for_file,

@ -90,21 +90,6 @@ impl BufferState {
buffers.insert(key, Mutex::new(buffer)); buffers.insert(key, Mutex::new(buffer));
} }
/// Checks if an entry for the specific key exists and resets
/// its state so that it can safely be accessed again.
pub fn reserve_entry(&self, key: &String) -> bool {
let buffers = self.buffer.read();
let entry = buffers.get(key);
if let Some(entry) = entry {
let mut entry = entry.lock();
entry.valid_until = Instant::now() + Duration::from_secs(120); // reset the timer so that it can be accessed again
true
} else {
false
}
}
/// Returns the cloned buffer entry and flags it for expiration /// Returns the cloned buffer entry and flags it for expiration
pub fn get_entry(&self, key: &str) -> Option<VolatileBuffer> { pub fn get_entry(&self, key: &str) -> Option<VolatileBuffer> {
let buffers = self.buffer.read(); let buffers = self.buffer.read();

@ -91,9 +91,7 @@ pub struct FileOSMetadata {
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ThumbnailMetadataResponse { pub struct ThumbnailMetadataResponse {
pub id: i64,
pub file_id: i64, pub file_id: i64,
pub hash: String,
pub height: i32, pub height: i32,
pub width: i32, pub width: i32,
pub mime_type: Option<String>, pub mime_type: Option<String>,

Loading…
Cancel
Save