Add api to create tags and change file tags

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

@ -1,6 +1,6 @@
[package] [package]
name = "mediarepo-api" name = "mediarepo-api"
version = "0.2.0" version = "0.3.0"
edition = "2018" edition = "2018"
license = "gpl-3" license = "gpl-3"

@ -57,14 +57,9 @@ impl FileApi {
/// Reads the file and returns its contents as bytes /// Reads the file and returns its contents as bytes
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn read_file_by_hash(&self, hash: String) -> ApiResult<Vec<u8>> { pub async fn read_file_by_hash(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
let payload: BytePayload = self let payload: BytePayload = self
.emit_and_get( .emit_and_get("read_file", ReadFileRequest { id })
"read_file",
ReadFileRequest {
id: FileIdentifier::Hash(hash),
},
)
.await?; .await?;
Ok(payload.to_payload_bytes()?) Ok(payload.to_payload_bytes()?)
@ -74,15 +69,10 @@ impl FileApi {
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn get_file_thumbnails( pub async fn get_file_thumbnails(
&self, &self,
hash: String, id: FileIdentifier,
) -> ApiResult<Vec<ThumbnailMetadataResponse>> { ) -> ApiResult<Vec<ThumbnailMetadataResponse>> {
self.emit_and_get( self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id })
"get_thumbnails", .await
GetFileThumbnailsRequest {
id: FileIdentifier::Hash(hash),
},
)
.await
} }
/// Reads the thumbnail of the file and returns its contents in bytes /// Reads the thumbnail of the file and returns its contents in bytes

@ -2,7 +2,7 @@ use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi; use crate::client_api::IPCApi;
use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest};
use crate::types::identifier::FileIdentifier; use crate::types::identifier::FileIdentifier;
use crate::types::tags::TagResponse; use crate::types::tags::{ChangeFileTagsRequest, TagResponse};
use async_trait::async_trait; use async_trait::async_trait;
use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::context::{PoolGuard, PooledContext};
use rmp_ipc::ipc::context::Context; use rmp_ipc::ipc::context::Context;
@ -36,14 +36,9 @@ impl TagApi {
/// Returns a list of all tags for a file /// Returns a list of all tags for a file
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn get_tags_for_file(&self, hash: String) -> ApiResult<Vec<TagResponse>> { pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get( self.emit_and_get("tags_for_file", GetFileTagsRequest { id })
"tags_for_file", .await
GetFileTagsRequest {
id: FileIdentifier::Hash(hash),
},
)
.await
} }
/// Returns a list of all tags that are assigned to the list of files /// 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 }) self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes })
.await .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<String>) -> ApiResult<Vec<TagResponse>> {
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<i64>,
removed_tags: Vec<i64>,
) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get(
"change_file_tags",
ChangeFileTagsRequest {
file_id,
added_tags,
removed_tags,
},
)
.await
}
} }

@ -1,6 +1,7 @@
use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess};
use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::error::PluginResult;
use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse}; use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse};
use crate::types::identifier::FileIdentifier;
#[tauri::command] #[tauri::command]
pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMetadataResponse>> { pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult<Vec<FileMetadataResponse>> {
@ -24,16 +25,17 @@ pub async fn find_files(
#[tauri::command] #[tauri::command]
pub async fn read_file_by_hash( pub async fn read_file_by_hash(
hash: String,
mime_type: String,
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,
buffer_state: BufferAccess<'_>, buffer_state: BufferAccess<'_>,
id: i64,
hash: String,
mime_type: String,
) -> PluginResult<String> { ) -> PluginResult<String> {
if buffer_state.reserve_entry(&hash) { if buffer_state.reserve_entry(&hash) {
Ok(format!("once://{}", hash)) // entry has been cached Ok(format!("once://{}", hash)) // entry has been cached
} else { } else {
let api = api_state.api().await?; 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); let uri = add_once_buffer(buffer_state, hash, mime_type, content);
Ok(uri) Ok(uri)
@ -42,11 +44,11 @@ pub async fn read_file_by_hash(
#[tauri::command] #[tauri::command]
pub async fn get_file_thumbnails( pub async fn get_file_thumbnails(
hash: String,
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,
id: i64,
) -> PluginResult<Vec<ThumbnailMetadataResponse>> { ) -> PluginResult<Vec<ThumbnailMetadataResponse>> {
let api = api_state.api().await?; 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) Ok(thumbs)
} }

@ -1,5 +1,6 @@
use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::commands::ApiAccess;
use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::error::PluginResult;
use crate::types::identifier::FileIdentifier;
use crate::types::tags::TagResponse; use crate::types::tags::TagResponse;
#[tauri::command] #[tauri::command]
@ -12,11 +13,11 @@ pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult<Vec<TagRespo
#[tauri::command] #[tauri::command]
pub async fn get_tags_for_file( pub async fn get_tags_for_file(
hash: String, id: i64,
api_state: ApiAccess<'_>, api_state: ApiAccess<'_>,
) -> PluginResult<Vec<TagResponse>> { ) -> PluginResult<Vec<TagResponse>> {
let api = api_state.api().await?; 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) Ok(tags)
} }
@ -31,3 +32,30 @@ pub async fn get_tags_for_files(
Ok(tags) Ok(tags)
} }
#[tauri::command]
pub async fn create_tags(
api_state: ApiAccess<'_>,
tags: Vec<String>,
) -> PluginResult<Vec<TagResponse>> {
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<i64>,
removed_tags: Vec<i64>,
) -> PluginResult<Vec<TagResponse>> {
let api = api_state.api().await?;
let tags = api
.tag
.change_file_tags(FileIdentifier::ID(id), added_tags, removed_tags)
.await?;
Ok(tags)
}

@ -48,7 +48,9 @@ impl<R: Runtime> MediarepoPlugin<R> {
disconnect_repository, disconnect_repository,
close_local_repository, close_local_repository,
check_local_repository_exists, check_local_repository_exists,
remove_repository remove_repository,
change_file_tags,
create_tags
]), ]),
} }
} }

@ -1,8 +1,16 @@
use serde::{Serialize, Deserialize}; use crate::types::identifier::FileIdentifier;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TagResponse { pub struct TagResponse {
pub id: i64, pub id: i64,
pub namespace: Option<String>, pub namespace: Option<String>,
pub name: String, pub name: String,
} }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ChangeFileTagsRequest {
pub file_id: FileIdentifier,
pub removed_tags: Vec<i64>,
pub added_tags: Vec<i64>,
}

Loading…
Cancel
Save