From f82bd68b44d344095c3e15cee6edc3462634b3e1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Nov 2021 16:16:40 +0100 Subject: [PATCH] Reintroduce command to get the contents of a file Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 23 ++++++++++++++++++- .../src/tauri_plugin/commands/mod.rs | 3 ++- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 799701b..98753bd 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,4 +1,4 @@ -use crate::tauri_plugin::commands::ApiAccess; +use crate::tauri_plugin::commands::{ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ @@ -99,6 +99,27 @@ pub async fn update_file_name( Ok(metadata) } +#[tauri::command] +pub async fn read_file( + api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, + hash: String, + mime_type: String, +) -> PluginResult> { + if let Some(buffer) = buffer_state.get_entry(&hash) { + Ok(buffer.buf) + } else { + let api = api_state.api().await?; + let content = api + .file + .read_file(FileIdentifier::Hash(hash.clone())) + .await?; + buffer_state.add_entry(hash, mime_type, content.clone()); + + Ok(content) + } +} + /// Saves a file on the local system #[tauri::command] pub async fn save_file_locally( diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index 106a4d2..d90e9ff 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -5,7 +5,7 @@ pub use file::*; pub use repo::*; pub use tag::*; -use crate::tauri_plugin::state::{ApiState, AppState}; +use crate::tauri_plugin::state::{ApiState, AppState, BufferState}; pub mod daemon; pub mod file; @@ -14,3 +14,4 @@ pub mod tag; pub type ApiAccess<'a> = State<'a, ApiState>; pub type AppAccess<'a> = State<'a, AppState>; +pub type BufferAccess<'a> = State<'a, BufferState>; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 1d5915b..97bc43b 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -54,7 +54,8 @@ impl MediarepoPlugin { resolve_paths_to_files, add_local_file, save_file_locally, - delete_thumbnails + delete_thumbnails, + read_file ]), } }