Add api to add files

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

@ -1,6 +1,6 @@
[package]
name = "mediarepo-api"
version = "0.5.3"
version = "0.6.0"
edition = "2018"
license = "gpl-3"
@ -10,7 +10,7 @@ license = "gpl-3"
tracing = "0.1.29"
thiserror = "1.0.30"
async-trait = {version = "0.1.51", optional=true}
rmp-ipc = {version = "0.9.0", optional=true}
rmp-ipc = {version = "0.9.2", optional=true}
parking_lot = {version="0.11.2", optional=true}
serde_json = {version="1.0.68", optional=true}
directories = {version="4.0.1", optional=true}

@ -1,9 +1,9 @@
use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi;
use crate::types::files::{
FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailOfSizeRequest,
GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, ThumbnailMetadataResponse,
UpdateFileNameRequest,
AddFileRequestHeader, FileMetadataResponse, FileOSMetadata, FindFilesByTagsRequest,
GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery,
ThumbnailMetadataResponse, UpdateFileNameRequest,
};
use crate::types::identifier::FileIdentifier;
use async_trait::async_trait;
@ -132,4 +132,20 @@ where
self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name })
.await
}
/// Adds a file with predefined tags
#[tracing::instrument(level = "debug", skip(self, bytes))]
pub async fn add_file(
&self,
metadata: FileOSMetadata,
tags: Vec<String>,
bytes: Vec<u8>,
) -> ApiResult<FileMetadataResponse> {
let payload = TandemPayload::new(
AddFileRequestHeader { metadata, tags },
BytePayload::new(bytes),
);
self.emit_and_get("add_file", payload).await
}
}

@ -14,14 +14,13 @@ use rmp_ipc::ipc::stream_emitter::EmitMetadata;
use rmp_ipc::payload::{EventReceivePayload, EventSendPayload};
use rmp_ipc::prelude::{AsyncProtocolStream, AsyncStreamProtocolListener};
use rmp_ipc::IPCBuilder;
use std::fmt::Debug;
#[async_trait]
pub trait IPCApi<S: AsyncProtocolStream> {
fn namespace() -> &'static str;
fn ctx(&self) -> PoolGuard<Context<S>>;
async fn emit<T: EventSendPayload + Debug + Send>(
async fn emit<T: EventSendPayload + Send>(
&self,
event_name: &str,
data: T,
@ -35,7 +34,7 @@ pub trait IPCApi<S: AsyncProtocolStream> {
Ok(meta)
}
async fn emit_and_get<T: EventSendPayload + Debug + Send, R: EventReceivePayload + Send>(
async fn emit_and_get<T: EventSendPayload + Send, R: EventReceivePayload + Send>(
&self,
event_name: &str,
data: T,

@ -5,10 +5,17 @@ use crate::types::files::{
FileMetadataResponse, FileOSMetadata, SortKey, TagQuery, ThumbnailMetadataResponse,
};
use crate::types::identifier::FileIdentifier;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio::fs;
use tokio::fs::DirEntry;
#[derive(Serialize, Deserialize, Debug)]
pub struct AddFileOptions {
pub read_tags_from_txt: bool,
pub delete_after_import: bool,
}
#[tauri::command]
pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMetadataResponse>> {
let api = api_state.api().await?;
@ -17,6 +24,36 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMet
Ok(all_files)
}
#[tauri::command]
pub async fn add_local_file(
api_state: ApiAccess<'_>,
metadata: FileOSMetadata,
options: AddFileOptions,
) -> PluginResult<FileMetadataResponse> {
let api = api_state.api().await?;
let path = PathBuf::from(&metadata.path);
let mut tags = Vec::new();
if options.read_tags_from_txt {
let txt_path = PathBuf::from(format!("{}.txt", path.to_string_lossy()));
if txt_path.exists() {
let content = fs::read_to_string(txt_path).await?;
tags.append(
&mut content
.split('\n')
.map(|line| line.to_owned())
.collect::<Vec<String>>(),
);
}
}
let file_content = fs::read(path).await?;
let file = api.file.add_file(metadata, tags, file_content).await?;
Ok(file)
}
#[tauri::command]
pub async fn find_files(
tags: Vec<TagQuery>,

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

@ -2,11 +2,6 @@ use crate::types::identifier::FileIdentifier;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddFileRequest {
pub path: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReadFileRequest {
pub id: FileIdentifier,
@ -109,3 +104,9 @@ pub struct UpdateFileNameRequest {
pub file_id: FileIdentifier,
pub name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddFileRequestHeader {
pub metadata: FileOSMetadata,
pub tags: Vec<String>,
}

Loading…
Cancel
Save