Reintroduce command to get the contents of a file

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

@ -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::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::{
@ -99,6 +99,27 @@ pub async fn update_file_name(
Ok(metadata) Ok(metadata)
} }
#[tauri::command]
pub async fn read_file(
api_state: ApiAccess<'_>,
buffer_state: BufferAccess<'_>,
hash: String,
mime_type: String,
) -> PluginResult<Vec<u8>> {
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 /// Saves a file on the local system
#[tauri::command] #[tauri::command]
pub async fn save_file_locally( pub async fn save_file_locally(

@ -5,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}; use crate::tauri_plugin::state::{ApiState, AppState, BufferState};
pub mod daemon; pub mod daemon;
pub mod file; pub mod file;
@ -14,3 +14,4 @@ pub mod tag;
pub type ApiAccess<'a> = State<'a, ApiState>; pub type ApiAccess<'a> = State<'a, ApiState>;
pub type AppAccess<'a> = State<'a, AppState>; pub type AppAccess<'a> = State<'a, AppState>;
pub type BufferAccess<'a> = State<'a, BufferState>;

@ -54,7 +54,8 @@ impl<R: Runtime> MediarepoPlugin<R> {
resolve_paths_to_files, resolve_paths_to_files,
add_local_file, add_local_file,
save_file_locally, save_file_locally,
delete_thumbnails delete_thumbnails,
read_file
]), ]),
} }
} }

Loading…
Cancel
Save