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]
name = "mediarepo-api"
version = "0.2.0"
version = "0.3.0"
edition = "2018"
license = "gpl-3"

@ -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<Vec<u8>> {
pub async fn read_file_by_hash(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
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,14 +69,9 @@ impl FileApi {
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_file_thumbnails(
&self,
hash: String,
id: FileIdentifier,
) -> ApiResult<Vec<ThumbnailMetadataResponse>> {
self.emit_and_get(
"get_thumbnails",
GetFileThumbnailsRequest {
id: FileIdentifier::Hash(hash),
},
)
self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id })
.await
}

@ -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,13 +36,8 @@ 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<Vec<TagResponse>> {
self.emit_and_get(
"tags_for_file",
GetFileTagsRequest {
id: FileIdentifier::Hash(hash),
},
)
pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get("tags_for_file", GetFileTagsRequest { id })
.await
}
@ -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<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::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<Vec<FileMetadataResponse>> {
@ -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<String> {
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<Vec<ThumbnailMetadataResponse>> {
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)
}

@ -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<Vec<TagRespo
#[tauri::command]
pub async fn get_tags_for_file(
hash: String,
id: i64,
api_state: ApiAccess<'_>,
) -> PluginResult<Vec<TagResponse>> {
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<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,
close_local_repository,
check_local_repository_exists,
remove_repository
remove_repository,
change_file_tags,
create_tags
]),
}
}

@ -1,4 +1,5 @@
use serde::{Serialize, Deserialize};
use crate::types::identifier::FileIdentifier;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TagResponse {
@ -6,3 +7,10 @@ pub struct TagResponse {
pub namespace: Option<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