From df2cbe94e0de9eebc3163dc0510d8e4330ce307f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 11:59:28 +0100 Subject: [PATCH] Add api to create tags and change file tags Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 20 +++------- mediarepo-api/src/client_api/tag.rs | 38 ++++++++++++++----- .../src/tauri_plugin/commands/file.rs | 12 +++--- .../src/tauri_plugin/commands/tag.rs | 32 +++++++++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 4 +- mediarepo-api/src/types/tags.rs | 12 +++++- 7 files changed, 85 insertions(+), 35 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index a6488ae..2875353 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.2.0" +version = "0.3.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 2ca6799..d49c86f 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -57,14 +57,9 @@ impl FileApi { /// Reads the file and returns its contents as bytes #[tracing::instrument(level = "debug", skip(self))] - pub async fn read_file_by_hash(&self, hash: String) -> ApiResult> { + pub async fn read_file_by_hash(&self, id: FileIdentifier) -> ApiResult> { let payload: BytePayload = self - .emit_and_get( - "read_file", - ReadFileRequest { - id: FileIdentifier::Hash(hash), - }, - ) + .emit_and_get("read_file", ReadFileRequest { id }) .await?; Ok(payload.to_payload_bytes()?) @@ -74,15 +69,10 @@ impl FileApi { #[tracing::instrument(level = "debug", skip(self))] pub async fn get_file_thumbnails( &self, - hash: String, + id: FileIdentifier, ) -> ApiResult> { - self.emit_and_get( - "get_thumbnails", - GetFileThumbnailsRequest { - id: FileIdentifier::Hash(hash), - }, - ) - .await + self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id }) + .await } /// Reads the thumbnail of the file and returns its contents in bytes diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index bdc2165..4ae9d8e 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -2,7 +2,7 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; use crate::types::identifier::FileIdentifier; -use crate::types::tags::TagResponse; +use crate::types::tags::{ChangeFileTagsRequest, TagResponse}; use async_trait::async_trait; use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; @@ -36,14 +36,9 @@ impl TagApi { /// Returns a list of all tags for a file #[tracing::instrument(level = "debug", skip(self))] - pub async fn get_tags_for_file(&self, hash: String) -> ApiResult> { - self.emit_and_get( - "tags_for_file", - GetFileTagsRequest { - id: FileIdentifier::Hash(hash), - }, - ) - .await + pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult> { + self.emit_and_get("tags_for_file", GetFileTagsRequest { id }) + .await } /// Returns a list of all tags that are assigned to the list of files @@ -52,4 +47,29 @@ impl TagApi { self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }) .await } + + /// Creates a new tag and returns the created tag object + #[tracing::instrument(level = "debug", skip(self))] + pub async fn create_tags(&self, tags: Vec) -> ApiResult> { + self.emit_and_get("create_tags", tags).await + } + + /// Changes the tags of a file + #[tracing::instrument(level = "debug", skip(self))] + pub async fn change_file_tags( + &self, + file_id: FileIdentifier, + added_tags: Vec, + removed_tags: Vec, + ) -> ApiResult> { + self.emit_and_get( + "change_file_tags", + ChangeFileTagsRequest { + file_id, + added_tags, + removed_tags, + }, + ) + .await + } } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 43be28e..324e7c5 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,6 +1,7 @@ use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse}; +use crate::types::identifier::FileIdentifier; #[tauri::command] pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { @@ -24,16 +25,17 @@ pub async fn find_files( #[tauri::command] pub async fn read_file_by_hash( - hash: String, - mime_type: String, api_state: ApiAccess<'_>, buffer_state: BufferAccess<'_>, + id: i64, + hash: String, + mime_type: String, ) -> PluginResult { 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(hash.clone()).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) @@ -42,11 +44,11 @@ pub async fn read_file_by_hash( #[tauri::command] pub async fn get_file_thumbnails( - hash: String, api_state: ApiAccess<'_>, + id: i64, ) -> PluginResult> { let api = api_state.api().await?; - let thumbs = api.file.get_file_thumbnails(hash).await?; + let thumbs = api.file.get_file_thumbnails(FileIdentifier::ID(id)).await?; Ok(thumbs) } diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index a9e2426..d54bfa7 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -1,5 +1,6 @@ use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::error::PluginResult; +use crate::types::identifier::FileIdentifier; use crate::types::tags::TagResponse; #[tauri::command] @@ -12,11 +13,11 @@ pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult, ) -> PluginResult> { let api = api_state.api().await?; - let tags = api.tag.get_tags_for_file(hash).await?; + let tags = api.tag.get_tags_for_file(FileIdentifier::ID(id)).await?; Ok(tags) } @@ -31,3 +32,30 @@ pub async fn get_tags_for_files( Ok(tags) } + +#[tauri::command] +pub async fn create_tags( + api_state: ApiAccess<'_>, + tags: Vec, +) -> PluginResult> { + let api = api_state.api().await?; + let tags = api.tag.create_tags(tags).await?; + + Ok(tags) +} + +#[tauri::command] +pub async fn change_file_tags( + api_state: ApiAccess<'_>, + id: i64, + added_tags: Vec, + removed_tags: Vec, +) -> PluginResult> { + let api = api_state.api().await?; + let tags = api + .tag + .change_file_tags(FileIdentifier::ID(id), added_tags, removed_tags) + .await?; + + Ok(tags) +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index ac031fd..46b97cf 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -48,7 +48,9 @@ impl MediarepoPlugin { disconnect_repository, close_local_repository, check_local_repository_exists, - remove_repository + remove_repository, + change_file_tags, + create_tags ]), } } diff --git a/mediarepo-api/src/types/tags.rs b/mediarepo-api/src/types/tags.rs index 4917af8..ecb7281 100644 --- a/mediarepo-api/src/types/tags.rs +++ b/mediarepo-api/src/types/tags.rs @@ -1,8 +1,16 @@ -use serde::{Serialize, Deserialize}; +use crate::types::identifier::FileIdentifier; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TagResponse { pub id: i64, pub namespace: Option, pub name: String, -} \ No newline at end of file +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChangeFileTagsRequest { + pub file_id: FileIdentifier, + pub removed_tags: Vec, + pub added_tags: Vec, +}