Add plugin command to save files on the local system

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 0534e543ac
commit 9411cbd1ab

@ -79,7 +79,7 @@ where
/// Reads the file and returns its contents as bytes
#[tracing::instrument(level = "debug", skip(self))]
pub async fn read_file_by_hash(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
pub async fn read_file(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
let payload: BytePayload = self
.emit_and_get("read_file", ReadFileRequest { id })
.await?;

@ -99,6 +99,20 @@ pub async fn update_file_name(
Ok(metadata)
}
/// Saves a file on the local system
#[tauri::command]
pub async fn save_file_locally(
api_state: ApiAccess<'_>,
id: i64,
path: String,
) -> PluginResult<()> {
let api = api_state.api().await?;
let content = api.file.read_file(FileIdentifier::ID(id)).await?;
fs::write(PathBuf::from(path), content).await?;
Ok(())
}
#[tauri::command]
pub async fn resolve_paths_to_files(paths: Vec<String>) -> PluginResult<Vec<FileOSMetadata>> {
let mut files = Vec::new();

@ -70,7 +70,7 @@ async fn content_scheme<R: Runtime>(app: &AppHandle<R>, request: &Request) -> Re
let mime = file.mime_type.unwrap_or("image/png".to_string());
let bytes = api
.file
.read_file_by_hash(FileIdentifier::Hash(hash.to_string()))
.read_file(FileIdentifier::Hash(hash.to_string()))
.await?;
buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone());
ResponseBuilder::new()

@ -52,7 +52,8 @@ impl<R: Runtime> MediarepoPlugin<R> {
create_tags,
update_file_name,
resolve_paths_to_files,
add_local_file
add_local_file,
save_file_locally
]),
}
}

Loading…
Cancel
Save