From 57fbd666a865cb9b3e9b413bcb03aefa707c999f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 10:54:59 +0200 Subject: [PATCH 001/116] Add initial project layout and dependencies Signed-off-by: trivernis --- mediarepo-api/.gitignore | 2 ++ mediarepo-api/.idea/.gitignore | 8 ++++++++ mediarepo-api/.idea/discord.xml | 7 +++++++ mediarepo-api/.idea/mediarepo-api.iml | 11 +++++++++++ mediarepo-api/.idea/modules.xml | 8 ++++++++ mediarepo-api/.idea/vcs.xml | 6 ++++++ mediarepo-api/Cargo.toml | 20 ++++++++++++++++++++ mediarepo-api/README.md | 16 ++++++++++++++++ mediarepo-api/src/lib.rs | 4 ++++ mediarepo-api/src/tauri_plugin/mod.rs | 0 mediarepo-api/src/types/mod.rs | 0 11 files changed, 82 insertions(+) create mode 100644 mediarepo-api/.gitignore create mode 100644 mediarepo-api/.idea/.gitignore create mode 100644 mediarepo-api/.idea/discord.xml create mode 100644 mediarepo-api/.idea/mediarepo-api.iml create mode 100644 mediarepo-api/.idea/modules.xml create mode 100644 mediarepo-api/.idea/vcs.xml create mode 100644 mediarepo-api/Cargo.toml create mode 100644 mediarepo-api/README.md create mode 100644 mediarepo-api/src/lib.rs create mode 100644 mediarepo-api/src/tauri_plugin/mod.rs create mode 100644 mediarepo-api/src/types/mod.rs diff --git a/mediarepo-api/.gitignore b/mediarepo-api/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/mediarepo-api/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/mediarepo-api/.idea/.gitignore b/mediarepo-api/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/mediarepo-api/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/mediarepo-api/.idea/discord.xml b/mediarepo-api/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/mediarepo-api/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/mediarepo-api/.idea/mediarepo-api.iml b/mediarepo-api/.idea/mediarepo-api.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/mediarepo-api/.idea/mediarepo-api.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/mediarepo-api/.idea/modules.xml b/mediarepo-api/.idea/modules.xml new file mode 100644 index 0000000..83599d9 --- /dev/null +++ b/mediarepo-api/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/mediarepo-api/.idea/vcs.xml b/mediarepo-api/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/mediarepo-api/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml new file mode 100644 index 0000000..198ff94 --- /dev/null +++ b/mediarepo-api/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "mediarepo-api" +version = "0.1.0" +edition = "2018" +license = "gpl-3" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tracing = "0.1.29" +thiserror = "1.0.30" +rmp-ipc = {version = "0.7.2", optional=true} +tauri = {version = "1.0.0-beta.8", optional=true} + +[dependencies.serde] +version = "1.0.130" +features = ["serde_derive"] + +[features] +tauri-plugin = ["tauri", "rmp-ipc"] \ No newline at end of file diff --git a/mediarepo-api/README.md b/mediarepo-api/README.md new file mode 100644 index 0000000..276d073 --- /dev/null +++ b/mediarepo-api/README.md @@ -0,0 +1,16 @@ +

+mediarepo-api +

+

+This project is a work in progress +

+ +- - - + +This repository contains common mediarepo API types to implement the API both serverside +and clientside. It also contains a tauri plugin (feature "tauri-plugin") to hook it +into the tauri application lifecycle. + +## License + +GPL-3 \ No newline at end of file diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs new file mode 100644 index 0000000..56ce163 --- /dev/null +++ b/mediarepo-api/src/lib.rs @@ -0,0 +1,4 @@ +pub mod types; + +#[cfg(feature = "tauri-plugin")] +pub mod tauri_plugin; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs new file mode 100644 index 0000000..e69de29 From ace0d78aea16407abcae93033b01797fa8636f82 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 11:02:24 +0200 Subject: [PATCH 002/116] Add request and response types Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 1 + mediarepo-api/src/types/files.rs | 55 +++++++++++++++++++++++++++ mediarepo-api/src/types/identifier.rs | 5 +++ mediarepo-api/src/types/misc.rs | 5 +++ mediarepo-api/src/types/mod.rs | 4 ++ mediarepo-api/src/types/tags.rs | 6 +++ 6 files changed, 76 insertions(+) create mode 100644 mediarepo-api/src/types/files.rs create mode 100644 mediarepo-api/src/types/identifier.rs create mode 100644 mediarepo-api/src/types/misc.rs create mode 100644 mediarepo-api/src/types/tags.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 198ff94..edd3ed4 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -9,6 +9,7 @@ license = "gpl-3" [dependencies] tracing = "0.1.29" thiserror = "1.0.30" +chrono = "0.4.19" rmp-ipc = {version = "0.7.2", optional=true} tauri = {version = "1.0.0-beta.8", optional=true} diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs new file mode 100644 index 0000000..4c121b0 --- /dev/null +++ b/mediarepo-api/src/types/files.rs @@ -0,0 +1,55 @@ +use chrono::NaiveDateTime; +use crate::types::identifier::FileIdentifier; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AddFileRequest { + pub path: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReadFileRequest { + pub id: FileIdentifier, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GetFileThumbnailsRequest { + pub id: FileIdentifier, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GetFileTagsRequest { + pub id: FileIdentifier, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FindFilesByTagsRequest { + pub tags: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TagQuery { + pub negate: bool, + pub name: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FileMetadataResponse { + pub id: i64, + pub name: Option, + pub comment: Option, + pub hash: String, + pub file_type: u32, + pub mime_type: Option, + pub creation_time: NaiveDateTime, + pub change_time: NaiveDateTime, + pub import_time: NaiveDateTime, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ThumbnailMetadataResponse { + pub id: i64, + pub hash: Strin, + pub height: i32, + pub width: i32, + pub mime_type: Option, +} \ No newline at end of file diff --git a/mediarepo-api/src/types/identifier.rs b/mediarepo-api/src/types/identifier.rs new file mode 100644 index 0000000..8617373 --- /dev/null +++ b/mediarepo-api/src/types/identifier.rs @@ -0,0 +1,5 @@ +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum FileIdentifier { + ID(i64), + Hash(String), +} \ No newline at end of file diff --git a/mediarepo-api/src/types/misc.rs b/mediarepo-api/src/types/misc.rs new file mode 100644 index 0000000..77307cf --- /dev/null +++ b/mediarepo-api/src/types/misc.rs @@ -0,0 +1,5 @@ +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct InfoResponse { + pub name: String, + pub version: String, +} \ No newline at end of file diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs index e69de29..2a5ed7e 100644 --- a/mediarepo-api/src/types/mod.rs +++ b/mediarepo-api/src/types/mod.rs @@ -0,0 +1,4 @@ +pub mod files; +pub mod identifier; +pub mod misc; +pub mod tags; \ No newline at end of file diff --git a/mediarepo-api/src/types/tags.rs b/mediarepo-api/src/types/tags.rs new file mode 100644 index 0000000..252b3d0 --- /dev/null +++ b/mediarepo-api/src/types/tags.rs @@ -0,0 +1,6 @@ +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TagResponse { + pub id: i64, + pub namespace: Option, + pub name: String, +} \ No newline at end of file From 4ca755c17cfa445ed15aa0d543212801fb17bf60 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 11:13:55 +0200 Subject: [PATCH 003/116] Fix compile errors and add api client Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 8 ++++++-- mediarepo-api/src/client_api/error.rs | 9 +++++++++ mediarepo-api/src/client_api/mod.rs | 24 ++++++++++++++++++++++++ mediarepo-api/src/lib.rs | 3 +++ mediarepo-api/src/types/files.rs | 3 ++- mediarepo-api/src/types/identifier.rs | 2 ++ mediarepo-api/src/types/misc.rs | 2 ++ mediarepo-api/src/types/tags.rs | 2 ++ 8 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 mediarepo-api/src/client_api/error.rs create mode 100644 mediarepo-api/src/client_api/mod.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index edd3ed4..f1071ba 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -9,7 +9,6 @@ license = "gpl-3" [dependencies] tracing = "0.1.29" thiserror = "1.0.30" -chrono = "0.4.19" rmp-ipc = {version = "0.7.2", optional=true} tauri = {version = "1.0.0-beta.8", optional=true} @@ -17,5 +16,10 @@ tauri = {version = "1.0.0-beta.8", optional=true} version = "1.0.130" features = ["serde_derive"] +[dependencies.chrono] +version = "0.4.19" +features = ["serde"] + [features] -tauri-plugin = ["tauri", "rmp-ipc"] \ No newline at end of file +tauri-plugin = ["client-api","tauri", "rmp-ipc"] +client-api = ["rmp-ipc"] \ No newline at end of file diff --git a/mediarepo-api/src/client_api/error.rs b/mediarepo-api/src/client_api/error.rs new file mode 100644 index 0000000..bd8f8df --- /dev/null +++ b/mediarepo-api/src/client_api/error.rs @@ -0,0 +1,9 @@ +use thiserror::Error; + +pub type ApiResult = Result; + +#[derive(Debug, Error)] +pub enum ApiError { + #[error(transparent)] + IPC(#[from] rmp_ipc::error::Error) +} \ No newline at end of file diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs new file mode 100644 index 0000000..a6d7b95 --- /dev/null +++ b/mediarepo-api/src/client_api/mod.rs @@ -0,0 +1,24 @@ +pub mod error; + +use rmp_ipc::ipc::context::Context; +use rmp_ipc::IPCBuilder; +use crate::client_api::error::ApiResult; + +#[derive(Clone)] +pub struct ApiClient { + ctx: Context, +} + +impl ApiClient { + /// Creates a new client from an existing ipc context + pub fn new(ctx: Context) -> Self { + Self {ctx} + } + + /// Connects to the ipc Socket + pub async fn connect(address: &str) -> ApiResult { + let ctx = IPCBuilder::new().address(address).build_client().await?; + + Ok(Self::new(ctx)) + } +} \ No newline at end of file diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs index 56ce163..1c1dbbe 100644 --- a/mediarepo-api/src/lib.rs +++ b/mediarepo-api/src/lib.rs @@ -1,4 +1,7 @@ pub mod types; +#[cfg(feature = "client-api")] +pub mod client_api; + #[cfg(feature = "tauri-plugin")] pub mod tauri_plugin; diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 4c121b0..e80e67a 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,3 +1,4 @@ +use serde::{Serialize, Deserialize}; use chrono::NaiveDateTime; use crate::types::identifier::FileIdentifier; @@ -48,7 +49,7 @@ pub struct FileMetadataResponse { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThumbnailMetadataResponse { pub id: i64, - pub hash: Strin, + pub hash: String, pub height: i32, pub width: i32, pub mime_type: Option, diff --git a/mediarepo-api/src/types/identifier.rs b/mediarepo-api/src/types/identifier.rs index 8617373..b8df84e 100644 --- a/mediarepo-api/src/types/identifier.rs +++ b/mediarepo-api/src/types/identifier.rs @@ -1,3 +1,5 @@ +use serde::{Serialize, Deserialize}; + #[derive(Clone, Debug, Serialize, Deserialize)] pub enum FileIdentifier { ID(i64), diff --git a/mediarepo-api/src/types/misc.rs b/mediarepo-api/src/types/misc.rs index 77307cf..137524f 100644 --- a/mediarepo-api/src/types/misc.rs +++ b/mediarepo-api/src/types/misc.rs @@ -1,3 +1,5 @@ +use serde::{Serialize, Deserialize}; + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct InfoResponse { pub name: String, diff --git a/mediarepo-api/src/types/tags.rs b/mediarepo-api/src/types/tags.rs index 252b3d0..4917af8 100644 --- a/mediarepo-api/src/types/tags.rs +++ b/mediarepo-api/src/types/tags.rs @@ -1,3 +1,5 @@ +use serde::{Serialize, Deserialize}; + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TagResponse { pub id: i64, From 8b5af040752fa11ef1edfcb75d886421e47d2851 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 11:37:22 +0200 Subject: [PATCH 004/116] Add file api Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 10 +++- mediarepo-api/src/client_api/file.rs | 86 ++++++++++++++++++++++++++++ mediarepo-api/src/client_api/mod.rs | 40 ++++++++++++- 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 mediarepo-api/src/client_api/file.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index f1071ba..fe61269 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -9,8 +9,8 @@ license = "gpl-3" [dependencies] tracing = "0.1.29" thiserror = "1.0.30" +async-trait = {version = "0.1.51", optional=true} rmp-ipc = {version = "0.7.2", optional=true} -tauri = {version = "1.0.0-beta.8", optional=true} [dependencies.serde] version = "1.0.130" @@ -20,6 +20,12 @@ features = ["serde_derive"] version = "0.4.19" features = ["serde"] +[dependencies.tauri] +version = "1.0.0-beta.8" +optional=true +default-features = false +features = [] + [features] tauri-plugin = ["client-api","tauri", "rmp-ipc"] -client-api = ["rmp-ipc"] \ No newline at end of file +client-api = ["rmp-ipc", "async-trait"] \ No newline at end of file diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs new file mode 100644 index 0000000..5ffc039 --- /dev/null +++ b/mediarepo-api/src/client_api/file.rs @@ -0,0 +1,86 @@ +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::files::{ + FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, + TagQuery, ThumbnailMetadataResponse, +}; +use crate::types::identifier::FileIdentifier; +use async_trait::async_trait; +use rmp_ipc::prelude::Context; + +#[derive(Clone)] +pub struct FileApi { + ctx: Context, +} + +#[async_trait] +impl IPCApi for FileApi { + fn namespace() -> &'static str { + "files" + } + + fn ctx(&self) -> &Context { + &self.ctx + } +} + +impl FileApi { + /// Creates a new file api client + pub fn new(ctx: Context) -> Self { + Self { ctx } + } + + /// Returns all known files + #[tracing::instrument(level = "debug", skip(self))] + pub async fn all_files(&self) -> ApiResult> { + self.emit_and_get("all_files", ()).await + } + + /// Searches for a file by a list of tags + #[tracing::instrument(level = "debug", skip(self))] + pub async fn find_files(&self, tags: Vec) -> ApiResult> { + let tags = tags + .into_iter() + .map(|tag| TagQuery { + name: tag, + negate: false, + }) + .collect(); + + self.emit_and_get("find_files", FindFilesByTagsRequest { tags }) + .await + } + + /// 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> { + self.emit_and_get( + "read_file", + ReadFileRequest { + id: FileIdentifier::Hash(hash), + }, + ) + .await + } + + /// Returns a list of all thumbnails of the file + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_file_thumbnails( + &self, + hash: String, + ) -> ApiResult> { + self.emit_and_get( + "get_thumbnails", + GetFileThumbnailsRequest { + id: FileIdentifier::Hash(hash), + }, + ) + .await + } + + /// Reads the thumbnail of the file and returns its contents in bytes + #[tracing::instrument(level = "debug", skip(self))] + pub async fn read_thumbnail(&self, hash: String) -> ApiResult> { + self.emit_and_get("read_thumbnail", hash).await + } +} diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index a6d7b95..10d7311 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,18 +1,49 @@ pub mod error; +pub mod file; +use std::fmt::Debug; use rmp_ipc::ipc::context::Context; use rmp_ipc::IPCBuilder; use crate::client_api::error::ApiResult; +use crate::client_api::file::FileApi; +use crate::types::misc::InfoResponse; +use async_trait::async_trait; +use rmp_ipc::ipc::stream_emitter::EmitMetadata; +use rmp_ipc::payload::{EventReceivePayload, EventSendPayload}; + +#[async_trait] +pub trait IPCApi { + fn namespace() -> &'static str; + fn ctx(&self) -> &Context; + + async fn emit(&self, event_name: &str, data: T) -> ApiResult { + let ctx = self.ctx(); + let meta = ctx.emitter.emit_to(Self::namespace(), event_name, data).await?; + + Ok(meta) + } + + async fn emit_and_get(&self, event_name: &str, data: T) -> ApiResult { + let meta = self.emit(event_name, data).await?; + let response = meta.await_reply(self.ctx()).await?; + + Ok(response.data()?) + } +} #[derive(Clone)] pub struct ApiClient { ctx: Context, + pub file: FileApi, } impl ApiClient { /// Creates a new client from an existing ipc context pub fn new(ctx: Context) -> Self { - Self {ctx} + Self { + file: FileApi::new(ctx.clone()), + ctx + } } /// Connects to the ipc Socket @@ -21,4 +52,11 @@ impl ApiClient { Ok(Self::new(ctx)) } + + /// Returns information about the connected ipc server + #[tracing::instrument(level="debug", skip(self))] + pub async fn info(&self) -> ApiResult { + let res = self.ctx.emitter.emit("info", ()).await?.await_reply(&self.ctx).await?; + Ok(res.data::()?) + } } \ No newline at end of file From f90580ea06275057832f19b9272aae224ff12dfa Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 11:40:46 +0200 Subject: [PATCH 005/116] Add tag api Signed-off-by: trivernis --- mediarepo-api/src/client_api/mod.rs | 44 ++++++++++++++++++++++------- mediarepo-api/src/client_api/tag.rs | 41 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 mediarepo-api/src/client_api/tag.rs diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 10d7311..fc55b41 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,29 +1,45 @@ pub mod error; pub mod file; +pub mod tag; -use std::fmt::Debug; -use rmp_ipc::ipc::context::Context; -use rmp_ipc::IPCBuilder; use crate::client_api::error::ApiResult; use crate::client_api::file::FileApi; +use crate::client_api::tag::TagApi; use crate::types::misc::InfoResponse; use async_trait::async_trait; +use rmp_ipc::ipc::context::Context; use rmp_ipc::ipc::stream_emitter::EmitMetadata; use rmp_ipc::payload::{EventReceivePayload, EventSendPayload}; +use rmp_ipc::IPCBuilder; +use std::fmt::Debug; #[async_trait] pub trait IPCApi { fn namespace() -> &'static str; fn ctx(&self) -> &Context; - async fn emit(&self, event_name: &str, data: T) -> ApiResult { + async fn emit( + &self, + event_name: &str, + data: T, + ) -> ApiResult { let ctx = self.ctx(); - let meta = ctx.emitter.emit_to(Self::namespace(), event_name, data).await?; + let meta = ctx + .emitter + .emit_to(Self::namespace(), event_name, data) + .await?; Ok(meta) } - async fn emit_and_get(&self, event_name: &str, data: T) -> ApiResult { + async fn emit_and_get< + T: EventSendPayload + Debug + Send, + R: EventReceivePayload + Debug + Send, + >( + &self, + event_name: &str, + data: T, + ) -> ApiResult { let meta = self.emit(event_name, data).await?; let response = meta.await_reply(self.ctx()).await?; @@ -35,6 +51,7 @@ pub trait IPCApi { pub struct ApiClient { ctx: Context, pub file: FileApi, + pub tag: TagApi, } impl ApiClient { @@ -42,7 +59,8 @@ impl ApiClient { pub fn new(ctx: Context) -> Self { Self { file: FileApi::new(ctx.clone()), - ctx + tag: TagApi::new(ctx.clone()), + ctx, } } @@ -54,9 +72,15 @@ impl ApiClient { } /// Returns information about the connected ipc server - #[tracing::instrument(level="debug", skip(self))] + #[tracing::instrument(level = "debug", skip(self))] pub async fn info(&self) -> ApiResult { - let res = self.ctx.emitter.emit("info", ()).await?.await_reply(&self.ctx).await?; + let res = self + .ctx + .emitter + .emit("info", ()) + .await? + .await_reply(&self.ctx) + .await?; Ok(res.data::()?) } -} \ No newline at end of file +} diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs new file mode 100644 index 0000000..c01c591 --- /dev/null +++ b/mediarepo-api/src/client_api/tag.rs @@ -0,0 +1,41 @@ +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::files::GetFileTagsRequest; +use crate::types::identifier::FileIdentifier; +use crate::types::tags::TagResponse; +use async_trait::async_trait; +use rmp_ipc::ipc::context::Context; + +#[derive(Clone)] +pub struct TagApi { + ctx: Context, +} + +#[async_trait] +impl IPCApi for TagApi { + fn namespace() -> &'static str { + "tags" + } + + fn ctx(&self) -> &Context { + &self.ctx + } +} + +impl TagApi { + pub fn new(ctx: Context) -> Self { + Self { ctx } + } + + /// 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 + } +} From 6912ad6f0532f8c658648edeb072c88b06271a6f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 12:29:43 +0200 Subject: [PATCH 006/116] Add commands to repo plugin Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 9 ++- mediarepo-api/src/client_api/mod.rs | 7 +++ .../src/tauri_plugin/commands/file.rs | 61 +++++++++++++++++++ .../src/tauri_plugin/commands/mod.rs | 20 ++++++ .../src/tauri_plugin/custom_schemes.rs | 25 ++++++++ mediarepo-api/src/tauri_plugin/error.rs | 25 ++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 61 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/state.rs | 53 ++++++++++++++++ 8 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 mediarepo-api/src/tauri_plugin/commands/file.rs create mode 100644 mediarepo-api/src/tauri_plugin/commands/mod.rs create mode 100644 mediarepo-api/src/tauri_plugin/custom_schemes.rs create mode 100644 mediarepo-api/src/tauri_plugin/error.rs create mode 100644 mediarepo-api/src/tauri_plugin/state.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index fe61269..668c4ad 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -11,6 +11,8 @@ tracing = "0.1.29" thiserror = "1.0.30" async-trait = {version = "0.1.51", optional=true} rmp-ipc = {version = "0.7.2", optional=true} +parking_lot = {version="0.11.2", optional=true} +serde_json = {version="1.0.68", optional=true} [dependencies.serde] version = "1.0.130" @@ -26,6 +28,11 @@ optional=true default-features = false features = [] +[dependencies.tokio] +version = "1.12.0" +optional = true +features = ["sync"] + [features] -tauri-plugin = ["client-api","tauri", "rmp-ipc"] +tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json"] client-api = ["rmp-ipc", "async-trait"] \ No newline at end of file diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index fc55b41..62ade72 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -65,6 +65,7 @@ impl ApiClient { } /// Connects to the ipc Socket + #[tracing::instrument(level = "debug")] pub async fn connect(address: &str) -> ApiResult { let ctx = IPCBuilder::new().address(address).build_client().await?; @@ -83,4 +84,10 @@ impl ApiClient { .await?; Ok(res.data::()?) } + + #[tracing::instrument(level = "debug", skip(self))] + pub async fn exit(self) -> ApiResult<()> { + self.ctx.stop().await?; + Ok(()) + } } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs new file mode 100644 index 0000000..535fc98 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -0,0 +1,61 @@ +use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; +use crate::tauri_plugin::error::PluginResult; +use crate::types::files::{FileMetadataResponse, ThumbnailMetadataResponse}; + +#[tauri::command] +pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { + let api = api_state.api().await?; + let all_files = api.file.all_files().await?; + + Ok(all_files) +} + +#[tauri::command] +pub async fn find_files( + tags: Vec, + api_state: ApiAccess<'_>, +) -> PluginResult> { + let api = api_state.api().await?; + let files = api.file.find_files(tags).await?; + + Ok(files) +} + +#[tauri::command] +pub async fn read_file_by_hash( + hash: String, + mime_type: String, + api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, +) -> PluginResult { + let api = api_state.api().await?; + let content = api.file.read_file_by_hash(hash.clone()).await?; + let uri = add_once_buffer(buffer_state, hash, mime_type, content); + + Ok(uri) +} + +#[tauri::command] +pub async fn get_file_thumbnails( + hash: String, + api_state: ApiAccess<'_>, +) -> PluginResult> { + let api = api_state.api().await?; + let thumbs = api.file.get_file_thumbnails(hash).await?; + + Ok(thumbs) +} + +#[tauri::command] +pub async fn read_thumbnail( + hash: String, + mime_type: String, + api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, +) -> PluginResult { + let api = api_state.api().await?; + let content = api.file.read_thumbnail(hash.clone()).await?; + let uri = add_once_buffer(buffer_state, hash, mime_type, content); + + Ok(uri) +} diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs new file mode 100644 index 0000000..f6b415f --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -0,0 +1,20 @@ +use tauri::State; + +pub use file::*; + +use crate::tauri_plugin::state::{ApiState, BufferState, OnceBuffer}; + +pub mod file; + +pub type ApiAccess<'a> = State<'a, ApiState>; +pub type BufferAccess<'a> = State<'a, BufferState>; + +/// Adds a once-buffer to the buffer store +fn add_once_buffer(buffer_state: BufferAccess, key: String, mime: String, buf: Vec) -> String { + let uri = format!("once://{}", key); + let once_buffer = OnceBuffer::new(mime, buf); + let mut once_buffers = buffer_state.buffer.lock(); + once_buffers.insert(key, once_buffer); + + uri +} diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs new file mode 100644 index 0000000..219d3b0 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -0,0 +1,25 @@ +use crate::tauri_plugin::state::BufferState; +use tauri::http::ResponseBuilder; +use tauri::{Builder, Manager, Runtime}; + +pub fn register_custom_uri_schemes(builder: Builder) -> Builder { + builder.register_uri_scheme_protocol("once", |app, request| { + let buf_state = app.state::(); + let resource_key = request.uri().trim_start_matches("once://"); + let buffer = { + let mut buffers = buf_state.buffer.lock(); + buffers.remove(resource_key) + }; + if let Some(buffer) = buffer { + ResponseBuilder::new() + .mimetype(&buffer.mime) + .status(200) + .body(buffer.buf) + } else { + ResponseBuilder::new() + .mimetype("text/plain") + .status(404) + .body("Resource not found".as_bytes().to_vec()) + } + }) +} diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs new file mode 100644 index 0000000..7fc37b0 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -0,0 +1,25 @@ +use crate::client_api::error::ApiError; +use serde::Serialize; + +pub type PluginResult = Result; + +#[derive(Clone, Serialize)] +pub struct PluginError { + message: String, +} + +impl From<&str> for PluginError { + fn from(s: &str) -> Self { + Self { + message: s.to_string(), + } + } +} + +impl From for PluginError { + fn from(e: ApiError) -> Self { + Self { + message: format!("ApiError: {:?}", e), + } + } +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index e69de29..3fa250f 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -0,0 +1,61 @@ +use tauri::plugin::Plugin; +use tauri::{AppHandle, Builder, Invoke, Manager, Runtime}; + +use state::ApiState; + +use crate::tauri_plugin::state::BufferState; + +mod commands; +pub mod custom_schemes; +pub mod error; +mod state; + +use commands::*; + +pub fn register_plugin(builder: Builder) -> Builder { + let repo_plugin = MediarepoPlugin::new(); + + custom_schemes::register_custom_uri_schemes(builder.plugin(repo_plugin)) +} + +pub struct MediarepoPlugin { + invoke_handler: Box) + Send + Sync>, +} + +impl MediarepoPlugin { + pub fn new() -> Self { + Self { + invoke_handler: Box::new(tauri::generate_handler![ + get_all_files, + find_files, + read_file_by_hash, + get_file_thumbnails, + read_thumbnail + ]), + } + } +} + +impl Plugin for MediarepoPlugin { + fn name(&self) -> &'static str { + "mediarepo" + } + + fn initialize( + &mut self, + app: &AppHandle, + _config: serde_json::value::Value, + ) -> tauri::plugin::Result<()> { + let api_state = ApiState::new(); + app.manage(api_state); + + let buffer_state = BufferState::default(); + app.manage(buffer_state); + + Ok(()) + } + + fn extend_api(&mut self, message: Invoke) { + (self.invoke_handler)(message) + } +} diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs new file mode 100644 index 0000000..307c814 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -0,0 +1,53 @@ +use std::collections::HashMap; +use std::mem; +use std::sync::Arc; + +use parking_lot::Mutex; +use tauri::async_runtime::RwLock; + +use crate::client_api::ApiClient; +use crate::tauri_plugin::error::{PluginError, PluginResult}; + +pub struct ApiState { + inner: Arc>>, +} + +impl ApiState { + pub fn new() -> Self { + Self { + inner: Arc::new(RwLock::new(None)), + } + } + + pub async fn set_api(&self, client: ApiClient) { + let mut inner = self.inner.write().await; + let old_client = mem::replace(&mut *inner, Some(client)); + + if let Some(client) = old_client { + let _ = client.exit().await; + } + } + + pub async fn api(&self) -> PluginResult { + let inner = self.inner.read().await; + inner + .clone() + .ok_or_else(|| PluginError::from("Not connected")) + } +} + +pub struct OnceBuffer { + pub mime: String, + pub buf: Vec, +} + +impl OnceBuffer { + pub fn new(mime: String, buf: Vec) -> Self { + Self { mime, buf } + } +} + +#[derive(Default)] +pub struct BufferState { + pub buffer: Arc>>, +} From d7a9a1450f960864abd785f09823ac737a2545c7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 12:55:51 +0200 Subject: [PATCH 007/116] Add repository commands Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 7 +- .../src/tauri_plugin/commands/mod.rs | 14 +++- .../src/tauri_plugin/commands/repo.rs | 82 +++++++++++++++++++ .../src/tauri_plugin/commands/tag.rs | 14 ++++ mediarepo-api/src/tauri_plugin/error.rs | 35 +++++++- mediarepo-api/src/tauri_plugin/mod.rs | 15 +++- mediarepo-api/src/tauri_plugin/settings.rs | 54 ++++++++++++ mediarepo-api/src/tauri_plugin/state.rs | 19 +++++ 8 files changed, 233 insertions(+), 7 deletions(-) create mode 100644 mediarepo-api/src/tauri_plugin/commands/repo.rs create mode 100644 mediarepo-api/src/tauri_plugin/commands/tag.rs create mode 100644 mediarepo-api/src/tauri_plugin/settings.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 668c4ad..61b03a2 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -13,6 +13,7 @@ async-trait = {version = "0.1.51", optional=true} rmp-ipc = {version = "0.7.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} [dependencies.serde] version = "1.0.130" @@ -31,7 +32,11 @@ features = [] [dependencies.tokio] version = "1.12.0" optional = true -features = ["sync"] +features = ["sync", "fs"] + +[dependencies.toml] +version = "0.5.8" +optional = true [features] tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json"] diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index f6b415f..845dc1f 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -1,16 +1,26 @@ use tauri::State; pub use file::*; +pub use repo::*; +pub use tag::*; -use crate::tauri_plugin::state::{ApiState, BufferState, OnceBuffer}; +use crate::tauri_plugin::state::{ApiState, AppState, BufferState, OnceBuffer}; pub mod file; +pub mod repo; +pub mod tag; pub type ApiAccess<'a> = State<'a, ApiState>; pub type BufferAccess<'a> = State<'a, BufferState>; +pub type AppAccess<'a> = State<'a, AppState>; /// Adds a once-buffer to the buffer store -fn add_once_buffer(buffer_state: BufferAccess, key: String, mime: String, buf: Vec) -> String { +fn add_once_buffer( + buffer_state: BufferAccess<'_>, + key: String, + mime: String, + buf: Vec, +) -> String { let uri = format!("once://{}", key); let once_buffer = OnceBuffer::new(mime, buf); let mut once_buffers = buffer_state.buffer.lock(); diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs new file mode 100644 index 0000000..2bd9cc8 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -0,0 +1,82 @@ +use crate::client_api::ApiClient; +use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; +use crate::tauri_plugin::error::{PluginError, PluginResult}; +use crate::tauri_plugin::settings::{save_settings, Repository}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use tokio::fs; + +static REPO_CONFIG_FILE: &str = "repo.toml"; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct RepoConfig { + pub listen_address: String, + pub database_path: String, + pub default_file_store: String, +} + +#[tauri::command] +pub async fn get_repositories(app_state: AppAccess<'_>) -> PluginResult> { + let settings = app_state.settings.read().await; + + Ok(settings.repositories.values().cloned().collect()) +} + +#[tauri::command] +pub async fn get_active_repository(app_state: AppAccess<'_>) -> PluginResult> { + let repo = app_state.active_repo.read().await; + Ok(repo.clone()) +} + +#[tauri::command] +pub async fn add_repository( + name: String, + path: String, + app_state: AppAccess<'_>, +) -> PluginResult> { + let repo_path = path.clone(); + let path = PathBuf::from(path); + let RepoConfig { listen_address, .. } = read_repo_config(path.join(REPO_CONFIG_FILE)).await?; + + let repo = Repository { + name, + path: Some(repo_path), + address: listen_address, + }; + + let mut repositories = Vec::new(); + { + let mut settings = app_state.settings.write().await; + settings.repositories.insert(repo.name.clone(), repo); + save_settings(&settings)?; + repositories.append(&mut settings.repositories.values().cloned().collect()); + } + + Ok(repositories) +} + +#[tauri::command] +pub async fn select_repository( + name: String, + app_state: AppAccess<'_>, + api_state: ApiAccess<'_>, +) -> PluginResult<()> { + let settings = app_state.settings.read().await; + let repo = settings.repositories.get(&name).ok_or(PluginError::from( + format!("Repository '{}' not found", name).as_str(), + ))?; + let client = ApiClient::connect(&repo.address).await?; + api_state.set_api(client).await; + + let mut active_repo = app_state.active_repo.write().await; + *active_repo = Some(repo.clone()); + + Ok(()) +} + +async fn read_repo_config(path: PathBuf) -> PluginResult { + let toml_str = fs::read_to_string(path).await?; + let config = toml::from_str(&toml_str)?; + + Ok(config) +} diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs new file mode 100644 index 0000000..3e6b136 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -0,0 +1,14 @@ +use crate::tauri_plugin::commands::ApiAccess; +use crate::tauri_plugin::error::PluginResult; +use crate::types::tags::TagResponse; + +#[tauri::command] +pub async fn get_tags_for_file( + hash: String, + api_state: ApiAccess<'_>, +) -> PluginResult> { + let api = api_state.api().await?; + let tags = api.tag.get_tags_for_file(hash).await?; + + Ok(tags) +} diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 7fc37b0..d1f1375 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -1,13 +1,22 @@ use crate::client_api::error::ApiError; use serde::Serialize; +use std::fmt::{Display, Formatter}; pub type PluginResult = Result; -#[derive(Clone, Serialize)] +#[derive(Clone, Debug, Serialize)] pub struct PluginError { message: String, } +impl Display for PluginError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.message.fmt(f) + } +} + +impl std::error::Error for PluginError {} + impl From<&str> for PluginError { fn from(s: &str) -> Self { Self { @@ -23,3 +32,27 @@ impl From for PluginError { } } } + +impl From for PluginError { + fn from(e: std::io::Error) -> Self { + Self { + message: e.to_string(), + } + } +} + +impl From for PluginError { + fn from(e: toml::de::Error) -> Self { + Self { + message: format!("Deserialization failed: {:?}", e), + } + } +} + +impl From for PluginError { + fn from(e: toml::ser::Error) -> Self { + Self { + message: format!("Serialization failed: {:?}", e), + } + } +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 3fa250f..801a384 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -3,11 +3,12 @@ use tauri::{AppHandle, Builder, Invoke, Manager, Runtime}; use state::ApiState; -use crate::tauri_plugin::state::BufferState; +use crate::tauri_plugin::state::{AppState, BufferState}; -mod commands; +pub(crate) mod commands; pub mod custom_schemes; pub mod error; +mod settings; mod state; use commands::*; @@ -30,7 +31,12 @@ impl MediarepoPlugin { find_files, read_file_by_hash, get_file_thumbnails, - read_thumbnail + read_thumbnail, + get_repositories, + get_tags_for_file, + get_active_repository, + add_repository, + select_repository ]), } } @@ -52,6 +58,9 @@ impl Plugin for MediarepoPlugin { let buffer_state = BufferState::default(); app.manage(buffer_state); + let repo_state = AppState::load()?; + app.manage(repo_state); + Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs new file mode 100644 index 0000000..0b7d164 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -0,0 +1,54 @@ +use crate::tauri_plugin::error::PluginResult; +use directories::ProjectDirs; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::fs; +use std::path::PathBuf; + +static SETTINGS_FILE: &str = "settings.toml"; + +#[derive(Serialize, Deserialize, Clone)] +pub struct Repository { + pub(crate) name: String, + pub(crate) path: Option, + pub(crate) address: String, +} + +#[derive(Default, Serialize, Deserialize)] +pub struct Settings { + pub repositories: HashMap, +} + +fn get_settings_path() -> PathBuf { + let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap(); + let config_path = dirs.config_dir().to_path_buf(); + + config_path.join(SETTINGS_FILE) +} + +/// Writes the settings to the file +pub fn save_settings(settings: &Settings) -> PluginResult<()> { + let settings_path = get_settings_path(); + let settings_string = toml::to_string(&settings)?; + fs::write(&settings_path, &settings_string.into_bytes())?; + + Ok(()) +} + +/// Loads the settings from the file +pub fn load_settings() -> PluginResult { + let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap(); + let config_path = dirs.config_dir().to_path_buf(); + if !config_path.exists() { + fs::create_dir_all(&config_path)?; + } + let settings_path = config_path.join(SETTINGS_FILE); + if !settings_path.exists() { + let settings = Settings::default(); + save_settings(&settings)?; + } + let config_str = fs::read_to_string(settings_path)?; + let settings = toml::from_str(&config_str)?; + + Ok(settings) +} diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 307c814..f23178e 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -7,6 +7,7 @@ use tauri::async_runtime::RwLock; use crate::client_api::ApiClient; use crate::tauri_plugin::error::{PluginError, PluginResult}; +use crate::tauri_plugin::settings::{load_settings, Repository, Settings}; pub struct ApiState { inner: Arc>>, @@ -51,3 +52,21 @@ impl OnceBuffer { pub struct BufferState { pub buffer: Arc>>, } + +pub struct AppState { + pub active_repo: Arc>>, + pub settings: Arc>, +} + +impl AppState { + pub fn load() -> PluginResult { + let settings = load_settings()?; + + let state = Self { + active_repo: Arc::new(RwLock::new(None)), + settings: Arc::new(RwLock::new(settings)), + }; + + Ok(state) + } +} From d9495e83f5cef533575af099f44d0d617247fb30 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 13:19:08 +0200 Subject: [PATCH 008/116] Fix feature set for rmp-ipc and tauri-plugin Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 61b03a2..dfa4138 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -32,12 +32,12 @@ features = [] [dependencies.tokio] version = "1.12.0" optional = true -features = ["sync", "fs"] +features = ["sync", "fs", "net", "io-util", "io-std", "time", "rt"] [dependencies.toml] version = "0.5.8" optional = true [features] -tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json"] -client-api = ["rmp-ipc", "async-trait"] \ No newline at end of file +tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories"] +client-api = ["rmp-ipc", "async-trait", "tokio"] \ No newline at end of file From 02653bdae8d3b9da758f26c5297421d276d24c64 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 13:23:50 +0200 Subject: [PATCH 009/116] =?UTF-8?q?Add=20more=20tracing=20and=20error=20ha?= =?UTF-8?q?ndling=C2=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/mod.rs | 1 + mediarepo-api/src/tauri_plugin/settings.rs | 9 ++++++--- mediarepo-api/src/tauri_plugin/state.rs | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 801a384..6436aa7 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -47,6 +47,7 @@ impl Plugin for MediarepoPlugin { "mediarepo" } + #[tracing::instrument(skip(self, app, _config))] fn initialize( &mut self, app: &AppHandle, diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 0b7d164..8fbcc68 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -7,14 +7,14 @@ use std::path::PathBuf; static SETTINGS_FILE: &str = "settings.toml"; -#[derive(Serialize, Deserialize, Clone)] +#[derive(Serialize, Debug, Deserialize, Clone)] pub struct Repository { pub(crate) name: String, pub(crate) path: Option, pub(crate) address: String, } -#[derive(Default, Serialize, Deserialize)] +#[derive(Default, Debug, Serialize, Deserialize)] pub struct Settings { pub repositories: HashMap, } @@ -27,6 +27,7 @@ fn get_settings_path() -> PathBuf { } /// Writes the settings to the file +#[tracing::instrument(level = "debug")] pub fn save_settings(settings: &Settings) -> PluginResult<()> { let settings_path = get_settings_path(); let settings_string = toml::to_string(&settings)?; @@ -36,8 +37,10 @@ pub fn save_settings(settings: &Settings) -> PluginResult<()> { } /// Loads the settings from the file +#[tracing::instrument(level = "debug")] pub fn load_settings() -> PluginResult { - let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap(); + let dirs = ProjectDirs::from("com", "trivernis", "mediarepo") + .expect("Failed to get project directories"); let config_path = dirs.config_dir().to_path_buf(); if !config_path.exists() { fs::create_dir_all(&config_path)?; diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index f23178e..6833f99 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -59,6 +59,7 @@ pub struct AppState { } impl AppState { + #[tracing::instrument(level = "debug")] pub fn load() -> PluginResult { let settings = load_settings()?; From 7e8b2440183495f4518c58bb7f9fb451688f24ee Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 13:37:42 +0200 Subject: [PATCH 010/116] Fix fetching of raw byte payloads for read_file and read_thumbnail Signed-off-by: trivernis --- mediarepo-api/src/client_api/file.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 5ffc039..52033d3 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -6,6 +6,7 @@ use crate::types::files::{ }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; +use rmp_ipc::payload::{BytePayload, EventSendPayload}; use rmp_ipc::prelude::Context; #[derive(Clone)] @@ -54,13 +55,16 @@ 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> { - self.emit_and_get( - "read_file", - ReadFileRequest { - id: FileIdentifier::Hash(hash), - }, - ) - .await + let payload: BytePayload = self + .emit_and_get( + "read_file", + ReadFileRequest { + id: FileIdentifier::Hash(hash), + }, + ) + .await?; + + Ok(payload.to_payload_bytes()?) } /// Returns a list of all thumbnails of the file @@ -81,6 +85,7 @@ impl FileApi { /// Reads the thumbnail of the file and returns its contents in bytes #[tracing::instrument(level = "debug", skip(self))] pub async fn read_thumbnail(&self, hash: String) -> ApiResult> { - self.emit_and_get("read_thumbnail", hash).await + let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?; + Ok(payload.to_payload_bytes()?) } } From a0316b01d24a729de901f7414ce8ef52012acfe3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 26 Oct 2021 14:19:04 +0200 Subject: [PATCH 011/116] Add api to get a list of all tags Signed-off-by: trivernis --- mediarepo-api/src/client_api/tag.rs | 6 ++++++ mediarepo-api/src/tauri_plugin/commands/tag.rs | 8 ++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 1 + 3 files changed, 15 insertions(+) diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index c01c591..f9f976b 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -27,6 +27,12 @@ impl TagApi { Self { ctx } } + /// Returns a list of all tags stored in the repo + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_all_tags(&self) -> ApiResult> { + self.emit_and_get("all_tags", ()).await + } + /// 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> { diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index 3e6b136..72e753f 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -2,6 +2,14 @@ use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::error::PluginResult; use crate::types::tags::TagResponse; +#[tauri::command] +pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult> { + let api = api_state.api().await?; + let all_tags = api.tag.get_all_tags().await?; + + Ok(all_tags) +} + #[tauri::command] pub async fn get_tags_for_file( hash: String, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 6436aa7..76507ee 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -33,6 +33,7 @@ impl MediarepoPlugin { get_file_thumbnails, read_thumbnail, get_repositories, + get_all_tags, get_tags_for_file, get_active_repository, add_repository, From b7742a0fbaa94e881d1fab1874369838b330a843 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 26 Oct 2021 14:27:16 +0200 Subject: [PATCH 012/116] Change reponse type of get_all_tags Signed-off-by: trivernis --- mediarepo-api/src/client_api/tag.rs | 2 +- mediarepo-api/src/tauri_plugin/commands/tag.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index f9f976b..12242e5 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -29,7 +29,7 @@ impl TagApi { /// Returns a list of all tags stored in the repo #[tracing::instrument(level = "debug", skip(self))] - pub async fn get_all_tags(&self) -> ApiResult> { + pub async fn get_all_tags(&self) -> ApiResult> { self.emit_and_get("all_tags", ()).await } diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index 72e753f..6a9b11c 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -3,7 +3,7 @@ use crate::tauri_plugin::error::PluginResult; use crate::types::tags::TagResponse; #[tauri::command] -pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult> { +pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult> { let api = api_state.api().await?; let all_tags = api.tag.get_all_tags().await?; From 2ad64f61daaa75975f1152369d57187e85894fdf Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 26 Oct 2021 21:58:08 +0200 Subject: [PATCH 013/116] Use TagQuery instead of strings for tag searches Signed-off-by: trivernis --- mediarepo-api/src/client_api/file.rs | 10 +--------- mediarepo-api/src/tauri_plugin/commands/file.rs | 4 ++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 52033d3..5936527 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -39,15 +39,7 @@ impl FileApi { /// Searches for a file by a list of tags #[tracing::instrument(level = "debug", skip(self))] - pub async fn find_files(&self, tags: Vec) -> ApiResult> { - let tags = tags - .into_iter() - .map(|tag| TagQuery { - name: tag, - negate: false, - }) - .collect(); - + pub async fn find_files(&self, tags: Vec) -> ApiResult> { self.emit_and_get("find_files", FindFilesByTagsRequest { tags }) .await } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 535fc98..c7347e7 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,6 +1,6 @@ use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; -use crate::types::files::{FileMetadataResponse, ThumbnailMetadataResponse}; +use crate::types::files::{FileMetadataResponse, TagQuery, ThumbnailMetadataResponse}; #[tauri::command] pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { @@ -12,7 +12,7 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult, + tags: Vec, api_state: ApiAccess<'_>, ) -> PluginResult> { let api = api_state.api().await?; From 4d4f143ec5ee2923cb4b7f6dec7ac7330a670c64 Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 29 Oct 2021 19:57:30 +0200 Subject: [PATCH 014/116] Add sorting to file search Signed-off-by: trivernis --- mediarepo-api/src/client_api/file.rs | 18 +++++++--- .../src/tauri_plugin/commands/file.rs | 5 +-- mediarepo-api/src/types/files.rs | 33 +++++++++++++++++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 5936527..cb8abe3 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -2,7 +2,7 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, - TagQuery, ThumbnailMetadataResponse, + SortKey, TagQuery, ThumbnailMetadataResponse, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -39,9 +39,19 @@ impl FileApi { /// Searches for a file by a list of tags #[tracing::instrument(level = "debug", skip(self))] - pub async fn find_files(&self, tags: Vec) -> ApiResult> { - self.emit_and_get("find_files", FindFilesByTagsRequest { tags }) - .await + pub async fn find_files( + &self, + tags: Vec, + sort_expression: Vec, + ) -> ApiResult> { + self.emit_and_get( + "find_files", + FindFilesByTagsRequest { + tags, + sort_expression, + }, + ) + .await } /// Reads the file and returns its contents as bytes diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index c7347e7..3b8bee2 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,6 +1,6 @@ use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; -use crate::types::files::{FileMetadataResponse, TagQuery, ThumbnailMetadataResponse}; +use crate::types::files::{FileMetadataResponse, SortKey, TagQuery, ThumbnailMetadataResponse}; #[tauri::command] pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { @@ -13,10 +13,11 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult, + sort_by: Vec, api_state: ApiAccess<'_>, ) -> PluginResult> { let api = api_state.api().await?; - let files = api.file.find_files(tags).await?; + let files = api.file.find_files(tags, sort_by).await?; Ok(files) } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index e80e67a..9a74219 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,6 +1,6 @@ -use serde::{Serialize, Deserialize}; -use chrono::NaiveDateTime; use crate::types::identifier::FileIdentifier; +use chrono::NaiveDateTime; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AddFileRequest { @@ -25,6 +25,7 @@ pub struct GetFileTagsRequest { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FindFilesByTagsRequest { pub tags: Vec, + pub sort_expression: Vec, } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -33,6 +34,32 @@ pub struct TagQuery { pub name: String, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum SortKey { + Namespace(SortNamespace), + FileName(SortDirection), + FileSize(SortDirection), + FileImportedTime(SortDirection), + FileCreatedTime(SortDirection), + FileChangeTime(SortDirection), + FileType(SortDirection), + NumTags(SortDirection), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SortNamespace { + pub tag: String, + pub direction: SortDirection, +} + +#[derive(Clone, Debug, Serialize, Deserialize, Ord, PartialOrd, PartialEq)] +pub enum SortDirection { + Ascending, + Descending, +} + +impl Eq for SortDirection {} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FileMetadataResponse { pub id: i64, @@ -53,4 +80,4 @@ pub struct ThumbnailMetadataResponse { pub height: i32, pub width: i32, pub mime_type: Option, -} \ No newline at end of file +} From bf6f50ecddf8ac2300217a541ee5a6555480b503 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 30 Oct 2021 19:07:07 +0200 Subject: [PATCH 015/116] Change name of sort namespace field 'tag' Signed-off-by: trivernis --- mediarepo-api/src/types/files.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 9a74219..ad4e813 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -48,7 +48,7 @@ pub enum SortKey { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct SortNamespace { - pub tag: String, + pub name: String, pub direction: SortDirection, } From 09c47763211a10af5a29c64d5f42a2898a170f6a Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 30 Oct 2021 19:28:57 +0200 Subject: [PATCH 016/116] Add caching to buffers Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 24 ++++--- .../src/tauri_plugin/commands/mod.rs | 4 +- .../src/tauri_plugin/custom_schemes.rs | 7 +- mediarepo-api/src/tauri_plugin/state.rs | 65 +++++++++++++++++-- 4 files changed, 82 insertions(+), 18 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 3b8bee2..43be28e 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -29,11 +29,15 @@ pub async fn read_file_by_hash( api_state: ApiAccess<'_>, buffer_state: BufferAccess<'_>, ) -> PluginResult { - let api = api_state.api().await?; - let content = api.file.read_file_by_hash(hash.clone()).await?; - let uri = add_once_buffer(buffer_state, hash, mime_type, content); + 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 uri = add_once_buffer(buffer_state, hash, mime_type, content); - Ok(uri) + Ok(uri) + } } #[tauri::command] @@ -54,9 +58,13 @@ pub async fn read_thumbnail( api_state: ApiAccess<'_>, buffer_state: BufferAccess<'_>, ) -> PluginResult { - let api = api_state.api().await?; - let content = api.file.read_thumbnail(hash.clone()).await?; - let uri = add_once_buffer(buffer_state, hash, mime_type, content); + 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_thumbnail(hash.clone()).await?; + let uri = add_once_buffer(buffer_state, hash, mime_type, content); - Ok(uri) + Ok(uri) + } } diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index 845dc1f..f58ab56 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -4,7 +4,7 @@ pub use file::*; pub use repo::*; pub use tag::*; -use crate::tauri_plugin::state::{ApiState, AppState, BufferState, OnceBuffer}; +use crate::tauri_plugin::state::{ApiState, AppState, BufferState, VolatileBuffer}; pub mod file; pub mod repo; @@ -22,7 +22,7 @@ fn add_once_buffer( buf: Vec, ) -> String { let uri = format!("once://{}", key); - let once_buffer = OnceBuffer::new(mime, buf); + let once_buffer = VolatileBuffer::new(mime, buf); let mut once_buffers = buffer_state.buffer.lock(); once_buffers.insert(key, once_buffer); diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 219d3b0..6ad31c4 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -6,10 +6,9 @@ pub fn register_custom_uri_schemes(builder: Builder) -> Builder(); let resource_key = request.uri().trim_start_matches("once://"); - let buffer = { - let mut buffers = buf_state.buffer.lock(); - buffers.remove(resource_key) - }; + + let buffer = buf_state.get_entry(resource_key); + buf_state.clear_expired(); if let Some(buffer) = buffer { ResponseBuilder::new() .mimetype(&buffer.mime) diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 6833f99..3179f42 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -1,9 +1,11 @@ use std::collections::HashMap; use std::mem; use std::sync::Arc; +use std::time::Duration; use parking_lot::Mutex; use tauri::async_runtime::RwLock; +use tokio::time::Instant; use crate::client_api::ApiClient; use crate::tauri_plugin::error::{PluginError, PluginResult}; @@ -37,20 +39,75 @@ impl ApiState { } } -pub struct OnceBuffer { +#[derive(Clone)] +pub struct VolatileBuffer { + pub accessed: bool, + pub valid_until: Instant, pub mime: String, pub buf: Vec, } -impl OnceBuffer { +impl VolatileBuffer { pub fn new(mime: String, buf: Vec) -> Self { - Self { mime, buf } + Self { + accessed: false, + valid_until: Instant::now() + Duration::from_secs(60), + mime, + buf, + } } } #[derive(Default)] pub struct BufferState { - pub buffer: Arc>>, + pub buffer: Arc>>, +} + +impl BufferState { + /// Checks if an entry for the specific key exists and resets + /// its state so that it can safely be accessed again. + pub fn reserve_entry(&self, key: &String) -> bool { + let mut buffers = self.buffer.lock(); + let entry = buffers.get_mut(key); + + if let Some(entry) = entry { + entry.accessed = false; // reset that it has been accessed so it can be reused + true + } else { + false + } + } + + /// Returns the cloned buffer entry and flags it for expiration + pub fn get_entry(&self, key: &str) -> Option { + let mut buffers = self.buffer.lock(); + let entry = buffers.get_mut(key); + + if let Some(entry) = entry { + entry.accessed = true; + entry.valid_until = Instant::now() + Duration::from_secs(10); // time to live is 10 seconds + + Some(entry.clone()) + } else { + None + } + } + + /// Clears all expired entries + pub fn clear_expired(&self) { + let mut buffer = self.buffer.lock(); + let keys: Vec = buffer.keys().cloned().collect(); + + for key in keys { + let (accessed, valid_until) = { + let entry = buffer.get(&key).unwrap(); + (entry.accessed, entry.valid_until.clone()) + }; + if accessed && valid_until < Instant::now() { + buffer.remove(&key); + } + } + } } pub struct AppState { From 32e85dae7a5be5922d217df5e97052bd16c0108c Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 30 Oct 2021 19:35:27 +0200 Subject: [PATCH 017/116] Add cleaning of buffers to separate worker thread Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 2 +- mediarepo-api/src/tauri_plugin/mod.rs | 8 +++++++- mediarepo-api/src/tauri_plugin/state.rs | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 6ad31c4..a5b4bd2 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -8,7 +8,7 @@ pub fn register_custom_uri_schemes(builder: Builder) -> Builder Plugin for MediarepoPlugin { app.manage(api_state); let buffer_state = BufferState::default(); - app.manage(buffer_state); + app.manage(buffer_state.clone()); + thread::spawn(move || loop { + thread::sleep(Duration::from_secs(10)); + buffer_state.clear_expired(); + }); let repo_state = AppState::load()?; app.manage(repo_state); diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 3179f42..f73132a 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -58,7 +58,7 @@ impl VolatileBuffer { } } -#[derive(Default)] +#[derive(Default, Clone)] pub struct BufferState { pub buffer: Arc>>, } From b64cb291370cbf9680b7d541071089d8db4db941 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 31 Oct 2021 10:41:35 +0100 Subject: [PATCH 018/116] Update rmp-ipc version Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index dfa4138..91da66e 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -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.7.2", optional=true} +rmp-ipc = {version = "0.8.1", 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} From 9c1ba03bac6dfc64c25aabc4bd864d197cfb0442 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 31 Oct 2021 10:59:33 +0100 Subject: [PATCH 019/116] Switch to pooled ipc client Signed-off-by: trivernis --- mediarepo-api/src/client_api/file.rs | 9 +++++---- mediarepo-api/src/client_api/mod.rs | 23 ++++++++++++++--------- mediarepo-api/src/client_api/tag.rs | 9 +++++---- mediarepo-api/src/tauri_plugin/error.rs | 17 ++++++++++++++--- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index cb8abe3..2ca6799 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -6,12 +6,13 @@ use crate::types::files::{ }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::payload::{BytePayload, EventSendPayload}; use rmp_ipc::prelude::Context; #[derive(Clone)] pub struct FileApi { - ctx: Context, + ctx: PooledContext, } #[async_trait] @@ -20,14 +21,14 @@ impl IPCApi for FileApi { "files" } - fn ctx(&self) -> &Context { - &self.ctx + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() } } impl FileApi { /// Creates a new file api client - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 62ade72..3f27ac5 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -7,6 +7,7 @@ use crate::client_api::file::FileApi; use crate::client_api::tag::TagApi; use crate::types::misc::InfoResponse; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; use rmp_ipc::ipc::stream_emitter::EmitMetadata; use rmp_ipc::payload::{EventReceivePayload, EventSendPayload}; @@ -16,7 +17,7 @@ use std::fmt::Debug; #[async_trait] pub trait IPCApi { fn namespace() -> &'static str; - fn ctx(&self) -> &Context; + fn ctx(&self) -> PoolGuard; async fn emit( &self, @@ -41,7 +42,7 @@ pub trait IPCApi { data: T, ) -> ApiResult { let meta = self.emit(event_name, data).await?; - let response = meta.await_reply(self.ctx()).await?; + let response = meta.await_reply(&self.ctx()).await?; Ok(response.data()?) } @@ -49,14 +50,14 @@ pub trait IPCApi { #[derive(Clone)] pub struct ApiClient { - ctx: Context, + ctx: PooledContext, pub file: FileApi, pub tag: TagApi, } impl ApiClient { /// Creates a new client from an existing ipc context - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), @@ -67,7 +68,10 @@ impl ApiClient { /// Connects to the ipc Socket #[tracing::instrument(level = "debug")] pub async fn connect(address: &str) -> ApiResult { - let ctx = IPCBuilder::new().address(address).build_client().await?; + let ctx = IPCBuilder::new() + .address(address) + .build_pooled_client(8) + .await?; Ok(Self::new(ctx)) } @@ -75,19 +79,20 @@ impl ApiClient { /// Returns information about the connected ipc server #[tracing::instrument(level = "debug", skip(self))] pub async fn info(&self) -> ApiResult { - let res = self - .ctx + let ctx = self.ctx.acquire(); + let res = ctx .emitter .emit("info", ()) .await? - .await_reply(&self.ctx) + .await_reply(&ctx) .await?; Ok(res.data::()?) } #[tracing::instrument(level = "debug", skip(self))] pub async fn exit(self) -> ApiResult<()> { - self.ctx.stop().await?; + let ctx = (*self.ctx.acquire()).clone(); + ctx.stop().await?; Ok(()) } } diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 12242e5..05ceda6 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -4,11 +4,12 @@ use crate::types::files::GetFileTagsRequest; use crate::types::identifier::FileIdentifier; use crate::types::tags::TagResponse; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; #[derive(Clone)] pub struct TagApi { - ctx: Context, + ctx: PooledContext, } #[async_trait] @@ -17,13 +18,13 @@ impl IPCApi for TagApi { "tags" } - fn ctx(&self) -> &Context { - &self.ctx + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() } } impl TagApi { - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index d1f1375..f5440fd 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -1,4 +1,5 @@ use crate::client_api::error::ApiError; +use rmp_ipc::error::Error; use serde::Serialize; use std::fmt::{Display, Formatter}; @@ -27,9 +28,19 @@ impl From<&str> for PluginError { impl From for PluginError { fn from(e: ApiError) -> Self { - Self { - message: format!("ApiError: {:?}", e), - } + let message = match e { + ApiError::IPC(ipc_error) => match ipc_error { + Error::Message(message) => message, + Error::SendError => String::from("Failed to send event to daemon"), + Error::ErrorEvent(e) => { + format!("Received error: {}", e.to_string()) + } + e => { + format!("{:?}", e) + } + }, + }; + Self { message } } } From aab7f71e124e287fd1d551757ed2b2a54edb95d7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 31 Oct 2021 11:15:00 +0100 Subject: [PATCH 020/116] Improve buffer performance Signed-off-by: trivernis --- .../src/tauri_plugin/commands/mod.rs | 5 ++- mediarepo-api/src/tauri_plugin/state.rs | 38 ++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index f58ab56..7e2cac7 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -1,3 +1,4 @@ +use parking_lot::lock_api::Mutex; use tauri::State; pub use file::*; @@ -23,8 +24,8 @@ fn add_once_buffer( ) -> String { let uri = format!("once://{}", key); let once_buffer = VolatileBuffer::new(mime, buf); - let mut once_buffers = buffer_state.buffer.lock(); - once_buffers.insert(key, once_buffer); + let mut once_buffers = buffer_state.buffer.write(); + once_buffers.insert(key, Mutex::new(once_buffer)); uri } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index f73132a..98dadb6 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use std::time::Duration; use parking_lot::Mutex; +use parking_lot::RwLock as ParkingRwLock; use tauri::async_runtime::RwLock; use tokio::time::Instant; @@ -41,7 +42,6 @@ impl ApiState { #[derive(Clone)] pub struct VolatileBuffer { - pub accessed: bool, pub valid_until: Instant, pub mime: String, pub buf: Vec, @@ -50,8 +50,7 @@ pub struct VolatileBuffer { impl VolatileBuffer { pub fn new(mime: String, buf: Vec) -> Self { Self { - accessed: false, - valid_until: Instant::now() + Duration::from_secs(60), + valid_until: Instant::now() + Duration::from_secs(120), // buffers that weren't accessed get deleted after 2 minutes mime, buf, } @@ -60,18 +59,19 @@ impl VolatileBuffer { #[derive(Default, Clone)] pub struct BufferState { - pub buffer: Arc>>, + pub buffer: Arc>>>, } impl BufferState { /// Checks if an entry for the specific key exists and resets /// its state so that it can safely be accessed again. pub fn reserve_entry(&self, key: &String) -> bool { - let mut buffers = self.buffer.lock(); - let entry = buffers.get_mut(key); + let buffers = self.buffer.read(); + let entry = buffers.get(key); if let Some(entry) = entry { - entry.accessed = false; // reset that it has been accessed so it can be reused + let mut entry = entry.lock(); + entry.valid_until = Instant::now() + Duration::from_secs(120); // reset the timer so that it can be accessed again true } else { false @@ -80,12 +80,12 @@ impl BufferState { /// Returns the cloned buffer entry and flags it for expiration pub fn get_entry(&self, key: &str) -> Option { - let mut buffers = self.buffer.lock(); - let entry = buffers.get_mut(key); + let buffers = self.buffer.read(); + let entry = buffers.get(key); if let Some(entry) = entry { - entry.accessed = true; - entry.valid_until = Instant::now() + Duration::from_secs(10); // time to live is 10 seconds + let mut entry = entry.lock(); + entry.valid_until = Instant::now() + Duration::from_secs(30); // ttl is 30 seconds after being accessed Some(entry.clone()) } else { @@ -95,15 +95,19 @@ impl BufferState { /// Clears all expired entries pub fn clear_expired(&self) { - let mut buffer = self.buffer.lock(); - let keys: Vec = buffer.keys().cloned().collect(); + let keys: Vec = { + let buffer = self.buffer.read(); + buffer.keys().cloned().collect() + }; for key in keys { - let (accessed, valid_until) = { - let entry = buffer.get(&key).unwrap(); - (entry.accessed, entry.valid_until.clone()) + let valid_until = { + let buffer = self.buffer.read(); + let entry = buffer.get(&key).unwrap().lock(); + entry.valid_until.clone() }; - if accessed && valid_until < Instant::now() { + if valid_until < Instant::now() { + let mut buffer = self.buffer.write(); buffer.remove(&key); } } From 305ac256d1a0f950c22c99340d7c04e0e3e507f8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 1 Nov 2021 21:06:40 +0100 Subject: [PATCH 021/116] Add daemon management commands Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/daemon_management/cli.rs | 82 +++++++++++++++++++ mediarepo-api/src/daemon_management/error.rs | 30 +++++++ mediarepo-api/src/daemon_management/mod.rs | 2 + mediarepo-api/src/lib.rs | 3 + .../src/tauri_plugin/commands/daemon.rs | 25 ++++++ .../src/tauri_plugin/commands/mod.rs | 2 + mediarepo-api/src/tauri_plugin/error.rs | 7 ++ mediarepo-api/src/tauri_plugin/mod.rs | 12 ++- mediarepo-api/src/tauri_plugin/settings.rs | 12 ++- mediarepo-api/src/tauri_plugin/state.rs | 19 ++++- 11 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 mediarepo-api/src/daemon_management/cli.rs create mode 100644 mediarepo-api/src/daemon_management/error.rs create mode 100644 mediarepo-api/src/daemon_management/mod.rs create mode 100644 mediarepo-api/src/tauri_plugin/commands/daemon.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 91da66e..41a3a71 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -32,7 +32,7 @@ features = [] [dependencies.tokio] version = "1.12.0" optional = true -features = ["sync", "fs", "net", "io-util", "io-std", "time", "rt"] +features = ["sync", "fs", "net", "io-util", "io-std", "time", "rt", "process"] [dependencies.toml] version = "0.5.8" diff --git a/mediarepo-api/src/daemon_management/cli.rs b/mediarepo-api/src/daemon_management/cli.rs new file mode 100644 index 0000000..215403f --- /dev/null +++ b/mediarepo-api/src/daemon_management/cli.rs @@ -0,0 +1,82 @@ +use crate::daemon_management::error::{DaemonError, DaemonResult}; +use std::ffi::OsStr; +use tokio::process::{Child, Command}; + +#[derive(Debug)] +pub struct DaemonCli { + daemon_path: String, + repo_path: String, + child: Option, +} + +impl DaemonCli { + pub fn new(daemon_path: String, repo_path: String) -> Self { + Self { + daemon_path, + repo_path, + child: None, + } + } + + /// Initializes a repository at the specified path + #[tracing::instrument] + pub async fn init_repo(&self) -> DaemonResult<()> { + let output = self + .run_command(vec!["--repo", self.repo_path.as_str(), "init"]) + .await?; + tracing::debug!("Response: {}", String::from_utf8(output).unwrap()); + + Ok(()) + } + + /// Starts a daemon for the given repository + #[tracing::instrument] + pub fn start_daemon(&mut self) -> DaemonResult<()> { + let child = self.run_daemon_process(vec!["--repo", self.repo_path.as_str(), "start"])?; + self.child = Some(child); + + Ok(()) + } + + /// Returns if the daemon is currently running + pub fn daemon_running(&mut self) -> bool { + if let Some(child) = &mut self.child { + child.try_wait().map(|e| e.is_some()).unwrap_or(true) + } else { + false + } + } + + /// Returns the path the daemon is serving + pub fn repo_path(&self) -> &String { + &self.repo_path + } + + /// Runs a daemon subcommand + async fn run_command, I: IntoIterator>( + &self, + args: I, + ) -> DaemonResult> { + let child = self.run_daemon_process(args)?; + let output = child.wait_with_output().await?; + + if output.status.success() { + Ok(output.stdout) + } else { + let stdout = String::from_utf8(output.stdout).map_err(|e| e.to_string())?; + let stderr = String::from_utf8(output.stderr).map_err(|e| e.to_string())?; + Err(DaemonError::from(format!("{}\n{}", stdout, stderr))) + } + } + + /// Runs a daemon process with the given args + fn run_daemon_process, I: IntoIterator>( + &self, + args: I, + ) -> DaemonResult { + Command::new(&self.daemon_path) + .args(args) + .spawn() + .map_err(DaemonError::from) + } +} diff --git a/mediarepo-api/src/daemon_management/error.rs b/mediarepo-api/src/daemon_management/error.rs new file mode 100644 index 0000000..6506695 --- /dev/null +++ b/mediarepo-api/src/daemon_management/error.rs @@ -0,0 +1,30 @@ +use std::fmt::{Display, Formatter}; + +pub type DaemonResult = Result; + +#[derive(Debug)] +pub struct DaemonError { + pub message: String, +} + +impl Display for DaemonError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.message.fmt(f) + } +} + +impl std::error::Error for DaemonError {} + +impl From for DaemonError { + fn from(e: std::io::Error) -> Self { + Self { + message: e.to_string(), + } + } +} + +impl From for DaemonError { + fn from(s: String) -> Self { + Self { message: s } + } +} diff --git a/mediarepo-api/src/daemon_management/mod.rs b/mediarepo-api/src/daemon_management/mod.rs new file mode 100644 index 0000000..0414a80 --- /dev/null +++ b/mediarepo-api/src/daemon_management/mod.rs @@ -0,0 +1,2 @@ +pub mod cli; +pub mod error; diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs index 1c1dbbe..0d2a12b 100644 --- a/mediarepo-api/src/lib.rs +++ b/mediarepo-api/src/lib.rs @@ -3,5 +3,8 @@ pub mod types; #[cfg(feature = "client-api")] pub mod client_api; +#[cfg(feature = "client-api")] +pub mod daemon_management; + #[cfg(feature = "tauri-plugin")] pub mod tauri_plugin; diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs new file mode 100644 index 0000000..14711b7 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -0,0 +1,25 @@ +use crate::client_api::ApiClient; +use crate::tauri_plugin::commands::AppAccess; +use crate::tauri_plugin::error::PluginResult; + +#[tauri::command] +pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { + let daemon = app_state.get_daemon_cli(repo_path).await; + daemon.init_repo().await?; + + Ok(()) +} + +#[tauri::command] +pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { + let mut daemon = app_state.get_daemon_cli(repo_path).await; + daemon.start_daemon()?; + app_state.add_started_daemon(daemon).await; + + Ok(()) +} + +#[tauri::command] +pub async fn check_daemon_running(address: String) -> PluginResult { + Ok(ApiClient::connect(&address).await.is_ok()) +} diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index 7e2cac7..42f6626 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -1,12 +1,14 @@ use parking_lot::lock_api::Mutex; use tauri::State; +pub use daemon::*; pub use file::*; pub use repo::*; pub use tag::*; use crate::tauri_plugin::state::{ApiState, AppState, BufferState, VolatileBuffer}; +pub mod daemon; pub mod file; pub mod repo; pub mod tag; diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index f5440fd..5abf4e3 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -1,4 +1,5 @@ use crate::client_api::error::ApiError; +use crate::daemon_management::error::DaemonError; use rmp_ipc::error::Error; use serde::Serialize; use std::fmt::{Display, Formatter}; @@ -67,3 +68,9 @@ impl From for PluginError { } } } + +impl From for PluginError { + fn from(e: DaemonError) -> Self { + Self { message: e.message } + } +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index d155002..5c13ce9 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -39,7 +39,10 @@ impl MediarepoPlugin { get_tags_for_file, get_active_repository, add_repository, - select_repository + select_repository, + init_repository, + start_daemon, + check_daemon_running, ]), } } @@ -61,14 +64,15 @@ impl Plugin for MediarepoPlugin { let buffer_state = BufferState::default(); app.manage(buffer_state.clone()); + + let repo_state = AppState::load()?; + app.manage(repo_state); + thread::spawn(move || loop { thread::sleep(Duration::from_secs(10)); buffer_state.clear_expired(); }); - let repo_state = AppState::load()?; - app.manage(repo_state); - Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 8fbcc68..4c1addd 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -14,11 +14,21 @@ pub struct Repository { pub(crate) address: String, } -#[derive(Default, Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct Settings { + pub daemon_path: String, pub repositories: HashMap, } +impl Default for Settings { + fn default() -> Self { + Self { + daemon_path: String::from("mediarepo-daemon"), + repositories: HashMap::new(), + } + } +} + fn get_settings_path() -> PathBuf { let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap(); let config_path = dirs.config_dir().to_path_buf(); diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 98dadb6..28bbb6e 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -9,6 +9,7 @@ use tauri::async_runtime::RwLock; use tokio::time::Instant; use crate::client_api::ApiClient; +use crate::daemon_management::cli::DaemonCli; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{load_settings, Repository, Settings}; @@ -117,6 +118,7 @@ impl BufferState { pub struct AppState { pub active_repo: Arc>>, pub settings: Arc>, + pub running_daemons: Arc>>, } impl AppState { @@ -125,10 +127,25 @@ impl AppState { let settings = load_settings()?; let state = Self { - active_repo: Arc::new(RwLock::new(None)), + active_repo: Default::default(), settings: Arc::new(RwLock::new(settings)), + running_daemons: Default::default(), }; Ok(state) } + + /// Returns the daemon cli client + pub async fn get_daemon_cli(&self, repo_path: String) -> DaemonCli { + let settings = self.settings.read().await; + let path = settings.daemon_path.clone(); + + DaemonCli::new(path, repo_path) + } + + /// Adds a started daemon to the running daemons + pub async fn add_started_daemon(&self, daemon: DaemonCli) { + let mut daemons = self.running_daemons.write().await; + daemons.insert(daemon.repo_path().to_owned(), daemon); + } } From 778bb2fe774da69a359080bfe92319098f26be63 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 1 Nov 2021 21:19:58 +0100 Subject: [PATCH 022/116] Fix command to check if a daemon is running Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/daemon.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 14711b7..2e90dfa 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -21,5 +21,9 @@ pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> Plugin #[tauri::command] pub async fn check_daemon_running(address: String) -> PluginResult { - Ok(ApiClient::connect(&address).await.is_ok()) + if let Ok(api_client) = ApiClient::connect(&address).await { + Ok(api_client.info().await.is_ok()) + } else { + Ok(false) + } } From 1dc4a32953ed2032d9d8566cb247b25a8bd7c2b0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 19:15:38 +0100 Subject: [PATCH 023/116] Change daemons to be killed on drop Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 1 + mediarepo-api/src/daemon_management/cli.rs | 13 +++++++++++++ mediarepo-api/src/tauri_plugin/commands/daemon.rs | 7 +++++++ mediarepo-api/src/tauri_plugin/mod.rs | 1 + mediarepo-api/src/tauri_plugin/settings.rs | 3 ++- mediarepo-api/src/tauri_plugin/state.rs | 11 +++++++++++ 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 41a3a71..2a06a8d 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -14,6 +14,7 @@ rmp-ipc = {version = "0.8.1", 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} +serde_piecewise_default = "0.2.0" [dependencies.serde] version = "1.0.130" diff --git a/mediarepo-api/src/daemon_management/cli.rs b/mediarepo-api/src/daemon_management/cli.rs index 215403f..e0baf9d 100644 --- a/mediarepo-api/src/daemon_management/cli.rs +++ b/mediarepo-api/src/daemon_management/cli.rs @@ -1,5 +1,6 @@ use crate::daemon_management::error::{DaemonError, DaemonResult}; use std::ffi::OsStr; +use std::mem; use tokio::process::{Child, Command}; #[derive(Debug)] @@ -38,6 +39,17 @@ impl DaemonCli { Ok(()) } + /// Kills the running daemon process if there's one associated with the + /// daemon cli + #[tracing::instrument] + pub async fn stop_daemon(&mut self) -> DaemonResult<()> { + if let Some(mut child) = mem::take(&mut self.child) { + child.kill().await?; + } + + Ok(()) + } + /// Returns if the daemon is currently running pub fn daemon_running(&mut self) -> bool { if let Some(child) = &mut self.child { @@ -76,6 +88,7 @@ impl DaemonCli { ) -> DaemonResult { Command::new(&self.daemon_path) .args(args) + .kill_on_drop(true) .spawn() .map_err(DaemonError::from) } diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 2e90dfa..9a6d658 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -19,6 +19,13 @@ pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> Plugin Ok(()) } +#[tauri::command] +pub async fn stop_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { + app_state.stop_running_daemon(&repo_path).await?; + + Ok(()) +} + #[tauri::command] pub async fn check_daemon_running(address: String) -> PluginResult { if let Ok(api_client) = ApiClient::connect(&address).await { diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 5c13ce9..072107f 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -43,6 +43,7 @@ impl MediarepoPlugin { init_repository, start_daemon, check_daemon_running, + stop_daemon, ]), } } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 4c1addd..9fa128e 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -1,6 +1,7 @@ use crate::tauri_plugin::error::PluginResult; use directories::ProjectDirs; use serde::{Deserialize, Serialize}; +use serde_piecewise_default::DeserializePiecewiseDefault; use std::collections::HashMap; use std::fs; use std::path::PathBuf; @@ -14,7 +15,7 @@ pub struct Repository { pub(crate) address: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(DeserializePiecewiseDefault, Debug, Serialize)] pub struct Settings { pub daemon_path: String, pub repositories: HashMap, diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 28bbb6e..489a24c 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -148,4 +148,15 @@ impl AppState { let mut daemons = self.running_daemons.write().await; daemons.insert(daemon.repo_path().to_owned(), daemon); } + + /// Tries to stop a running daemon + pub async fn stop_running_daemon(&self, repo_path: &String) -> PluginResult<()> { + let mut daemons = self.running_daemons.write().await; + + if let Some(mut daemon) = daemons.remove(repo_path) { + daemon.stop_daemon().await?; + } + + Ok(()) + } } From e54fda8f5cde4ad9c2d15b219dd11b3f504dd561 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 19:23:07 +0100 Subject: [PATCH 024/116] Change repo config to differentiate local and remote repositories Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 32 ++++++++++++++----- mediarepo-api/src/tauri_plugin/settings.rs | 3 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 2bd9cc8..d6aaa59 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -31,17 +31,21 @@ pub async fn get_active_repository(app_state: AppAccess<'_>) -> PluginResult, + address: Option, + local: bool, app_state: AppAccess<'_>, ) -> PluginResult> { - let repo_path = path.clone(); - let path = PathBuf::from(path); - let RepoConfig { listen_address, .. } = read_repo_config(path.join(REPO_CONFIG_FILE)).await?; - + if path.is_none() && address.is_none() { + return Err(PluginError::from( + "Either a path or an address needs to be specified for the repository", + )); + } let repo = Repository { name, - path: Some(repo_path), - address: listen_address, + path, + address, + local, }; let mut repositories = Vec::new(); @@ -65,7 +69,19 @@ pub async fn select_repository( let repo = settings.repositories.get(&name).ok_or(PluginError::from( format!("Repository '{}' not found", name).as_str(), ))?; - let client = ApiClient::connect(&repo.address).await?; + let address = if let Some(address) = &repo.address { + address.clone() + } else { + tracing::debug!("Reading repo address from config."); + let path = repo + .path + .clone() + .ok_or_else(|| PluginError::from("Missing repo path or address in config."))?; + let config = read_repo_config(PathBuf::from(path).join(REPO_CONFIG_FILE)).await?; + + config.listen_address + }; + let client = ApiClient::connect(&address).await?; api_state.set_api(client).await; let mut active_repo = app_state.active_repo.write().await; diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 9fa128e..be49830 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -12,7 +12,8 @@ static SETTINGS_FILE: &str = "settings.toml"; pub struct Repository { pub(crate) name: String, pub(crate) path: Option, - pub(crate) address: String, + pub(crate) address: Option, + pub(crate) local: bool, } #[derive(DeserializePiecewiseDefault, Debug, Serialize)] From 9b457564bbba67fb1978e1436b405d1db04b8012 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 19:38:48 +0100 Subject: [PATCH 025/116] Add API version validation Signed-off-by: trivernis --- mediarepo-api/src/client_api/error.rs | 7 +++- mediarepo-api/src/client_api/mod.rs | 13 +++++-- mediarepo-api/src/tauri_plugin/error.rs | 1 + mediarepo-api/src/types/misc.rs | 52 ++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/mediarepo-api/src/client_api/error.rs b/mediarepo-api/src/client_api/error.rs index bd8f8df..0e88754 100644 --- a/mediarepo-api/src/client_api/error.rs +++ b/mediarepo-api/src/client_api/error.rs @@ -5,5 +5,8 @@ pub type ApiResult = Result; #[derive(Debug, Error)] pub enum ApiError { #[error(transparent)] - IPC(#[from] rmp_ipc::error::Error) -} \ No newline at end of file + IPC(#[from] rmp_ipc::error::Error), + + #[error("The servers api version is incompatible with the api client")] + VersionMismatch, +} diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 3f27ac5..fb60401 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -2,10 +2,10 @@ pub mod error; pub mod file; pub mod tag; -use crate::client_api::error::ApiResult; +use crate::client_api::error::{ApiError, ApiResult}; use crate::client_api::file::FileApi; use crate::client_api::tag::TagApi; -use crate::types::misc::InfoResponse; +use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use async_trait::async_trait; use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; @@ -72,8 +72,15 @@ impl ApiClient { .address(address) .build_pooled_client(8) .await?; + let client = Self::new(ctx); + let info = client.info().await?; + let server_api_version = info.api_version(); - Ok(Self::new(ctx)) + if !check_apis_compatible(get_api_version(), server_api_version) { + Err(ApiError::VersionMismatch) + } else { + Ok(client) + } } /// Returns information about the connected ipc server diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 5abf4e3..1480239 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -40,6 +40,7 @@ impl From for PluginError { format!("{:?}", e) } }, + ApiError::VersionMismatch => {String::from("The servers API version is not supported by the client. Please make sure both are up to date.")} }; Self { message } } diff --git a/mediarepo-api/src/types/misc.rs b/mediarepo-api/src/types/misc.rs index 137524f..73ca70d 100644 --- a/mediarepo-api/src/types/misc.rs +++ b/mediarepo-api/src/types/misc.rs @@ -1,7 +1,55 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct InfoResponse { pub name: String, pub version: String, -} \ No newline at end of file + pub(crate) api_version: (u32, u32, u32), +} + +impl InfoResponse { + /// Creates a new info response + pub fn new(name: String, version: String) -> Self { + Self { + name, + version, + api_version: get_api_version(), + } + } + + /// Returns the api version of the crate + pub fn api_version(&self) -> (u32, u32, u32) { + self.api_version + } +} + +/// Retrieves the api version of the crate version in numbers +pub fn get_api_version() -> (u32, u32, u32) { + let mut major = env!("CARGO_PKG_VERSION_MAJOR").to_string(); + let mut minor = env!("CARGO_PKG_VERSION_MINOR").to_string(); + let mut patch = env!("CARGO_PKG_VERSION_PATCH").to_string(); + major.retain(char::is_numeric); + minor.retain(char::is_numeric); + patch.retain(char::is_numeric); + let major = major + .parse::() + .expect("Failed to parse major crate version"); + let minor = minor + .parse::() + .expect("Failed to parse minor crate version"); + let patch = patch + .parse::() + .expect("Failed to parse patch crate version"); + + (major, minor, patch) +} + +/// Checks if the api the client consumes is compatible to the one the server provides +pub fn check_apis_compatible( + client_version: (u32, u32, u32), + server_version: (u32, u32, u32), +) -> bool { + // the major version must be the same while the client minor version can be lower than the servers + // so that the client has access to all its supported functionality + client_version.0 == server_version.0 && client_version.1 <= server_version.1 +} From 455dd331f3867f73f70f2b58e1e265d362b5dd6d Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 20:32:00 +0100 Subject: [PATCH 026/116] Add commands to close and disconnect repositories Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 22 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 2 ++ mediarepo-api/src/tauri_plugin/state.rs | 11 ++++++++++ 3 files changed, 35 insertions(+) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index d6aaa59..2977030 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,6 +3,7 @@ use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; use serde::{Deserialize, Serialize}; +use std::mem; use std::path::PathBuf; use tokio::fs; @@ -59,6 +60,27 @@ pub async fn add_repository( Ok(repositories) } +#[tauri::command] +pub async fn disconnect_repository(api_state: ApiAccess<'_>) -> PluginResult<()> { + api_state.disconnect().await; + + Ok(()) +} + +#[tauri::command] +pub async fn close_local_repository( + app_state: AppAccess<'_>, + api_state: ApiAccess<'_>, +) -> PluginResult<()> { + let mut active_repo = app_state.active_repo.write().await; + if let Some(path) = mem::take(&mut *active_repo).and_then(|r| r.path) { + app_state.stop_running_daemon(&path).await?; + } + api_state.disconnect().await; + + Ok(()) +} + #[tauri::command] pub async fn select_repository( name: String, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 072107f..04825a5 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -44,6 +44,8 @@ impl MediarepoPlugin { start_daemon, check_daemon_running, stop_daemon, + disconnect_repository, + close_local_repository ]), } } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 489a24c..e59ea80 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -24,6 +24,7 @@ impl ApiState { } } + /// Sets the active api client and disconnects the old one pub async fn set_api(&self, client: ApiClient) { let mut inner = self.inner.write().await; let old_client = mem::replace(&mut *inner, Some(client)); @@ -33,6 +34,16 @@ impl ApiState { } } + /// Disconnects the api client + pub async fn disconnect(&self) { + let mut inner = self.inner.write().await; + let old_client = mem::take(&mut *inner); + + if let Some(client) = old_client { + let _ = client.exit().await; + } + } + pub async fn api(&self) -> PluginResult { let inner = self.inner.read().await; inner From 98539718c1c5b8d1c2e0502a1cff28fe2c0d5798 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 20:33:16 +0100 Subject: [PATCH 027/116] Fix disconnecting from a repository not removing the selected repository Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/repo.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 2977030..fa7854a 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -61,8 +61,13 @@ pub async fn add_repository( } #[tauri::command] -pub async fn disconnect_repository(api_state: ApiAccess<'_>) -> PluginResult<()> { +pub async fn disconnect_repository( + app_state: AppAccess<'_>, + api_state: ApiAccess<'_>, +) -> PluginResult<()> { api_state.disconnect().await; + let mut active_repo = app_state.active_repo.write().await; + mem::take(&mut *active_repo); Ok(()) } From b4a528e2bb33da2a3ef2e76fc254f1f127d02638 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 20:37:37 +0100 Subject: [PATCH 028/116] Change client online checking to use a non-pooled approach Signed-off-by: trivernis --- .../src/tauri_plugin/commands/daemon.rs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 9a6d658..bf96dd7 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -1,6 +1,7 @@ -use crate::client_api::ApiClient; use crate::tauri_plugin::commands::AppAccess; use crate::tauri_plugin::error::PluginResult; +use rmp_ipc::prelude::IPCResult; +use rmp_ipc::IPCBuilder; #[tauri::command] pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { @@ -28,9 +29,18 @@ pub async fn stop_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginR #[tauri::command] pub async fn check_daemon_running(address: String) -> PluginResult { - if let Ok(api_client) = ApiClient::connect(&address).await { - Ok(api_client.info().await.is_ok()) - } else { - Ok(false) - } + let connect_result = try_connect_daemon(address).await; + + Ok(connect_result.is_ok()) +} + +async fn try_connect_daemon(address: String) -> IPCResult<()> { + let ctx = IPCBuilder::new().address(address).build_client().await?; + ctx.emitter + .emit("info", ()) + .await? + .await_reply(&ctx) + .await?; + ctx.stop().await?; + Ok(()) } From 57ebbf87e0c6f55b067bf7b1580680b9148a2905 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 21:11:21 +0100 Subject: [PATCH 029/116] Add sorting of repositories by last opened Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 26 +++++++++++++++---- mediarepo-api/src/tauri_plugin/settings.rs | 1 + 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index fa7854a..db7e4f3 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -5,6 +5,7 @@ use crate::tauri_plugin::settings::{save_settings, Repository}; use serde::{Deserialize, Serialize}; use std::mem; use std::path::PathBuf; +use std::time::{SystemTime, UNIX_EPOCH}; use tokio::fs; static REPO_CONFIG_FILE: &str = "repo.toml"; @@ -19,8 +20,11 @@ pub struct RepoConfig { #[tauri::command] pub async fn get_repositories(app_state: AppAccess<'_>) -> PluginResult> { let settings = app_state.settings.read().await; + let mut repositories: Vec = settings.repositories.values().cloned().collect(); + repositories.sort_by_key(|r| r.last_opened.unwrap_or(0)); + repositories.reverse(); // the last opened repository should always be on top - Ok(settings.repositories.values().cloned().collect()) + Ok(repositories) } #[tauri::command] @@ -47,6 +51,7 @@ pub async fn add_repository( path, address, local, + last_opened: None, }; let mut repositories = Vec::new(); @@ -92,10 +97,13 @@ pub async fn select_repository( app_state: AppAccess<'_>, api_state: ApiAccess<'_>, ) -> PluginResult<()> { - let settings = app_state.settings.read().await; - let repo = settings.repositories.get(&name).ok_or(PluginError::from( - format!("Repository '{}' not found", name).as_str(), - ))?; + let mut settings = app_state.settings.write().await; + let repo = settings + .repositories + .get_mut(&name) + .ok_or(PluginError::from( + format!("Repository '{}' not found", name).as_str(), + ))?; let address = if let Some(address) = &repo.address { address.clone() } else { @@ -112,7 +120,15 @@ pub async fn select_repository( api_state.set_api(client).await; let mut active_repo = app_state.active_repo.write().await; + repo.last_opened = Some( + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis(), + ); + *active_repo = Some(repo.clone()); + save_settings(&settings)?; Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index be49830..0149e2a 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -14,6 +14,7 @@ pub struct Repository { pub(crate) path: Option, pub(crate) address: Option, pub(crate) local: bool, + pub(crate) last_opened: Option, } #[derive(DeserializePiecewiseDefault, Debug, Serialize)] From cadf866cd7ca8dcc25f282ffeec2972be86aa5a3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 21:19:38 +0100 Subject: [PATCH 030/116] Add more tracing Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 04825a5..846e695 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -79,6 +79,7 @@ impl Plugin for MediarepoPlugin { Ok(()) } + #[tracing::instrument(skip_all)] fn extend_api(&mut self, message: Invoke) { (self.invoke_handler)(message) } From 03718e9a6774470c723c0159f1150392746dbf5d Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 2 Nov 2021 21:34:21 +0100 Subject: [PATCH 031/116] Change last opened field to u64 Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/repo.rs | 2 +- mediarepo-api/src/tauri_plugin/settings.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index db7e4f3..245aa2e 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -124,7 +124,7 @@ pub async fn select_repository( SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() - .as_millis(), + .as_secs(), ); *active_repo = Some(repo.clone()); diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 0149e2a..7c1f53b 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -14,7 +14,7 @@ pub struct Repository { pub(crate) path: Option, pub(crate) address: Option, pub(crate) local: bool, - pub(crate) last_opened: Option, + pub(crate) last_opened: Option, } #[derive(DeserializePiecewiseDefault, Debug, Serialize)] From b91a3ab4d29ceb4b45efd3c792c65f2b3712cfda Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 3 Nov 2021 19:36:51 +0100 Subject: [PATCH 032/116] Add shutting down of daemons when the repository is changed Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/repo.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 245aa2e..0471d10 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -104,6 +104,15 @@ pub async fn select_repository( .ok_or(PluginError::from( format!("Repository '{}' not found", name).as_str(), ))?; + if let Some(path) = app_state + .active_repo + .read() + .await + .clone() + .and_then(|r| r.path) + { + app_state.stop_running_daemon(&path).await?; + } let address = if let Some(address) = &repo.address { address.clone() } else { From 55fc99bcce11cf6c0e0ec204b19eb1f3faff9d49 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 4 Nov 2021 19:18:17 +0100 Subject: [PATCH 033/116] Add commands to delete and check repositories Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 50 +++++++++++++++---- mediarepo-api/src/tauri_plugin/error.rs | 6 +++ mediarepo-api/src/tauri_plugin/mod.rs | 4 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 0471d10..015a351 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -65,6 +65,32 @@ pub async fn add_repository( Ok(repositories) } +#[tauri::command] +pub async fn remove_repository(app_state: AppAccess<'_>, name: String) -> PluginResult<()> { + let mut settings = app_state.settings.write().await; + + if let Some(_repo) = settings.repositories.remove(&name) { + save_settings(&settings)?; + Ok(()) + } else { + Err(PluginError::from(format!( + "The repository '{}' does not exist.", + name + ))) + } +} + +#[tauri::command] +pub async fn check_local_repository_exists(path: String) -> PluginResult { + let config_path = PathBuf::from(path).join(REPO_CONFIG_FILE); + + if !config_path.exists() { + Ok(false) + } else { + Ok(true) + } +} + #[tauri::command] pub async fn disconnect_repository( app_state: AppAccess<'_>, @@ -104,15 +130,7 @@ pub async fn select_repository( .ok_or(PluginError::from( format!("Repository '{}' not found", name).as_str(), ))?; - if let Some(path) = app_state - .active_repo - .read() - .await - .clone() - .and_then(|r| r.path) - { - app_state.stop_running_daemon(&path).await?; - } + close_selected_repository(&app_state).await?; let address = if let Some(address) = &repo.address { address.clone() } else { @@ -142,6 +160,20 @@ pub async fn select_repository( Ok(()) } +async fn close_selected_repository(app_state: &AppAccess<'_>) -> PluginResult<()> { + if let Some(path) = app_state + .active_repo + .read() + .await + .clone() + .and_then(|r| r.path) + { + app_state.stop_running_daemon(&path).await?; + } + + Ok(()) +} + async fn read_repo_config(path: PathBuf) -> PluginResult { let toml_str = fs::read_to_string(path).await?; let config = toml::from_str(&toml_str)?; diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 1480239..705dca3 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -27,6 +27,12 @@ impl From<&str> for PluginError { } } +impl From for PluginError { + fn from(message: String) -> Self { + Self { message } + } +} + impl From for PluginError { fn from(e: ApiError) -> Self { let message = match e { diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 846e695..f3848e8 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -45,7 +45,9 @@ impl MediarepoPlugin { check_daemon_running, stop_daemon, disconnect_repository, - close_local_repository + close_local_repository, + check_local_repository_exists, + remove_repository ]), } } From af104dedec788051253dfd85d10251093c3ea00e Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 4 Nov 2021 20:16:18 +0100 Subject: [PATCH 034/116] Add api to get tags for a list of file hashes Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/tag.rs | 9 ++++++++- mediarepo-api/src/types/files.rs | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 2a06a8d..a6488ae 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.1.0" +version = "0.2.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 05ceda6..bdc2165 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -1,6 +1,6 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; -use crate::types::files::GetFileTagsRequest; +use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; use crate::types::identifier::FileIdentifier; use crate::types::tags::TagResponse; use async_trait::async_trait; @@ -45,4 +45,11 @@ impl TagApi { ) .await } + + /// Returns a list of all tags that are assigned to the list of files + #[tracing::instrument(level = "debug", skip_all)] + pub async fn get_tags_for_files(&self, hashes: Vec) -> ApiResult> { + self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }) + .await + } } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index ad4e813..b8dcd1c 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -22,6 +22,11 @@ pub struct GetFileTagsRequest { pub id: FileIdentifier, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GetFilesTagsRequest { + pub hashes: Vec, +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FindFilesByTagsRequest { pub tags: Vec, From 209bc8a9d0068d4569f068beee1bee7827012019 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 4 Nov 2021 20:29:40 +0100 Subject: [PATCH 035/116] Add tauri command to get all tags for a list of files Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/tag.rs | 11 +++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 1 + 2 files changed, 12 insertions(+) diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index 6a9b11c..a9e2426 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -20,3 +20,14 @@ pub async fn get_tags_for_file( Ok(tags) } + +#[tauri::command] +pub async fn get_tags_for_files( + hashes: Vec, + api_state: ApiAccess<'_>, +) -> PluginResult> { + let api = api_state.api().await?; + let tags = api.tag.get_tags_for_files(hashes).await?; + + Ok(tags) +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index f3848e8..ac031fd 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -37,6 +37,7 @@ impl MediarepoPlugin { get_repositories, get_all_tags, get_tags_for_file, + get_tags_for_files, get_active_repository, add_repository, select_repository, From df2cbe94e0de9eebc3163dc0510d8e4330ce307f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 11:59:28 +0100 Subject: [PATCH 036/116] 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, +} From b402fab69753a3357505fc5cc84acb01846feec1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 13:37:14 +0100 Subject: [PATCH 037/116] Add api to update a files name Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 11 ++++++++++- mediarepo-api/src/tauri_plugin/commands/file.rs | 10 ++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- mediarepo-api/src/types/files.rs | 6 ++++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 2875353..5ee8df6 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.3.0" +version = "0.4.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 d49c86f..257c634 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -2,7 +2,7 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, - SortKey, TagQuery, ThumbnailMetadataResponse, + SortKey, TagQuery, ThumbnailMetadataResponse, UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -81,4 +81,13 @@ impl FileApi { let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?; Ok(payload.to_payload_bytes()?) } + + /// Updates a files name + #[tracing::instrument(level = "debug", skip(self))] + pub async fn update_file_name(&self, file_id: FileIdentifier, name: String) -> ApiResult<()> { + self.emit("update_file_name", UpdateFileNameRequest { file_id, name }) + .await?; + + Ok(()) + } } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 324e7c5..6a9f40d 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -70,3 +70,13 @@ pub async fn read_thumbnail( Ok(uri) } } + +#[tauri::command] +pub async fn update_file_name(api_state: ApiAccess<'_>, id: i64, name: String) -> PluginResult<()> { + let api = api_state.api().await?; + api.file + .update_file_name(FileIdentifier::ID(id), name) + .await?; + + Ok(()) +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 46b97cf..c97d739 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -50,7 +50,8 @@ impl MediarepoPlugin { check_local_repository_exists, remove_repository, change_file_tags, - create_tags + create_tags, + update_file_name ]), } } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index b8dcd1c..0760c80 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -86,3 +86,9 @@ pub struct ThumbnailMetadataResponse { pub width: i32, pub mime_type: Option, } + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct UpdateFileNameRequest { + pub file_id: FileIdentifier, + pub name: String, +} From 6929a88c922dc1ec557a1f609284fa13292ff385 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 13:41:30 +0100 Subject: [PATCH 038/116] Change update_file_name to expect a metadata response Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 5ee8df6..9df85dc 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.4.0" +version = "0.4.1" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 257c634..e3fb0ed 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -84,10 +84,12 @@ impl FileApi { /// Updates a files name #[tracing::instrument(level = "debug", skip(self))] - pub async fn update_file_name(&self, file_id: FileIdentifier, name: String) -> ApiResult<()> { - self.emit("update_file_name", UpdateFileNameRequest { file_id, name }) - .await?; - - Ok(()) + pub async fn update_file_name( + &self, + file_id: FileIdentifier, + name: String, + ) -> ApiResult { + self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name }) + .await } } From ee9d4b0312ebb02b2de93b32af8008ac5d13beb2 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 13:56:00 +0100 Subject: [PATCH 039/116] Fix command to update the file name returning nothing Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/file.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 6a9f40d..8d527d9 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -72,11 +72,16 @@ pub async fn read_thumbnail( } #[tauri::command] -pub async fn update_file_name(api_state: ApiAccess<'_>, id: i64, name: String) -> PluginResult<()> { +pub async fn update_file_name( + api_state: ApiAccess<'_>, + id: i64, + name: String, +) -> PluginResult { let api = api_state.api().await?; - api.file + let metadata = api + .file .update_file_name(FileIdentifier::ID(id), name) .await?; - Ok(()) + Ok(metadata) } From 5fee8a2e4a0886025120919d968b31c00d0dddd0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 6 Nov 2021 20:21:50 +0100 Subject: [PATCH 040/116] Improve display of version mismatch error Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/error.rs | 4 ++-- mediarepo-api/src/client_api/mod.rs | 10 +++++++++- mediarepo-api/src/tauri_plugin/error.rs | 4 +++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 9df85dc..9030736 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.4.1" +version = "0.4.2" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/error.rs b/mediarepo-api/src/client_api/error.rs index 0e88754..8634385 100644 --- a/mediarepo-api/src/client_api/error.rs +++ b/mediarepo-api/src/client_api/error.rs @@ -7,6 +7,6 @@ pub enum ApiError { #[error(transparent)] IPC(#[from] rmp_ipc::error::Error), - #[error("The servers api version is incompatible with the api client")] - VersionMismatch, + #[error("The servers api version (version {server:?}) is incompatible with the api client {client:?}")] + VersionMismatch { server: String, client: String }, } diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index fb60401..650c651 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -77,7 +77,15 @@ impl ApiClient { let server_api_version = info.api_version(); if !check_apis_compatible(get_api_version(), server_api_version) { - Err(ApiError::VersionMismatch) + let server_version_string = format!( + "{}.{}.{}", + server_api_version.0, server_api_version.1, server_api_version.2 + ); + let client_version_string = env!("CARGO_PKG_VERSION").to_string(); + Err(ApiError::VersionMismatch { + server: server_version_string, + client: client_version_string, + }) } else { Ok(client) } diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 705dca3..5547863 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -46,7 +46,9 @@ impl From for PluginError { format!("{:?}", e) } }, - ApiError::VersionMismatch => {String::from("The servers API version is not supported by the client. Please make sure both are up to date.")} + ApiError::VersionMismatch { server, client } => { + format!("The servers API version ({}) is not supported by the client ({}). Please make sure both are up to date.", server, client) + } }; Self { message } } From f48a9aad6408e507d1373a1b9b15be9b0ee8a88f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 15:42:53 +0100 Subject: [PATCH 041/116] Update rmp-ipc and implement wrapper protocol Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 32 +++- mediarepo-api/src/client_api/mod.rs | 38 ++-- mediarepo-api/src/client_api/protocol.rs | 177 ++++++++++++++++++ mediarepo-api/src/client_api/tag.rs | 31 ++- .../src/tauri_plugin/commands/daemon.rs | 24 ++- .../src/tauri_plugin/commands/repo.rs | 2 +- mediarepo-api/src/tauri_plugin/state.rs | 10 +- mediarepo-api/src/types/files.rs | 14 ++ 9 files changed, 297 insertions(+), 33 deletions(-) create mode 100644 mediarepo-api/src/client_api/protocol.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 9030736..42c9029 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -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.8.1", optional=true} +rmp-ipc = {version = "0.9.0", 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} diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index e3fb0ed..51904be 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -8,27 +8,43 @@ use crate::types::identifier::FileIdentifier; use async_trait::async_trait; use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::payload::{BytePayload, EventSendPayload}; -use rmp_ipc::prelude::Context; +use rmp_ipc::prelude::*; -#[derive(Clone)] -pub struct FileApi { - ctx: PooledContext, +pub struct FileApi { + ctx: PooledContext, +} + +impl Clone for FileApi +where + S: AsyncProtocolStream, +{ + fn clone(&self) -> Self { + Self { + ctx: self.ctx.clone(), + } + } } #[async_trait] -impl IPCApi for FileApi { +impl IPCApi for FileApi +where + S: AsyncProtocolStream, +{ fn namespace() -> &'static str { "files" } - fn ctx(&self) -> PoolGuard { + fn ctx(&self) -> PoolGuard> { self.ctx.acquire() } } -impl FileApi { +impl FileApi +where + S: AsyncProtocolStream, +{ /// Creates a new file api client - pub fn new(ctx: PooledContext) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 650c651..219acca 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,5 +1,6 @@ pub mod error; pub mod file; +pub mod protocol; pub mod tag; use crate::client_api::error::{ApiError, ApiResult}; @@ -11,13 +12,14 @@ use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; 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 { +pub trait IPCApi { fn namespace() -> &'static str; - fn ctx(&self) -> PoolGuard; + fn ctx(&self) -> PoolGuard>; async fn emit( &self, @@ -47,17 +49,31 @@ pub trait IPCApi { Ok(response.data()?) } } +pub struct ApiClient { + ctx: PooledContext, + pub file: FileApi, + pub tag: TagApi, +} -#[derive(Clone)] -pub struct ApiClient { - ctx: PooledContext, - pub file: FileApi, - pub tag: TagApi, +impl Clone for ApiClient +where + L: AsyncStreamProtocolListener, +{ + fn clone(&self) -> Self { + Self { + ctx: self.ctx.clone(), + file: self.file.clone(), + tag: self.tag.clone(), + } + } } -impl ApiClient { +impl ApiClient +where + L: AsyncStreamProtocolListener, +{ /// Creates a new client from an existing ipc context - pub fn new(ctx: PooledContext) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), @@ -67,8 +83,8 @@ impl ApiClient { /// Connects to the ipc Socket #[tracing::instrument(level = "debug")] - pub async fn connect(address: &str) -> ApiResult { - let ctx = IPCBuilder::new() + pub async fn connect(address: L::AddressType) -> ApiResult { + let ctx = IPCBuilder::::new() .address(address) .build_pooled_client(8) .await?; diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs new file mode 100644 index 0000000..90659e0 --- /dev/null +++ b/mediarepo-api/src/client_api/protocol.rs @@ -0,0 +1,177 @@ +use async_trait::async_trait; +use rmp_ipc::error::Result; +use rmp_ipc::prelude::IPCResult; +use rmp_ipc::protocol::*; +use std::io::Error; +use std::net::ToSocketAddrs; +use std::pin::Pin; +use std::task::{Context, Poll}; +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; +use tokio::net::{TcpListener, TcpStream}; + +#[derive(Debug)] +pub enum ApiProtocolListener { + #[cfg(unix)] + UnixSocket(tokio::net::UnixListener), + + Tcp(TcpListener), +} + +unsafe impl Send for ApiProtocolListener {} +unsafe impl Sync for ApiProtocolListener {} + +#[async_trait] +impl AsyncStreamProtocolListener for ApiProtocolListener { + type AddressType = String; + type RemoteAddressType = String; + type Stream = ApiProtocolStream; + + async fn protocol_bind(address: Self::AddressType) -> Result { + if let Some(addr) = address.to_socket_addrs().ok().and_then(|mut a| a.next()) { + let listener = TcpListener::bind(addr).await?; + Ok(Self::Tcp(listener)) + } else { + #[cfg(unix)] + { + use std::path::PathBuf; + use tokio::net::UnixListener; + let path = PathBuf::from(address); + let listener = UnixListener::bind(path)?; + + Ok(Self::UnixSocket(listener)) + } + #[cfg(not(unix))] + { + Err(IPCError::BuildError( + "The address can not be made into a socket address".to_string(), + )) + } + } + } + + async fn protocol_accept(&self) -> Result<(Self::Stream, Self::RemoteAddressType)> { + match self { + ApiProtocolListener::UnixSocket(listener) => { + let (stream, addr) = listener.accept().await?; + Ok(( + ApiProtocolStream::UnixSocket(stream), + addr.as_pathname() + .map(|p| p.to_str().unwrap().to_string()) + .unwrap_or(String::from("unknown")), + )) + } + ApiProtocolListener::Tcp(listener) => { + let (stream, addr) = listener.accept().await?; + Ok((ApiProtocolStream::Tcp(stream), addr.to_string())) + } + } + } +} + +#[derive(Debug)] +pub enum ApiProtocolStream { + #[cfg(unix)] + UnixSocket(tokio::net::UnixStream), + + Tcp(TcpStream), +} + +unsafe impl Send for ApiProtocolStream {} +unsafe impl Sync for ApiProtocolStream {} + +impl AsyncProtocolStreamSplit for ApiProtocolStream { + type OwnedSplitReadHalf = Box; + type OwnedSplitWriteHalf = Box; + + fn protocol_into_split(self) -> (Self::OwnedSplitReadHalf, Self::OwnedSplitWriteHalf) { + match self { + #[cfg(unix)] + ApiProtocolStream::UnixSocket(stream) => { + let (read, write) = stream.into_split(); + (Box::new(read), Box::new(write)) + } + ApiProtocolStream::Tcp(stream) => { + let (read, write) = stream.into_split(); + (Box::new(read), Box::new(write)) + } + } + } +} + +#[async_trait] +impl AsyncProtocolStream for ApiProtocolStream { + type AddressType = String; + + async fn protocol_connect(address: Self::AddressType) -> IPCResult { + if let Some(addr) = address.to_socket_addrs().ok().and_then(|mut a| a.next()) { + let stream = TcpStream::connect(addr).await?; + Ok(Self::Tcp(stream)) + } else { + #[cfg(unix)] + { + use std::path::PathBuf; + use tokio::net::UnixStream; + let path = PathBuf::from(address); + let stream = UnixStream::connect(path).await?; + + Ok(Self::UnixSocket(stream)) + } + #[cfg(not(unix))] + { + Err(IPCError::BuildError( + "The address can not be made into a socket address".to_string(), + )) + } + } + } +} + +impl AsyncRead for ApiProtocolStream { + fn poll_read( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut ReadBuf<'_>, + ) -> Poll> { + match self.get_mut() { + #[cfg(unix)] + ApiProtocolStream::UnixSocket(stream) => Pin::new(stream).poll_read(cx, buf), + ApiProtocolStream::Tcp(stream) => Pin::new(stream).poll_read(cx, buf), + } + } +} + +impl AsyncWrite for ApiProtocolStream { + fn poll_write( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + match self.get_mut() { + #[cfg(unix)] + ApiProtocolStream::UnixSocket(stream) => Pin::new(stream).poll_write(cx, buf), + ApiProtocolStream::Tcp(stream) => Pin::new(stream).poll_write(cx, buf), + } + } + + fn poll_flush( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + match self.get_mut() { + #[cfg(unix)] + ApiProtocolStream::UnixSocket(stream) => Pin::new(stream).poll_flush(cx), + ApiProtocolStream::Tcp(stream) => Pin::new(stream).poll_flush(cx), + } + } + + fn poll_shutdown( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + match self.get_mut() { + #[cfg(unix)] + ApiProtocolStream::UnixSocket(stream) => Pin::new(stream).poll_shutdown(cx), + ApiProtocolStream::Tcp(stream) => Pin::new(stream).poll_flush(cx), + } + } +} diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 4ae9d8e..0a77859 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -6,25 +6,42 @@ use crate::types::tags::{ChangeFileTagsRequest, TagResponse}; use async_trait::async_trait; use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; +use rmp_ipc::protocol::AsyncProtocolStream; -#[derive(Clone)] -pub struct TagApi { - ctx: PooledContext, +pub struct TagApi { + ctx: PooledContext, +} + +impl Clone for TagApi +where + S: AsyncProtocolStream, +{ + fn clone(&self) -> Self { + Self { + ctx: self.ctx.clone(), + } + } } #[async_trait] -impl IPCApi for TagApi { +impl IPCApi for TagApi +where + S: AsyncProtocolStream, +{ fn namespace() -> &'static str { "tags" } - fn ctx(&self) -> PoolGuard { + fn ctx(&self) -> PoolGuard> { self.ctx.acquire() } } -impl TagApi { - pub fn new(ctx: PooledContext) -> Self { +impl TagApi +where + S: AsyncProtocolStream, +{ + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index bf96dd7..5dc67f7 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -1,7 +1,10 @@ use crate::tauri_plugin::commands::AppAccess; use crate::tauri_plugin::error::PluginResult; -use rmp_ipc::prelude::IPCResult; +use rmp_ipc::prelude::{IPCError, IPCResult}; use rmp_ipc::IPCBuilder; +use std::io::ErrorKind; +use std::net::{SocketAddr, ToSocketAddrs}; +use tokio::net::TcpListener; #[tauri::command] pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { @@ -35,7 +38,11 @@ pub async fn check_daemon_running(address: String) -> PluginResult { } async fn try_connect_daemon(address: String) -> IPCResult<()> { - let ctx = IPCBuilder::new().address(address).build_client().await?; + let address = get_socket_address(address)?; + let ctx = IPCBuilder::::new() + .address(address) + .build_client() + .await?; ctx.emitter .emit("info", ()) .await? @@ -44,3 +51,16 @@ async fn try_connect_daemon(address: String) -> IPCResult<()> { ctx.stop().await?; Ok(()) } + +fn get_socket_address(address: String) -> IPCResult { + address + .to_socket_addrs() + .ok() + .and_then(|mut addr| addr.next()) + .ok_or_else(|| { + IPCError::IoError(std::io::Error::new( + ErrorKind::InvalidInput, + "Invalid Socket address", + )) + }) +} diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 015a351..fd39b4d 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -143,7 +143,7 @@ pub async fn select_repository( config.listen_address }; - let client = ApiClient::connect(&address).await?; + let client = ApiClient::connect(address).await?; api_state.set_api(client).await; let mut active_repo = app_state.active_repo.write().await; diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index e59ea80..dba0be4 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -8,15 +8,19 @@ use parking_lot::RwLock as ParkingRwLock; use tauri::async_runtime::RwLock; use tokio::time::Instant; +use crate::client_api::protocol::ApiProtocolListener; use crate::client_api::ApiClient; use crate::daemon_management::cli::DaemonCli; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{load_settings, Repository, Settings}; pub struct ApiState { - inner: Arc>>, + inner: Arc>>>, } +unsafe impl Send for ApiState {} +unsafe impl Sync for ApiState {} + impl ApiState { pub fn new() -> Self { Self { @@ -25,7 +29,7 @@ impl ApiState { } /// Sets the active api client and disconnects the old one - pub async fn set_api(&self, client: ApiClient) { + pub async fn set_api(&self, client: ApiClient) { let mut inner = self.inner.write().await; let old_client = mem::replace(&mut *inner, Some(client)); @@ -44,7 +48,7 @@ impl ApiState { } } - pub async fn api(&self) -> PluginResult { + pub async fn api(&self) -> PluginResult> { let inner = self.inner.read().await; inner .clone() diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 0760c80..c812672 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -17,6 +17,13 @@ pub struct GetFileThumbnailsRequest { pub id: FileIdentifier, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GetFileThumbnailOfSizeRequest { + pub id: FileIdentifier, + pub min_size: (u32, u32), + pub max_size: (u32, u32), +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GetFileTagsRequest { pub id: FileIdentifier, @@ -81,6 +88,7 @@ pub struct FileMetadataResponse { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThumbnailMetadataResponse { pub id: i64, + pub file_id: i64, pub hash: String, pub height: i32, pub width: i32, @@ -92,3 +100,9 @@ pub struct UpdateFileNameRequest { pub file_id: FileIdentifier, pub name: String, } + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ThumbnailFullResponse { + pub metadata: ThumbnailMetadataResponse, + pub data: Vec, +} From 32492afedf478e1c9b21b12c7e604cadfc8617f9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 15:56:47 +0100 Subject: [PATCH 042/116] Add api to request thumbnails of specific sizes Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 30 +++++++++++++++++++++++++--- mediarepo-api/src/client_api/mod.rs | 5 +---- mediarepo-api/src/types/files.rs | 7 ++----- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 42c9029..0ab3d9d 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.4.2" +version = "0.5.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 51904be..0e361a6 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -1,8 +1,9 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ - FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, - SortKey, TagQuery, ThumbnailMetadataResponse, UpdateFileNameRequest, + FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailOfSizeRequest, + GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, ThumbnailFullResponse, + ThumbnailMetadataResponse, UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -95,7 +96,30 @@ where #[tracing::instrument(level = "debug", skip(self))] pub async fn read_thumbnail(&self, hash: String) -> ApiResult> { let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?; - Ok(payload.to_payload_bytes()?) + Ok(payload.into_inner()) + } + + /// Returns a thumbnail of size that is within the specified range + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_thumbnail_of_size( + &self, + file_id: FileIdentifier, + min_size: (u32, u32), + max_size: (u32, u32), + ) -> ApiResult<(ThumbnailMetadataResponse, Vec)> { + let payload: ThumbnailFullResponse = self + .emit_and_get( + "get_thumbnail_of_size", + GetFileThumbnailOfSizeRequest { + id: file_id, + min_size, + max_size, + }, + ) + .await?; + let (metadata, bytes) = payload.into_inner(); + + Ok((metadata, bytes.into_inner())) } /// Updates a files name diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 219acca..36e4f66 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -35,10 +35,7 @@ pub trait IPCApi { Ok(meta) } - async fn emit_and_get< - T: EventSendPayload + Debug + Send, - R: EventReceivePayload + Debug + Send, - >( + async fn emit_and_get( &self, event_name: &str, data: T, diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index c812672..2bcf8f8 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,5 +1,6 @@ use crate::types::identifier::FileIdentifier; use chrono::NaiveDateTime; +use rmp_ipc::payload::{BytePayload, TandemPayload}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] @@ -101,8 +102,4 @@ pub struct UpdateFileNameRequest { pub name: String, } -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ThumbnailFullResponse { - pub metadata: ThumbnailMetadataResponse, - pub data: Vec, -} +pub type ThumbnailFullResponse = TandemPayload; From 2ac8cd165bfed785e35b19322c2f02836db33193 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 16:04:41 +0100 Subject: [PATCH 043/116] Fix compilation error without client-api feature Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 6 +++--- mediarepo-api/src/types/files.rs | 3 --- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 0ab3d9d..07998d7 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.5.0" +version = "0.5.1" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 0e361a6..d75b887 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -2,8 +2,8 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailOfSizeRequest, - GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, ThumbnailFullResponse, - ThumbnailMetadataResponse, UpdateFileNameRequest, + GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, ThumbnailMetadataResponse, + UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -107,7 +107,7 @@ where min_size: (u32, u32), max_size: (u32, u32), ) -> ApiResult<(ThumbnailMetadataResponse, Vec)> { - let payload: ThumbnailFullResponse = self + let payload: TandemPayload = self .emit_and_get( "get_thumbnail_of_size", GetFileThumbnailOfSizeRequest { diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 2bcf8f8..83609f8 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,6 +1,5 @@ use crate::types::identifier::FileIdentifier; use chrono::NaiveDateTime; -use rmp_ipc::payload::{BytePayload, TandemPayload}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] @@ -101,5 +100,3 @@ pub struct UpdateFileNameRequest { pub file_id: FileIdentifier, pub name: String, } - -pub type ThumbnailFullResponse = TandemPayload; From f397ad858fff66cbee5a49e245aa7675accd22dc Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 16:40:13 +0100 Subject: [PATCH 044/116] Change connection implementation to use the .tcp file in the repository Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index fd39b4d..86fbb6c 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -7,6 +7,7 @@ use std::mem; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use tokio::fs; +use tokio::time::Duration; static REPO_CONFIG_FILE: &str = "repo.toml"; @@ -134,14 +135,21 @@ pub async fn select_repository( let address = if let Some(address) = &repo.address { address.clone() } else { - tracing::debug!("Reading repo address from config."); + tracing::debug!("Reading repo address from local file."); let path = repo .path .clone() .ok_or_else(|| PluginError::from("Missing repo path or address in config."))?; - let config = read_repo_config(PathBuf::from(path).join(REPO_CONFIG_FILE)).await?; - - config.listen_address + let address_path = PathBuf::from(path).join(".tcp"); + let mut address = String::from("127.0.0.1:2400"); + for _ in 0..10 { + if address_path.exists() { + address = fs::read_to_string(address_path).await?; + break; + } + tokio::time::sleep(Duration::from_millis(250)).await; + } + address }; let client = ApiClient::connect(address).await?; api_state.set_api(client).await; @@ -173,10 +181,3 @@ async fn close_selected_repository(app_state: &AppAccess<'_>) -> PluginResult<() Ok(()) } - -async fn read_repo_config(path: PathBuf) -> PluginResult { - let toml_str = fs::read_to_string(path).await?; - let config = toml::from_str(&toml_str)?; - - Ok(config) -} From 89bf781bd9e590af3cbfa89b48a53569cf314cae Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 17:01:56 +0100 Subject: [PATCH 045/116] Add support for unix sockets Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 86fbb6c..cea86b0 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -140,16 +140,7 @@ pub async fn select_repository( .path .clone() .ok_or_else(|| PluginError::from("Missing repo path or address in config."))?; - let address_path = PathBuf::from(path).join(".tcp"); - let mut address = String::from("127.0.0.1:2400"); - for _ in 0..10 { - if address_path.exists() { - address = fs::read_to_string(address_path).await?; - break; - } - tokio::time::sleep(Duration::from_millis(250)).await; - } - address + get_repo_address(path).await? }; let client = ApiClient::connect(address).await?; api_state.set_api(client).await; @@ -168,6 +159,27 @@ pub async fn select_repository( Ok(()) } +async fn get_repo_address(path: String) -> PluginResult { + let tcp_path = PathBuf::from(&path).join("repo.tcp"); + let socket_path = PathBuf::from(&path).join("repo.socket"); + + let mut address = String::from("127.0.0.1:2400"); + for _ in 0..10 { + #[cfg(unix)] + if socket_path.exists() { + address = socket_path.to_str().unwrap().to_string(); + break; + } + if tcp_path.exists() { + address = fs::read_to_string(tcp_path).await?; + break; + } + tokio::time::sleep(Duration::from_millis(250)).await; + } + + Ok(address) +} + async fn close_selected_repository(app_state: &AppAccess<'_>) -> PluginResult<()> { if let Some(path) = app_state .active_repo From 5f14077feda9c5457117555d7a033cbc5a03f134 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 17:13:00 +0100 Subject: [PATCH 046/116] Add command to get thumbnails of a specific size Signed-off-by: trivernis --- mediarepo-api/src/client_api/protocol.rs | 4 ++++ .../src/tauri_plugin/commands/file.rs | 23 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index 90659e0..81bea70 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -26,9 +26,12 @@ impl AsyncStreamProtocolListener for ApiProtocolListener { type RemoteAddressType = String; type Stream = ApiProtocolStream; + #[tracing::instrument] async fn protocol_bind(address: Self::AddressType) -> Result { if let Some(addr) = address.to_socket_addrs().ok().and_then(|mut a| a.next()) { let listener = TcpListener::bind(addr).await?; + tracing::info!("Connecting via TCP"); + Ok(Self::Tcp(listener)) } else { #[cfg(unix)] @@ -37,6 +40,7 @@ impl AsyncStreamProtocolListener for ApiProtocolListener { use tokio::net::UnixListener; let path = PathBuf::from(address); let listener = UnixListener::bind(path)?; + tracing::info!("Connecting via unix domain socket"); Ok(Self::UnixSocket(listener)) } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 8d527d9..08ac34f 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -71,6 +71,29 @@ pub async fn read_thumbnail( } } +#[tauri::command] +pub async fn get_thumbnail_of_size( + api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, + file_id: i64, + min_size: (u32, u32), + max_size: (u32, u32), +) -> PluginResult { + let api = api_state.api().await?; + let (thumb, data) = api + .file + .get_thumbnail_of_size(FileIdentifier::ID(file_id), min_size, max_size) + .await?; + let uri = add_once_buffer( + buffer_state, + thumb.hash, + thumb.mime_type.unwrap_or(String::from("image/png")), + data, + ); + + Ok(uri) +} + #[tauri::command] pub async fn update_file_name( api_state: ApiAccess<'_>, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index c97d739..2a3ff0a 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -34,6 +34,7 @@ impl MediarepoPlugin { read_file_by_hash, get_file_thumbnails, read_thumbnail, + get_thumbnail_of_size, get_repositories, get_all_tags, get_tags_for_file, @@ -85,7 +86,7 @@ impl Plugin for MediarepoPlugin { Ok(()) } - #[tracing::instrument(skip_all)] + #[tracing::instrument(level = "trace", skip_all)] fn extend_api(&mut self, message: Invoke) { (self.invoke_handler)(message) } From 22bddaff52df8f9e04197f38cbaf3c8d8f7a8dc4 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 17:19:52 +0100 Subject: [PATCH 047/116] Fix configured default socket file Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/repo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index cea86b0..23dd053 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -161,7 +161,7 @@ pub async fn select_repository( async fn get_repo_address(path: String) -> PluginResult { let tcp_path = PathBuf::from(&path).join("repo.tcp"); - let socket_path = PathBuf::from(&path).join("repo.socket"); + let socket_path = PathBuf::from(&path).join("repo.sock"); let mut address = String::from("127.0.0.1:2400"); for _ in 0..10 { From a2d7617d948bbaec57c51f62cd17d69340232850 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 18:50:34 +0100 Subject: [PATCH 048/116] Add github action to build on multiple platforms Signed-off-by: trivernis --- mediarepo-api/.github/workflows/build.yml | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 mediarepo-api/.github/workflows/build.yml diff --git a/mediarepo-api/.github/workflows/build.yml b/mediarepo-api/.github/workflows/build.yml new file mode 100644 index 0000000..32d21f5 --- /dev/null +++ b/mediarepo-api/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: Build on multiple platforms + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +env: + CARGO_TERM_COLOR: always + +jobs: + + build: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + + - name: Cache build data + uses: actions/cache@v2 + with: + path: | + target + ~/.cargo/ + key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo- + - name: Build + run: cargo build --verbose \ No newline at end of file From d4069b14ae4a86bed5770121559b1444bf294622 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 18:53:10 +0100 Subject: [PATCH 049/116] Fix build compiler flags for windows Signed-off-by: trivernis --- mediarepo-api/src/client_api/protocol.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index 81bea70..543e66b 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use rmp_ipc::error::Result; +use rmp_ipc::prelude::IPCError; use rmp_ipc::prelude::IPCResult; use rmp_ipc::protocol::*; use std::io::Error; @@ -55,6 +56,7 @@ impl AsyncStreamProtocolListener for ApiProtocolListener { async fn protocol_accept(&self) -> Result<(Self::Stream, Self::RemoteAddressType)> { match self { + #[cfg(unix)] ApiProtocolListener::UnixSocket(listener) => { let (stream, addr) = listener.accept().await?; Ok(( From 9dcbf0efda48ca00757a43c5348de72a1278e69b Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 18:56:27 +0100 Subject: [PATCH 050/116] Add builds for all features to build workflow Signed-off-by: trivernis --- mediarepo-api/.github/workflows/build.yml | 8 +++++++- mediarepo-api/src/client_api/protocol.rs | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/.github/workflows/build.yml b/mediarepo-api/.github/workflows/build.yml index 32d21f5..a1afbce 100644 --- a/mediarepo-api/.github/workflows/build.yml +++ b/mediarepo-api/.github/workflows/build.yml @@ -30,4 +30,10 @@ jobs: restore-keys: | ${{ runner.os }}-cargo- - name: Build - run: cargo build --verbose \ No newline at end of file + run: cargo build + + - name: Build API + run: cargo build --features=client-api + + - name: Build Plugin + run: cargo build --features=tauri-plugin \ No newline at end of file diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index 543e66b..3efd2fa 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -1,6 +1,5 @@ use async_trait::async_trait; use rmp_ipc::error::Result; -use rmp_ipc::prelude::IPCError; use rmp_ipc::prelude::IPCResult; use rmp_ipc::protocol::*; use std::io::Error; @@ -47,6 +46,7 @@ impl AsyncStreamProtocolListener for ApiProtocolListener { } #[cfg(not(unix))] { + use rmp_ipc::prelude::IPCError; Err(IPCError::BuildError( "The address can not be made into a socket address".to_string(), )) From 560b63ede9f3f5e38eaae6f08d6e98494419ebc0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 18:59:07 +0100 Subject: [PATCH 051/116] Add missing import for windows build Signed-off-by: trivernis --- mediarepo-api/src/client_api/protocol.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index 3efd2fa..924ee65 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -124,6 +124,7 @@ impl AsyncProtocolStream for ApiProtocolStream { } #[cfg(not(unix))] { + use rmp_ipc::prelude::IPCError; Err(IPCError::BuildError( "The address can not be made into a socket address".to_string(), )) From c0ce49abc94592f0ef946a4c3195c82bd9cc80c2 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 19:06:44 +0100 Subject: [PATCH 052/116] Add installation task for missing dependencies on ubuntu to workflow Signed-off-by: trivernis --- mediarepo-api/.github/workflows/build.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mediarepo-api/.github/workflows/build.yml b/mediarepo-api/.github/workflows/build.yml index a1afbce..bbaa82d 100644 --- a/mediarepo-api/.github/workflows/build.yml +++ b/mediarepo-api/.github/workflows/build.yml @@ -29,6 +29,14 @@ jobs: key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} restore-keys: | ${{ runner.os }}-cargo- + + - name: Install OS-specific dependencies + uses: knicknic/os-specific-run@v1.0.3 + with: + linux: | + sudo apt update + sudo apt install libwebkit2gtk-4.0-dev libgtk-3-dev libappindicator3-dev -y + - name: Build run: cargo build From 144b95dcfd8848694300c4f8433011066b175275 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 9 Nov 2021 17:53:24 +0100 Subject: [PATCH 053/116] Add command to retrieve files for a list of paths Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 5 +- .../src/tauri_plugin/commands/file.rs | 86 ++++++++++++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 4 +- mediarepo-api/src/tauri_plugin/utils.rs | 12 +++ mediarepo-api/src/types/files.rs | 9 ++ 5 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 mediarepo-api/src/tauri_plugin/utils.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 07998d7..89ac073 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.5.1" +version = "0.5.2" edition = "2018" license = "gpl-3" @@ -14,6 +14,7 @@ rmp-ipc = {version = "0.9.0", 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} +mime_guess = {version = "2.0.3", optional=true} serde_piecewise_default = "0.2.0" [dependencies.serde] @@ -40,5 +41,5 @@ version = "0.5.8" optional = true [features] -tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories"] +tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess"] client-api = ["rmp-ipc", "async-trait", "tokio"] \ No newline at end of file diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 08ac34f..8ea6827 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,7 +1,13 @@ 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::tauri_plugin::utils::system_time_to_naive_date_time; +use crate::types::files::{ + FileMetadataResponse, FileOSMetadata, SortKey, TagQuery, ThumbnailMetadataResponse, +}; use crate::types::identifier::FileIdentifier; +use std::path::PathBuf; +use tokio::fs; +use tokio::fs::DirEntry; #[tauri::command] pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { @@ -108,3 +114,81 @@ pub async fn update_file_name( Ok(metadata) } + +#[tauri::command] +pub async fn resolve_paths_to_files(paths: Vec) -> PluginResult> { + let mut files = Vec::new(); + + for path in paths { + let path = PathBuf::from(path); + if path.exists() { + files.append(&mut resolve_path_to_files(path).await?); + } + } + + Ok(files) +} + +/// Resolves a path into several file metadata objects +async fn resolve_path_to_files(path: PathBuf) -> PluginResult> { + let mut files = Vec::new(); + + if path.is_dir() { + let mut read_dir = fs::read_dir(path).await?; + + while let Some(entry) = read_dir.next_entry().await? { + let subdir_entries = resolve_subdir(entry).await?; + for entry in subdir_entries { + let metadata = retrieve_file_information(entry.path()).await?; + files.push(metadata); + } + } + } else { + let metadata = retrieve_file_information(path).await?; + files.push(metadata); + } + + Ok(files) +} + +/// Iteratively resolves a directory into its sub components +async fn resolve_subdir(entry: DirEntry) -> PluginResult> { + let mut entries = vec![entry]; + + for i in 0..entries.len() { + let entry = &entries[i]; + + if entry.path().is_dir() { + let mut read_dir = fs::read_dir(entry.path()).await?; + while let Some(entry) = read_dir.next_entry().await? { + entries.push(entry); + } + } + } + + Ok(entries) +} + +/// Retrieves information about a path that MUST be a file and returns +/// metadata for it +async fn retrieve_file_information(path: PathBuf) -> PluginResult { + let mime = mime_guess::from_path(&path) + .first() + .ok_or_else(|| format!("Could not guess mime for file {:?}", path))?; + let metadata = fs::metadata(&path).await?; + let creation_time = metadata.created()?; + let change_time = metadata.modified()?; + let name = path + .file_name() + .ok_or_else(|| "Could not retrieve file name")?; + + let os_metadata = FileOSMetadata { + path: path.to_string_lossy().to_string(), + name: name.to_string_lossy().to_string(), + mime_type: mime.to_string(), + creation_time: system_time_to_naive_date_time(creation_time), + change_time: system_time_to_naive_date_time(change_time), + }; + + Ok(os_metadata) +} diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 2a3ff0a..004d6f4 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -12,6 +12,7 @@ pub mod custom_schemes; pub mod error; mod settings; mod state; +mod utils; use commands::*; @@ -52,7 +53,8 @@ impl MediarepoPlugin { remove_repository, change_file_tags, create_tags, - update_file_name + update_file_name, + resolve_paths_to_files ]), } } diff --git a/mediarepo-api/src/tauri_plugin/utils.rs b/mediarepo-api/src/tauri_plugin/utils.rs new file mode 100644 index 0000000..d3a4035 --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/utils.rs @@ -0,0 +1,12 @@ +use chrono::NaiveDateTime; +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Converts a system time timestamp to a NaiveDateTime object +pub fn system_time_to_naive_date_time(system_time: SystemTime) -> NaiveDateTime { + let epoch_duration = system_time.duration_since(UNIX_EPOCH).unwrap(); + + NaiveDateTime::from_timestamp( + epoch_duration.as_secs() as i64, + epoch_duration.subsec_nanos(), + ) +} diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 83609f8..3f03694 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -85,6 +85,15 @@ pub struct FileMetadataResponse { pub import_time: NaiveDateTime, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FileOSMetadata { + pub path: String, + pub name: String, + pub mime_type: String, + pub creation_time: NaiveDateTime, + pub change_time: NaiveDateTime, +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThumbnailMetadataResponse { pub id: i64, From 52aa9d4f20e4b60f3580667a4c302d72ea8c15cb Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 9 Nov 2021 19:13:14 +0100 Subject: [PATCH 054/116] Add tracing to file retrieval command Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/tauri_plugin/commands/file.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 89ac073..38db871 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.5.2" +version = "0.5.3" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 8ea6827..ffb1cc8 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -130,6 +130,7 @@ pub async fn resolve_paths_to_files(paths: Vec) -> PluginResult PluginResult> { let mut files = Vec::new(); @@ -152,6 +153,7 @@ async fn resolve_path_to_files(path: PathBuf) -> PluginResult PluginResult> { let mut entries = vec![entry]; @@ -171,6 +173,7 @@ async fn resolve_subdir(entry: DirEntry) -> PluginResult> { /// Retrieves information about a path that MUST be a file and returns /// metadata for it +#[tracing::instrument(level = "trace")] async fn retrieve_file_information(path: PathBuf) -> PluginResult { let mime = mime_guess::from_path(&path) .first() From 70ef6246a12f29918262e37a3b3277fd77cbd4c7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 9 Nov 2021 19:16:48 +0100 Subject: [PATCH 055/116] Make mime type for importing files optional Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/file.rs | 6 ++---- mediarepo-api/src/types/files.rs | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index ffb1cc8..538c92f 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -175,9 +175,7 @@ async fn resolve_subdir(entry: DirEntry) -> PluginResult> { /// metadata for it #[tracing::instrument(level = "trace")] async fn retrieve_file_information(path: PathBuf) -> PluginResult { - let mime = mime_guess::from_path(&path) - .first() - .ok_or_else(|| format!("Could not guess mime for file {:?}", path))?; + let mime = mime_guess::from_path(&path).first(); let metadata = fs::metadata(&path).await?; let creation_time = metadata.created()?; let change_time = metadata.modified()?; @@ -188,7 +186,7 @@ async fn retrieve_file_information(path: PathBuf) -> PluginResult, pub creation_time: NaiveDateTime, pub change_time: NaiveDateTime, } From 3ad889f7241375e1500674aeb8b36c630517f743 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 9 Nov 2021 19:27:09 +0100 Subject: [PATCH 056/116] Fix iterative folder resolving algorithm Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/file.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 538c92f..b46f295 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -157,7 +157,9 @@ async fn resolve_path_to_files(path: PathBuf) -> PluginResult PluginResult> { let mut entries = vec![entry]; - for i in 0..entries.len() { + let mut i = 0; + + while i < entries.len() { let entry = &entries[i]; if entry.path().is_dir() { @@ -166,6 +168,7 @@ async fn resolve_subdir(entry: DirEntry) -> PluginResult> { entries.push(entry); } } + i += 1; } Ok(entries) From 3f667b8d72da6428cafe16208de743a5f72ca74f Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 9 Nov 2021 20:00:49 +0100 Subject: [PATCH 057/116] Add api to add files Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 4 +- mediarepo-api/src/client_api/file.rs | 22 +++++++++-- mediarepo-api/src/client_api/mod.rs | 5 +-- .../src/tauri_plugin/commands/file.rs | 37 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 +- mediarepo-api/src/types/files.rs | 11 +++--- 6 files changed, 68 insertions(+), 14 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 38db871..adbb3fc 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -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} diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index d75b887..52a044b 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -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, + bytes: Vec, + ) -> ApiResult { + let payload = TandemPayload::new( + AddFileRequestHeader { metadata, tags }, + BytePayload::new(bytes), + ); + + self.emit_and_get("add_file", payload).await + } } diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 36e4f66..3360313 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -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 { fn namespace() -> &'static str; fn ctx(&self) -> PoolGuard>; - async fn emit( + async fn emit( &self, event_name: &str, data: T, @@ -35,7 +34,7 @@ pub trait IPCApi { Ok(meta) } - async fn emit_and_get( + async fn emit_and_get( &self, event_name: &str, data: T, diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index b46f295..a075c96 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -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> { let api = api_state.api().await?; @@ -17,6 +24,36 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult, + metadata: FileOSMetadata, + options: AddFileOptions, +) -> PluginResult { + 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::>(), + ); + } + } + + 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, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 004d6f4..50e88d1 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -54,7 +54,8 @@ impl MediarepoPlugin { change_file_tags, create_tags, update_file_name, - resolve_paths_to_files + resolve_paths_to_files, + add_local_file ]), } } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 6b89079..5bccab0 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -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, +} From 2a69c5b74886448728246f7e37c5df8a3380bff3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 11 Nov 2021 20:51:50 +0100 Subject: [PATCH 058/116] Add schema to retrieve file content and api to get a file by id Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 6 +- mediarepo-api/src/client_api/file.rs | 5 + .../src/tauri_plugin/custom_schemes.rs | 122 +++++++++++++++--- mediarepo-api/src/tauri_plugin/state.rs | 11 ++ 4 files changed, 121 insertions(+), 23 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index adbb3fc..113ce18 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.6.0" +version = "0.7.0" edition = "2018" license = "gpl-3" @@ -16,6 +16,8 @@ serde_json = {version="1.0.68", optional=true} directories = {version="4.0.1", optional=true} mime_guess = {version = "2.0.3", optional=true} serde_piecewise_default = "0.2.0" +futures = {version = "0.3.17", optional=true} +url = {version = "2.2.2", optional=true } [dependencies.serde] version = "1.0.130" @@ -41,5 +43,5 @@ version = "0.5.8" optional = true [features] -tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess"] +tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess", "futures", "url"] client-api = ["rmp-ipc", "async-trait", "tokio"] \ No newline at end of file diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 52a044b..27f7657 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -55,6 +55,11 @@ where self.emit_and_get("all_files", ()).await } + /// Returns a file by identifier + pub async fn get_file(&self, id: FileIdentifier) -> ApiResult { + self.emit_and_get("get_file", id).await + } + /// Searches for a file by a list of tags #[tracing::instrument(level = "debug", skip(self))] pub async fn find_files( diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index a5b4bd2..8d87c95 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -1,24 +1,104 @@ -use crate::tauri_plugin::state::BufferState; -use tauri::http::ResponseBuilder; -use tauri::{Builder, Manager, Runtime}; +use crate::tauri_plugin::state::{ApiState, BufferState}; +use crate::types::identifier::FileIdentifier; +use std::borrow::Cow; +use std::collections::HashMap; +use tauri::http::{Request, Response, ResponseBuilder}; +use tauri::{AppHandle, Builder, Manager, Runtime}; +use url::Url; + +type Result = std::result::Result>; pub fn register_custom_uri_schemes(builder: Builder) -> Builder { - builder.register_uri_scheme_protocol("once", |app, request| { - let buf_state = app.state::(); - let resource_key = request.uri().trim_start_matches("once://"); - - let buffer = buf_state.get_entry(resource_key); - - if let Some(buffer) = buffer { - ResponseBuilder::new() - .mimetype(&buffer.mime) - .status(200) - .body(buffer.buf) - } else { - ResponseBuilder::new() - .mimetype("text/plain") - .status(404) - .body("Resource not found".as_bytes().to_vec()) - } - }) + builder + .register_uri_scheme_protocol("once", once_scheme) + .register_uri_scheme_protocol("content", content_scheme) + .register_uri_scheme_protocol("thumb", thumb_scheme) +} + +fn once_scheme(app: &AppHandle, request: &Request) -> Result { + let buf_state = app.state::(); + let resource_key = request.uri().trim_start_matches("once://"); + + let buffer = buf_state.get_entry(resource_key); + + if let Some(buffer) = buffer { + ResponseBuilder::new() + .mimetype(&buffer.mime) + .status(200) + .body(buffer.buf) + } else { + ResponseBuilder::new() + .mimetype("text/plain") + .status(404) + .body("Resource not found".as_bytes().to_vec()) + } +} + +fn content_scheme(app: &AppHandle, request: &Request) -> Result { + let api_state = app.state::(); + let buf_state = app.state::(); + let hash = request.uri().trim_start_matches("content://"); + + if let Some(buffer) = buf_state.get_entry(hash) { + ResponseBuilder::new() + .status(200) + .mimetype(&buffer.mime) + .body(buffer.buf) + } else { + let api = api_state.api_sync()?; + let file = + futures::executor::block_on(api.file.get_file(FileIdentifier::Hash(hash.to_string())))?; + let mime = file.mime_type.unwrap_or("image/png".to_string()); + let bytes = futures::executor::block_on( + api.file + .read_file_by_hash(FileIdentifier::Hash(hash.to_string())), + )?; + buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone()); + ResponseBuilder::new() + .mimetype(&mime) + .status(200) + .body(bytes) + } +} + +fn thumb_scheme(app: &AppHandle, request: &Request) -> Result { + let api_state = app.state::(); + let buf_state = app.state::(); + + let url = Url::parse(request.uri())?; + let hash = url.path(); + + let query_pairs = url + .query_pairs() + .collect::, Cow<'_, str>>>(); + + let height = query_pairs + .get("height") + .and_then(|h| h.parse::().ok()) + .unwrap_or(250); + + let width = query_pairs + .get("width") + .and_then(|w| w.parse::().ok()) + .unwrap_or(250); + + if let Some(buffer) = buf_state.get_entry(hash) { + ResponseBuilder::new() + .status(200) + .mimetype(&buffer.mime) + .body(buffer.buf) + } else { + let api = api_state.api_sync()?; + let (thumb, bytes) = futures::executor::block_on(api.file.get_thumbnail_of_size( + FileIdentifier::Hash(hash.to_string()), + ((height as f32 * 0.8) as u32, (width as f32 * 0.8) as u32), + ((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32), + ))?; + let mime = thumb.mime_type.unwrap_or(String::from("image/png")); + buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone()); + ResponseBuilder::new() + .mimetype(&mime) + .status(200) + .body(bytes) + } } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index dba0be4..821975b 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -54,6 +54,10 @@ impl ApiState { .clone() .ok_or_else(|| PluginError::from("Not connected")) } + + pub fn api_sync(&self) -> PluginResult> { + futures::executor::block_on(self.api()) + } } #[derive(Clone)] @@ -79,6 +83,13 @@ pub struct BufferState { } impl BufferState { + /// Adds a cached buffer to the buffer state + pub fn add_entry(&self, key: String, mime: String, bytes: Vec) { + let mut buffers = self.buffer.write(); + let buffer = VolatileBuffer::new(mime, bytes); + buffers.insert(key, Mutex::new(buffer)); + } + /// Checks if an entry for the specific key exists and resets /// its state so that it can safely be accessed again. pub fn reserve_entry(&self, key: &String) -> bool { From 68b8aa39c9b7971910b5b9dcf714ac8d5442dfde Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 11 Nov 2021 21:02:12 +0100 Subject: [PATCH 059/116] Fix error when parsing hashes from uris Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 8d87c95..d61eaae 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -1,3 +1,4 @@ +use crate::tauri_plugin::error::PluginError; use crate::tauri_plugin::state::{ApiState, BufferState}; use crate::types::identifier::FileIdentifier; use std::borrow::Cow; @@ -66,7 +67,9 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result(); let url = Url::parse(request.uri())?; - let hash = url.path(); + let hash = url + .domain() + .ok_or_else(|| PluginError::from("Missing Domain"))?; let query_pairs = url .query_pairs() From 5d510cbf1554c71979185f9ce91f432d2036f95f Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 11 Nov 2021 21:06:08 +0100 Subject: [PATCH 060/116] Fix caching key for thumbnails Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index d61eaae..c2913fe 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -85,7 +85,7 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result().ok()) .unwrap_or(250); - if let Some(buffer) = buf_state.get_entry(hash) { + if let Some(buffer) = buf_state.get_entry(request.uri()) { ResponseBuilder::new() .status(200) .mimetype(&buffer.mime) @@ -98,7 +98,7 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result Date: Mon, 15 Nov 2021 20:10:41 +0100 Subject: [PATCH 061/116] Change thumbnail response to not include the thumb id Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 7 --- .../src/tauri_plugin/commands/file.rs | 62 +------------------ .../src/tauri_plugin/commands/mod.rs | 19 +----- .../src/tauri_plugin/custom_schemes.rs | 1 + mediarepo-api/src/tauri_plugin/mod.rs | 3 - mediarepo-api/src/tauri_plugin/state.rs | 15 ----- mediarepo-api/src/types/files.rs | 2 - 8 files changed, 4 insertions(+), 107 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 113ce18..4793bf5 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.7.0" +version = "0.8.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 27f7657..dba44f3 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -97,13 +97,6 @@ where .await } - /// Reads the thumbnail of the file and returns its contents in bytes - #[tracing::instrument(level = "debug", skip(self))] - pub async fn read_thumbnail(&self, hash: String) -> ApiResult> { - let payload: BytePayload = self.emit_and_get("read_thumbnail", hash).await?; - Ok(payload.into_inner()) - } - /// Returns a thumbnail of size that is within the specified range #[tracing::instrument(level = "debug", skip(self))] pub async fn get_thumbnail_of_size( diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index a075c96..0d4116c 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,4 +1,4 @@ -use crate::tauri_plugin::commands::{add_once_buffer, ApiAccess, BufferAccess}; +use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ @@ -66,25 +66,6 @@ pub async fn find_files( Ok(files) } -#[tauri::command] -pub async fn read_file_by_hash( - 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(FileIdentifier::ID(id)).await?; - let uri = add_once_buffer(buffer_state, hash, mime_type, content); - - Ok(uri) - } -} - #[tauri::command] pub async fn get_file_thumbnails( api_state: ApiAccess<'_>, @@ -96,47 +77,6 @@ pub async fn get_file_thumbnails( Ok(thumbs) } -#[tauri::command] -pub async fn read_thumbnail( - hash: String, - mime_type: String, - api_state: ApiAccess<'_>, - buffer_state: BufferAccess<'_>, -) -> 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_thumbnail(hash.clone()).await?; - let uri = add_once_buffer(buffer_state, hash, mime_type, content); - - Ok(uri) - } -} - -#[tauri::command] -pub async fn get_thumbnail_of_size( - api_state: ApiAccess<'_>, - buffer_state: BufferAccess<'_>, - file_id: i64, - min_size: (u32, u32), - max_size: (u32, u32), -) -> PluginResult { - let api = api_state.api().await?; - let (thumb, data) = api - .file - .get_thumbnail_of_size(FileIdentifier::ID(file_id), min_size, max_size) - .await?; - let uri = add_once_buffer( - buffer_state, - thumb.hash, - thumb.mime_type.unwrap_or(String::from("image/png")), - data, - ); - - Ok(uri) -} - #[tauri::command] pub async fn update_file_name( api_state: ApiAccess<'_>, diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index 42f6626..106a4d2 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -1,4 +1,3 @@ -use parking_lot::lock_api::Mutex; use tauri::State; pub use daemon::*; @@ -6,7 +5,7 @@ pub use file::*; pub use repo::*; pub use tag::*; -use crate::tauri_plugin::state::{ApiState, AppState, BufferState, VolatileBuffer}; +use crate::tauri_plugin::state::{ApiState, AppState}; pub mod daemon; pub mod file; @@ -14,20 +13,4 @@ pub mod repo; pub mod tag; pub type ApiAccess<'a> = State<'a, ApiState>; -pub type BufferAccess<'a> = State<'a, BufferState>; pub type AppAccess<'a> = State<'a, AppState>; - -/// Adds a once-buffer to the buffer store -fn add_once_buffer( - buffer_state: BufferAccess<'_>, - key: String, - mime: String, - buf: Vec, -) -> String { - let uri = format!("once://{}", key); - let once_buffer = VolatileBuffer::new(mime, buf); - let mut once_buffers = buffer_state.buffer.write(); - once_buffers.insert(key, Mutex::new(once_buffer)); - - uri -} diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index c2913fe..0200e79 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -99,6 +99,7 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result MediarepoPlugin { invoke_handler: Box::new(tauri::generate_handler![ get_all_files, find_files, - read_file_by_hash, get_file_thumbnails, - read_thumbnail, - get_thumbnail_of_size, get_repositories, get_all_tags, get_tags_for_file, diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 821975b..27afd8b 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -90,21 +90,6 @@ impl BufferState { buffers.insert(key, Mutex::new(buffer)); } - /// Checks if an entry for the specific key exists and resets - /// its state so that it can safely be accessed again. - pub fn reserve_entry(&self, key: &String) -> bool { - let buffers = self.buffer.read(); - let entry = buffers.get(key); - - if let Some(entry) = entry { - let mut entry = entry.lock(); - entry.valid_until = Instant::now() + Duration::from_secs(120); // reset the timer so that it can be accessed again - true - } else { - false - } - } - /// Returns the cloned buffer entry and flags it for expiration pub fn get_entry(&self, key: &str) -> Option { let buffers = self.buffer.read(); diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 5bccab0..ab27f11 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -91,9 +91,7 @@ pub struct FileOSMetadata { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThumbnailMetadataResponse { - pub id: i64, pub file_id: i64, - pub hash: String, pub height: i32, pub width: i32, pub mime_type: Option, From 14397dd0379bad1026b71ed96a236dd04908e7c6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 15 Nov 2021 20:28:37 +0100 Subject: [PATCH 062/116] Change thumbnail requirements to be stricter Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 9 ++++++--- mediarepo-api/src/types/files.rs | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 4793bf5..5eaf5f0 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.8.0" +version = "0.9.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 0200e79..0b8ef4a 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -97,11 +97,14 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result, + pub height: u32, + pub width: u32, + pub mime_type: String, } #[derive(Clone, Debug, Serialize, Deserialize)] From 131a46816c2597459e82c280533c59558cb18a3e Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 15 Nov 2021 21:01:49 +0100 Subject: [PATCH 063/116] Change thumbnail to store the file hash instead to be even faster Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/types/files.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 5eaf5f0..4701a7f 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.9.0" +version = "0.10.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 6561002..f1ac76e 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -91,7 +91,7 @@ pub struct FileOSMetadata { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThumbnailMetadataResponse { - pub file_id: i64, + pub file_hash: String, pub height: u32, pub width: u32, pub mime_type: String, From 3dbd15e102d6944e123020f6b0c52f3d244bdb18 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 Nov 2021 19:10:28 +0100 Subject: [PATCH 064/116] Fix issues with file import This commit fixes the resolving of directories that sometimes still contain folders, the mapping of the created and modified time as well as the deletion of files after import if the deletion flag was set. Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 0d4116c..ab829e0 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -7,6 +7,7 @@ use crate::types::files::{ use crate::types::identifier::FileIdentifier; use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use std::time::SystemTime; use tokio::fs; use tokio::fs::DirEntry; @@ -33,12 +34,11 @@ pub async fn add_local_file( let api = api_state.api().await?; let path = PathBuf::from(&metadata.path); let mut tags = Vec::new(); + let txt_path = PathBuf::from(format!("{}.txt", path.to_string_lossy())); 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?; + let content = fs::read_to_string(&txt_path).await?; tags.append( &mut content .split('\n') @@ -48,8 +48,15 @@ pub async fn add_local_file( } } - let file_content = fs::read(path).await?; + let file_content = fs::read(&path).await?; let file = api.file.add_file(metadata, tags, file_content).await?; + if options.delete_after_import { + fs::remove_file(path).await?; + + if options.read_tags_from_txt { + fs::remove_file(txt_path).await?; + } + } Ok(file) } @@ -116,7 +123,7 @@ async fn resolve_path_to_files(path: PathBuf) -> PluginResult PluginResult> { async fn retrieve_file_information(path: PathBuf) -> PluginResult { let mime = mime_guess::from_path(&path).first(); let metadata = fs::metadata(&path).await?; - let creation_time = metadata.created()?; - let change_time = metadata.modified()?; + let creation_time = metadata.created().unwrap_or(SystemTime::now()); + let change_time = metadata.modified().unwrap_or(SystemTime::now()); let name = path .file_name() .ok_or_else(|| "Could not retrieve file name")?; From a2a6b858f0776f8b75b67cecc4b5652cbc785724 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 Nov 2021 20:30:47 +0100 Subject: [PATCH 065/116] Update rmp-ipc and add timeout Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/mod.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 4701a7f..755fafd 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -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.2", optional=true} +rmp-ipc = {version = "0.10.0", 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} diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 3360313..13de291 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -14,6 +14,7 @@ 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::time::Duration; #[async_trait] pub trait IPCApi { @@ -82,6 +83,7 @@ where pub async fn connect(address: L::AddressType) -> ApiResult { let ctx = IPCBuilder::::new() .address(address) + .timeout(Duration::from_secs(10)) .build_pooled_client(8) .await?; let client = Self::new(ctx); From 03f70e2f8a804d77e8a6fd678abbb76d22f09c82 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 Nov 2021 20:45:14 +0100 Subject: [PATCH 066/116] Change custom schemes to run in tokio runtime Signed-off-by: trivernis --- .../src/tauri_plugin/custom_schemes.rs | 55 +++++++++++++------ mediarepo-api/src/tauri_plugin/state.rs | 4 -- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 0b8ef4a..2998d93 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -1,10 +1,11 @@ -use crate::tauri_plugin::error::PluginError; +use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::state::{ApiState, BufferState}; use crate::types::identifier::FileIdentifier; use std::borrow::Cow; use std::collections::HashMap; use tauri::http::{Request, Response, ResponseBuilder}; use tauri::{AppHandle, Builder, Manager, Runtime}; +use tokio::runtime::{Builder as TokioRuntimeBuilder, Runtime as TokioRuntime}; use url::Url; type Result = std::result::Result>; @@ -12,8 +13,21 @@ type Result = std::result::Result>; pub fn register_custom_uri_schemes(builder: Builder) -> Builder { builder .register_uri_scheme_protocol("once", once_scheme) - .register_uri_scheme_protocol("content", content_scheme) - .register_uri_scheme_protocol("thumb", thumb_scheme) + .register_uri_scheme_protocol("content", |a, r| { + build_uri_runtime()?.block_on(content_scheme(a, r)) + }) + .register_uri_scheme_protocol("thumb", |a, r| { + build_uri_runtime()?.block_on(thumb_scheme(a, r)) + }) +} + +fn build_uri_runtime() -> PluginResult { + let runtime = TokioRuntimeBuilder::new_current_thread() + .thread_name("custom-scheme") + .max_blocking_threads(1) + .build()?; + + Ok(runtime) } fn once_scheme(app: &AppHandle, request: &Request) -> Result { @@ -35,7 +49,7 @@ fn once_scheme(app: &AppHandle, request: &Request) -> Result(app: &AppHandle, request: &Request) -> Result { +async fn content_scheme(app: &AppHandle, request: &Request) -> Result { let api_state = app.state::(); let buf_state = app.state::(); let hash = request.uri().trim_start_matches("content://"); @@ -46,14 +60,16 @@ fn content_scheme(app: &AppHandle, request: &Request) -> Result(app: &AppHandle, request: &Request) -> Result(app: &AppHandle, request: &Request) -> Result { +async fn thumb_scheme(app: &AppHandle, request: &Request) -> Result { let api_state = app.state::(); let buf_state = app.state::(); @@ -91,12 +107,15 @@ fn thumb_scheme(app: &AppHandle, request: &Request) -> Result PluginResult> { - futures::executor::block_on(self.api()) - } } #[derive(Clone)] From e1aceced33eee4ce98dfc3faae11e32fcda14f9d Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 Nov 2021 20:48:17 +0100 Subject: [PATCH 067/116] Fix tokio runtime in custom schemes not having time enabled Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 2998d93..af553a1 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -24,6 +24,7 @@ pub fn register_custom_uri_schemes(builder: Builder) -> Builder PluginResult { let runtime = TokioRuntimeBuilder::new_current_thread() .thread_name("custom-scheme") + .enable_all() .max_blocking_threads(1) .build()?; From e240d5f1ad2391e586f25e70ff2838a309ab6bd8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 Nov 2021 20:56:14 +0100 Subject: [PATCH 068/116] Change scheme runtimes to use one shared runtime instead of creating a new one Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index af553a1..2e9f154 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -3,6 +3,7 @@ use crate::tauri_plugin::state::{ApiState, BufferState}; use crate::types::identifier::FileIdentifier; use std::borrow::Cow; use std::collections::HashMap; +use std::sync::Arc; use tauri::http::{Request, Response, ResponseBuilder}; use tauri::{AppHandle, Builder, Manager, Runtime}; use tokio::runtime::{Builder as TokioRuntimeBuilder, Runtime as TokioRuntime}; @@ -11,21 +12,21 @@ use url::Url; type Result = std::result::Result>; pub fn register_custom_uri_schemes(builder: Builder) -> Builder { + let runtime = + Arc::new(build_uri_runtime().expect("Failed to build async runtime for custom schemes")); builder .register_uri_scheme_protocol("once", once_scheme) - .register_uri_scheme_protocol("content", |a, r| { - build_uri_runtime()?.block_on(content_scheme(a, r)) - }) - .register_uri_scheme_protocol("thumb", |a, r| { - build_uri_runtime()?.block_on(thumb_scheme(a, r)) + .register_uri_scheme_protocol("content", { + let runtime = Arc::clone(&runtime); + move |a, r| runtime.block_on(content_scheme(a, r)) }) + .register_uri_scheme_protocol("thumb", move |a, r| runtime.block_on(thumb_scheme(a, r))) } fn build_uri_runtime() -> PluginResult { let runtime = TokioRuntimeBuilder::new_current_thread() .thread_name("custom-scheme") .enable_all() - .max_blocking_threads(1) .build()?; Ok(runtime) From a5403588cd612cc4f114960ff6ce7da2c26bc472 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 18 Nov 2021 19:06:59 +0100 Subject: [PATCH 069/116] Change find file filter to support or-expressions Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 10 +++++----- mediarepo-api/src/tauri_plugin/commands/file.rs | 6 +++--- mediarepo-api/src/types/files.rs | 13 ++++++++++--- mediarepo-api/src/types/misc.rs | 11 ++++++++--- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 755fafd..32ecb6f 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.10.0" +version = "0.11.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 dba44f3..a4e0cc6 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -1,8 +1,8 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ - AddFileRequestHeader, FileMetadataResponse, FileOSMetadata, FindFilesByTagsRequest, - GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, SortKey, TagQuery, + AddFileRequestHeader, FileMetadataResponse, FileOSMetadata, FilterExpression, FindFilesRequest, + GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, SortKey, ThumbnailMetadataResponse, UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; @@ -64,13 +64,13 @@ where #[tracing::instrument(level = "debug", skip(self))] pub async fn find_files( &self, - tags: Vec, + filters: Vec, sort_expression: Vec, ) -> ApiResult> { self.emit_and_get( "find_files", - FindFilesByTagsRequest { - tags, + FindFilesRequest { + filters, sort_expression, }, ) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index ab829e0..822510d 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -2,7 +2,7 @@ use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ - FileMetadataResponse, FileOSMetadata, SortKey, TagQuery, ThumbnailMetadataResponse, + FileMetadataResponse, FileOSMetadata, FilterExpression, SortKey, ThumbnailMetadataResponse, }; use crate::types::identifier::FileIdentifier; use serde::{Deserialize, Serialize}; @@ -63,12 +63,12 @@ pub async fn add_local_file( #[tauri::command] pub async fn find_files( - tags: Vec, + filters: Vec, sort_by: Vec, api_state: ApiAccess<'_>, ) -> PluginResult> { let api = api_state.api().await?; - let files = api.file.find_files(tags, sort_by).await?; + let files = api.file.find_files(filters, sort_by).await?; Ok(files) } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index f1ac76e..cf68886 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -30,15 +30,22 @@ pub struct GetFilesTagsRequest { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct FindFilesByTagsRequest { - pub tags: Vec, +pub struct FindFilesRequest { + pub filters: Vec, pub sort_expression: Vec, } +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(tag = "filter_type")] +pub enum FilterExpression { + OrExpression(Vec), + Query(TagQuery), +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TagQuery { pub negate: bool, - pub name: String, + pub tags: String, } #[derive(Clone, Debug, Serialize, Deserialize)] diff --git a/mediarepo-api/src/types/misc.rs b/mediarepo-api/src/types/misc.rs index 73ca70d..cf04ed8 100644 --- a/mediarepo-api/src/types/misc.rs +++ b/mediarepo-api/src/types/misc.rs @@ -49,7 +49,12 @@ pub fn check_apis_compatible( client_version: (u32, u32, u32), server_version: (u32, u32, u32), ) -> bool { - // the major version must be the same while the client minor version can be lower than the servers - // so that the client has access to all its supported functionality - client_version.0 == server_version.0 && client_version.1 <= server_version.1 + if client_version.0 == 0 { + // For the beta and alpha (major 0) compare the minor and patch level + client_version.1 == server_version.1 && client_version.2 <= server_version.2 + } else { + // the major version must be the same while the client minor version can be lower than the servers + // so that the client has access to all its supported functionality + client_version.0 == server_version.0 && client_version.1 <= server_version.1 + } } From 59b014bc3f999ef59846dca1a57e2cb3afedbbcd Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 18 Nov 2021 19:19:31 +0100 Subject: [PATCH 070/116] Change misleading name 'tags' on tagquery to 'tag Signed-off-by: trivernis --- mediarepo-api/src/types/files.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index cf68886..45ee684 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -45,7 +45,7 @@ pub enum FilterExpression { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TagQuery { pub negate: bool, - pub tags: String, + pub tag: String, } #[derive(Clone, Debug, Serialize, Deserialize)] From 0534e543aca2d172474648f2525f4be749b38608 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 18 Nov 2021 19:46:53 +0100 Subject: [PATCH 071/116] Change tagging of FilterExpression to adjacent Signed-off-by: trivernis --- mediarepo-api/src/types/files.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 45ee684..66110f3 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -36,7 +36,7 @@ pub struct FindFilesRequest { } #[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(tag = "filter_type")] +#[serde(tag = "filter_type", content = "filter")] pub enum FilterExpression { OrExpression(Vec), Query(TagQuery), From 9411cbd1ab50ba3cdc5cf1ec8b018ae03a8026ed Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 20 Nov 2021 15:47:56 +0100 Subject: [PATCH 072/116] Add plugin command to save files on the local system Signed-off-by: trivernis --- mediarepo-api/src/client_api/file.rs | 2 +- mediarepo-api/src/tauri_plugin/commands/file.rs | 14 ++++++++++++++ mediarepo-api/src/tauri_plugin/custom_schemes.rs | 2 +- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index a4e0cc6..8940ba7 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -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> { + pub async fn read_file(&self, id: FileIdentifier) -> ApiResult> { let payload: BytePayload = self .emit_and_get("read_file", ReadFileRequest { id }) .await?; diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 822510d..6d68922 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -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) -> PluginResult> { let mut files = Vec::new(); diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 2e9f154..29695eb 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -70,7 +70,7 @@ async fn content_scheme(app: &AppHandle, 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() diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 58b8800..55d62e8 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -52,7 +52,8 @@ impl MediarepoPlugin { create_tags, update_file_name, resolve_paths_to_files, - add_local_file + add_local_file, + save_file_locally ]), } } From cccc5ac7130033311616d1034cd8560c641f1af5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 20 Nov 2021 18:09:42 +0100 Subject: [PATCH 073/116] Add api to delete thumbnails for a file Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 8 ++++++++ mediarepo-api/src/tauri_plugin/commands/file.rs | 8 ++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 32ecb6f..a49d486 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.11.0" +version = "0.11.1" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 8940ba7..624ffe9 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -146,4 +146,12 @@ where self.emit_and_get("add_file", payload).await } + + /// Deletes all thumbnails of a file to regenerate them when requested + #[tracing::instrument(level = "debug", skip(self))] + pub async fn delete_thumbnails(&self, file_id: FileIdentifier) -> ApiResult<()> { + self.emit("delete_thumbnails", file_id).await?; + + Ok(()) + } } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 6d68922..543efec 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -113,6 +113,14 @@ pub async fn save_file_locally( Ok(()) } +#[tauri::command] +pub async fn delete_thumbnail(api_state: ApiAccess<'_>, id: i64) -> PluginResult<()> { + let api = api_state.api().await?; + api.file.delete_thumbnails(FileIdentifier::ID(id)).await?; + + Ok(()) +} + #[tauri::command] pub async fn resolve_paths_to_files(paths: Vec) -> PluginResult> { let mut files = Vec::new(); diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 55d62e8..b63f7a4 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -53,7 +53,8 @@ impl MediarepoPlugin { update_file_name, resolve_paths_to_files, add_local_file, - save_file_locally + save_file_locally, + delete_thumbnail ]), } } From e4ba7f26746475faf5731c79a612311fbcbe2554 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 20 Nov 2021 18:23:55 +0100 Subject: [PATCH 074/116] Rename delete_thumbnail command to delete_thumbnails Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/file.rs | 2 +- mediarepo-api/src/tauri_plugin/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 543efec..799701b 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -114,7 +114,7 @@ pub async fn save_file_locally( } #[tauri::command] -pub async fn delete_thumbnail(api_state: ApiAccess<'_>, id: i64) -> PluginResult<()> { +pub async fn delete_thumbnails(api_state: ApiAccess<'_>, id: i64) -> PluginResult<()> { let api = api_state.api().await?; api.file.delete_thumbnails(FileIdentifier::ID(id)).await?; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index b63f7a4..1d5915b 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -54,7 +54,7 @@ impl MediarepoPlugin { resolve_paths_to_files, add_local_file, save_file_locally, - delete_thumbnail + delete_thumbnails ]), } } From ba04083d2cce2d782ad7379d05a936d7aa86f507 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Nov 2021 14:56:29 +0100 Subject: [PATCH 075/116] Update rmp-ipc and add more tracing Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 4 ++-- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index a49d486..86f77d5 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.11.1" +version = "0.12.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.10.0", optional=true} +rmp-ipc = {version = "0.11.0", 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} diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 29695eb..d02f25f 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -32,6 +32,7 @@ fn build_uri_runtime() -> PluginResult { Ok(runtime) } +#[tracing::instrument(level = "debug", skip_all)] fn once_scheme(app: &AppHandle, request: &Request) -> Result { let buf_state = app.state::(); let resource_key = request.uri().trim_start_matches("once://"); @@ -51,17 +52,20 @@ fn once_scheme(app: &AppHandle, request: &Request) -> Result(app: &AppHandle, request: &Request) -> Result { let api_state = app.state::(); let buf_state = app.state::(); let hash = request.uri().trim_start_matches("content://"); if let Some(buffer) = buf_state.get_entry(hash) { + tracing::debug!("Fetching content from cache"); ResponseBuilder::new() .status(200) .mimetype(&buffer.mime) .body(buffer.buf) } else { + tracing::debug!("Fetching content from daemon"); let api = api_state.api().await?; let file = api .file @@ -72,6 +76,7 @@ async fn content_scheme(app: &AppHandle, request: &Request) -> Re .file .read_file(FileIdentifier::Hash(hash.to_string())) .await?; + tracing::debug!("Received {} content bytes", bytes.len()); buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone()); ResponseBuilder::new() .mimetype(&mime) @@ -80,6 +85,7 @@ async fn content_scheme(app: &AppHandle, request: &Request) -> Re } } +#[tracing::instrument(level = "debug", skip_all)] async fn thumb_scheme(app: &AppHandle, request: &Request) -> Result { let api_state = app.state::(); let buf_state = app.state::(); @@ -104,11 +110,13 @@ async fn thumb_scheme(app: &AppHandle, request: &Request) -> Resu .unwrap_or(250); if let Some(buffer) = buf_state.get_entry(request.uri()) { + tracing::debug!("Fetching content from cache"); ResponseBuilder::new() .status(200) .mimetype(&buffer.mime) .body(buffer.buf) } else { + tracing::debug!("Fetching content from daemon"); let api = api_state.api().await?; let (thumb, bytes) = api .file @@ -118,6 +126,7 @@ async fn thumb_scheme(app: &AppHandle, request: &Request) -> Resu ((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32), ) .await?; + tracing::debug!("Received {} content bytes", bytes.len()); buf_state.add_entry( request.uri().to_string(), thumb.mime_type.clone(), From f82bd68b44d344095c3e15cee6edc3462634b3e1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Nov 2021 16:16:40 +0100 Subject: [PATCH 076/116] Reintroduce command to get the contents of a file Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 23 ++++++++++++++++++- .../src/tauri_plugin/commands/mod.rs | 3 ++- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 799701b..98753bd 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -1,4 +1,4 @@ -use crate::tauri_plugin::commands::ApiAccess; +use crate::tauri_plugin::commands::{ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ @@ -99,6 +99,27 @@ pub async fn update_file_name( Ok(metadata) } +#[tauri::command] +pub async fn read_file( + api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, + hash: String, + mime_type: String, +) -> PluginResult> { + if let Some(buffer) = buffer_state.get_entry(&hash) { + Ok(buffer.buf) + } else { + let api = api_state.api().await?; + let content = api + .file + .read_file(FileIdentifier::Hash(hash.clone())) + .await?; + buffer_state.add_entry(hash, mime_type, content.clone()); + + Ok(content) + } +} + /// Saves a file on the local system #[tauri::command] pub async fn save_file_locally( diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index 106a4d2..d90e9ff 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -5,7 +5,7 @@ pub use file::*; pub use repo::*; pub use tag::*; -use crate::tauri_plugin::state::{ApiState, AppState}; +use crate::tauri_plugin::state::{ApiState, AppState, BufferState}; pub mod daemon; pub mod file; @@ -14,3 +14,4 @@ pub mod tag; pub type ApiAccess<'a> = State<'a, ApiState>; pub type AppAccess<'a> = State<'a, AppState>; +pub type BufferAccess<'a> = State<'a, BufferState>; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 1d5915b..97bc43b 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -54,7 +54,8 @@ impl MediarepoPlugin { resolve_paths_to_files, add_local_file, save_file_locally, - delete_thumbnails + delete_thumbnails, + read_file ]), } } From a713c20aa45cf3ba1803ea59f2e991c23d267dae Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 28 Nov 2021 16:48:04 +0100 Subject: [PATCH 077/116] Add size limit to cache buffer Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 2 - mediarepo-api/src/tauri_plugin/mod.rs | 3 ++ mediarepo-api/src/tauri_plugin/state.rs | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 98753bd..f3d1c20 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -104,7 +104,6 @@ pub async fn read_file( api_state: ApiAccess<'_>, buffer_state: BufferAccess<'_>, hash: String, - mime_type: String, ) -> PluginResult> { if let Some(buffer) = buffer_state.get_entry(&hash) { Ok(buffer.buf) @@ -114,7 +113,6 @@ pub async fn read_file( .file .read_file(FileIdentifier::Hash(hash.clone())) .await?; - buffer_state.add_entry(hash, mime_type, content.clone()); Ok(content) } diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 97bc43b..c7b06b5 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -16,6 +16,8 @@ mod utils; use commands::*; +const MAX_BUFFER_SIZE: usize = 2 * 1024 * 1024 * 1024; + pub fn register_plugin(builder: Builder) -> Builder { let repo_plugin = MediarepoPlugin::new(); @@ -84,6 +86,7 @@ impl Plugin for MediarepoPlugin { thread::spawn(move || loop { thread::sleep(Duration::from_secs(10)); buffer_state.clear_expired(); + buffer_state.trim_to_size(MAX_BUFFER_SIZE); }); Ok(()) diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 6df107d..890437a 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::mem; +use std::ops::Deref; use std::sync::Arc; use std::time::Duration; @@ -120,6 +121,42 @@ impl BufferState { } } } + + /// Trims the buffer to the given target size + pub fn trim_to_size(&self, target_size: usize) { + let mut size = self.get_size(); + if size < target_size { + return; + } + + let mut keys: Vec = { + let buffer = self.buffer.read(); + buffer.keys().cloned().collect() + }; + keys.reverse(); + + while size > target_size && keys.len() > 0 { + let key = keys.pop().unwrap(); + let mut buffers = self.buffer.write(); + + if let Some(entry) = buffers.remove(&key) { + size -= entry.lock().buf.len(); + } + } + } + + /// Calculates the size of the whole buffer + pub fn get_size(&self) -> usize { + let buffer = self.buffer.read(); + let mut size = 0; + + for value in buffer.deref().values() { + let value = value.lock(); + size += value.buf.len(); + } + + size + } } pub struct AppState { From 8218ae323583a05a2d47b14a39f96a1d18ff1b3a Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 30 Nov 2021 19:31:22 +0100 Subject: [PATCH 078/116] Add command to delete local repositories TG-25 Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 23 +++++++++++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 23dd053..b17f625 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -81,6 +81,29 @@ pub async fn remove_repository(app_state: AppAccess<'_>, name: String) -> Plugin } } +#[tauri::command] +pub async fn delete_repository(app_state: AppAccess<'_>, name: String) -> PluginResult<()> { + let settings = app_state.settings.read().await; + + if let Some(repository) = settings.repositories.get(&name) { + if let Some(path) = &repository.path { + fs::remove_dir_all(PathBuf::from(path)).await?; + + Ok(()) + } else { + Err(PluginError::from(format!( + "The repository '{}' is a remote repository", + name + ))) + } + } else { + Err(PluginError::from(format!( + "The repository '{}' does not exist.", + name + ))) + } +} + #[tauri::command] pub async fn check_local_repository_exists(path: String) -> PluginResult { let config_path = PathBuf::from(path).join(REPO_CONFIG_FILE); diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index c7b06b5..73110f7 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -57,7 +57,8 @@ impl MediarepoPlugin { add_local_file, save_file_locally, delete_thumbnails, - read_file + read_file, + delete_repository ]), } } From f78986328738114f3593091fd0c7eeeb7e602b4f Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 6 Dec 2021 20:55:17 +0100 Subject: [PATCH 079/116] Update rmp-ipc to bromine and reexport it Signed-off-by: trivernis --- mediarepo-api/.idea/discord.xml | 2 +- mediarepo-api/Cargo.toml | 12 ++-- mediarepo-api/src/client_api/error.rs | 2 +- mediarepo-api/src/client_api/file.rs | 35 +++++------- mediarepo-api/src/client_api/mod.rs | 56 +++++++------------ mediarepo-api/src/client_api/protocol.rs | 6 +- mediarepo-api/src/client_api/tag.rs | 28 +++------- mediarepo-api/src/lib.rs | 2 + .../src/tauri_plugin/commands/daemon.rs | 10 +--- .../src/tauri_plugin/commands/repo.rs | 3 +- mediarepo-api/src/tauri_plugin/error.rs | 2 +- mediarepo-api/src/tauri_plugin/state.rs | 7 +-- 12 files changed, 66 insertions(+), 99 deletions(-) diff --git a/mediarepo-api/.idea/discord.xml b/mediarepo-api/.idea/discord.xml index 30bab2a..d8e9561 100644 --- a/mediarepo-api/.idea/discord.xml +++ b/mediarepo-api/.idea/discord.xml @@ -1,7 +1,7 @@ - \ No newline at end of file diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 86f77d5..8e684e1 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.12.0" +version = "0.13.0" edition = "2018" license = "gpl-3" @@ -10,7 +10,6 @@ license = "gpl-3" tracing = "0.1.29" thiserror = "1.0.30" async-trait = {version = "0.1.51", optional=true} -rmp-ipc = {version = "0.11.0", 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} @@ -19,6 +18,11 @@ serde_piecewise_default = "0.2.0" futures = {version = "0.3.17", optional=true} url = {version = "2.2.2", optional=true } +[dependencies.bromine] +version = "0.16.1" +optional = true +features = ["serialize_bincode"] + [dependencies.serde] version = "1.0.130" features = ["serde_derive"] @@ -43,5 +47,5 @@ version = "0.5.8" optional = true [features] -tauri-plugin = ["client-api","tauri", "rmp-ipc", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess", "futures", "url"] -client-api = ["rmp-ipc", "async-trait", "tokio"] \ No newline at end of file +tauri-plugin = ["client-api","tauri", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess", "futures", "url"] +client-api = ["bromine", "async-trait", "tokio"] \ No newline at end of file diff --git a/mediarepo-api/src/client_api/error.rs b/mediarepo-api/src/client_api/error.rs index 8634385..f5e9e05 100644 --- a/mediarepo-api/src/client_api/error.rs +++ b/mediarepo-api/src/client_api/error.rs @@ -5,7 +5,7 @@ pub type ApiResult = Result; #[derive(Debug, Error)] pub enum ApiError { #[error(transparent)] - IPC(#[from] rmp_ipc::error::Error), + IPC(#[from] bromine::error::Error), #[error("The servers api version (version {server:?}) is incompatible with the api client {client:?}")] VersionMismatch { server: String, client: String }, diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 624ffe9..419289f 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -7,18 +7,15 @@ use crate::types::files::{ }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; -use rmp_ipc::context::{PoolGuard, PooledContext}; -use rmp_ipc::payload::{BytePayload, EventSendPayload}; -use rmp_ipc::prelude::*; +use bromine::context::{PoolGuard, PooledContext}; +use bromine::payload::BytePayload; +use bromine::prelude::*; -pub struct FileApi { - ctx: PooledContext, +pub struct FileApi { + ctx: PooledContext, } -impl Clone for FileApi -where - S: AsyncProtocolStream, -{ +impl Clone for FileApi { fn clone(&self) -> Self { Self { ctx: self.ctx.clone(), @@ -27,25 +24,19 @@ where } #[async_trait] -impl IPCApi for FileApi -where - S: AsyncProtocolStream, -{ +impl IPCApi for FileApi { fn namespace() -> &'static str { "files" } - fn ctx(&self) -> PoolGuard> { + fn ctx(&self) -> PoolGuard { self.ctx.acquire() } } -impl FileApi -where - S: AsyncProtocolStream, -{ +impl FileApi { /// Creates a new file api client - pub fn new(ctx: PooledContext) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } @@ -84,7 +75,7 @@ where .emit_and_get("read_file", ReadFileRequest { id }) .await?; - Ok(payload.to_payload_bytes()?) + Ok(payload.into_inner()) } /// Returns a list of all thumbnails of the file @@ -105,7 +96,7 @@ where min_size: (u32, u32), max_size: (u32, u32), ) -> ApiResult<(ThumbnailMetadataResponse, Vec)> { - let payload: TandemPayload = self + let payload: TandemPayload, BytePayload> = self .emit_and_get( "get_thumbnail_of_size", GetFileThumbnailOfSizeRequest { @@ -117,7 +108,7 @@ where .await?; let (metadata, bytes) = payload.into_inner(); - Ok((metadata, bytes.into_inner())) + Ok((metadata.data(), bytes.into_inner())) } /// Updates a files name diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 13de291..43b577b 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -8,34 +8,27 @@ use crate::client_api::file::FileApi; use crate::client_api::tag::TagApi; use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use async_trait::async_trait; -use rmp_ipc::context::{PoolGuard, PooledContext}; -use rmp_ipc::ipc::context::Context; -use rmp_ipc::ipc::stream_emitter::EmitMetadata; -use rmp_ipc::payload::{EventReceivePayload, EventSendPayload}; -use rmp_ipc::prelude::{AsyncProtocolStream, AsyncStreamProtocolListener}; -use rmp_ipc::IPCBuilder; +use bromine::ipc::stream_emitter::EmitMetadata; +use bromine::prelude::*; use std::time::Duration; #[async_trait] -pub trait IPCApi { +pub trait IPCApi { fn namespace() -> &'static str; - fn ctx(&self) -> PoolGuard>; + fn ctx(&self) -> PoolGuard; - async fn emit( + async fn emit( &self, event_name: &str, data: T, ) -> ApiResult { let ctx = self.ctx(); - let meta = ctx - .emitter - .emit_to(Self::namespace(), event_name, data) - .await?; + let meta = ctx.emit_to(Self::namespace(), event_name, data).await?; Ok(meta) } - async fn emit_and_get( + async fn emit_and_get( &self, event_name: &str, data: T, @@ -43,19 +36,16 @@ pub trait IPCApi { let meta = self.emit(event_name, data).await?; let response = meta.await_reply(&self.ctx()).await?; - Ok(response.data()?) + Ok(response.payload()?) } } -pub struct ApiClient { - ctx: PooledContext, - pub file: FileApi, - pub tag: TagApi, +pub struct ApiClient { + ctx: PooledContext, + pub file: FileApi, + pub tag: TagApi, } -impl Clone for ApiClient -where - L: AsyncStreamProtocolListener, -{ +impl Clone for ApiClient { fn clone(&self) -> Self { Self { ctx: self.ctx.clone(), @@ -65,12 +55,9 @@ where } } -impl ApiClient -where - L: AsyncStreamProtocolListener, -{ +impl ApiClient { /// Creates a new client from an existing ipc context - pub fn new(ctx: PooledContext) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), @@ -80,7 +67,9 @@ where /// Connects to the ipc Socket #[tracing::instrument(level = "debug")] - pub async fn connect(address: L::AddressType) -> ApiResult { + pub async fn connect( + address: L::AddressType, + ) -> ApiResult { let ctx = IPCBuilder::::new() .address(address) .timeout(Duration::from_secs(10)) @@ -109,13 +98,8 @@ where #[tracing::instrument(level = "debug", skip(self))] pub async fn info(&self) -> ApiResult { let ctx = self.ctx.acquire(); - let res = ctx - .emitter - .emit("info", ()) - .await? - .await_reply(&ctx) - .await?; - Ok(res.data::()?) + let res = ctx.emit("info", ()).await?.await_reply(&ctx).await?; + Ok(res.payload::()?) } #[tracing::instrument(level = "debug", skip(self))] diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index 924ee65..d51370e 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; -use rmp_ipc::error::Result; -use rmp_ipc::prelude::IPCResult; -use rmp_ipc::protocol::*; +use bromine::error::Result; +use bromine::prelude::IPCResult; +use bromine::protocol::*; use std::io::Error; use std::net::ToSocketAddrs; use std::pin::Pin; diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 0a77859..6541b6a 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -4,18 +4,14 @@ use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; use crate::types::identifier::FileIdentifier; use crate::types::tags::{ChangeFileTagsRequest, TagResponse}; use async_trait::async_trait; -use rmp_ipc::context::{PoolGuard, PooledContext}; -use rmp_ipc::ipc::context::Context; -use rmp_ipc::protocol::AsyncProtocolStream; +use bromine::context::{PoolGuard, PooledContext}; +use bromine::ipc::context::Context; -pub struct TagApi { - ctx: PooledContext, +pub struct TagApi { + ctx: PooledContext, } -impl Clone for TagApi -where - S: AsyncProtocolStream, -{ +impl Clone for TagApi { fn clone(&self) -> Self { Self { ctx: self.ctx.clone(), @@ -24,24 +20,18 @@ where } #[async_trait] -impl IPCApi for TagApi -where - S: AsyncProtocolStream, -{ +impl IPCApi for TagApi { fn namespace() -> &'static str { "tags" } - fn ctx(&self) -> PoolGuard> { + fn ctx(&self) -> PoolGuard { self.ctx.acquire() } } -impl TagApi -where - S: AsyncProtocolStream, -{ - pub fn new(ctx: PooledContext) -> Self { +impl TagApi { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs index 0d2a12b..aa1587a 100644 --- a/mediarepo-api/src/lib.rs +++ b/mediarepo-api/src/lib.rs @@ -8,3 +8,5 @@ pub mod daemon_management; #[cfg(feature = "tauri-plugin")] pub mod tauri_plugin; + +pub use bromine; diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 5dc67f7..3a539bf 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -1,7 +1,7 @@ use crate::tauri_plugin::commands::AppAccess; use crate::tauri_plugin::error::PluginResult; -use rmp_ipc::prelude::{IPCError, IPCResult}; -use rmp_ipc::IPCBuilder; +use bromine::prelude::{IPCError, IPCResult}; +use bromine::IPCBuilder; use std::io::ErrorKind; use std::net::{SocketAddr, ToSocketAddrs}; use tokio::net::TcpListener; @@ -43,11 +43,7 @@ async fn try_connect_daemon(address: String) -> IPCResult<()> { .address(address) .build_client() .await?; - ctx.emitter - .emit("info", ()) - .await? - .await_reply(&ctx) - .await?; + ctx.emit("info", ()).await?.await_reply(&ctx).await?; ctx.stop().await?; Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index b17f625..d54af95 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -1,3 +1,4 @@ +use crate::client_api::protocol::ApiProtocolListener; use crate::client_api::ApiClient; use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; @@ -165,7 +166,7 @@ pub async fn select_repository( .ok_or_else(|| PluginError::from("Missing repo path or address in config."))?; get_repo_address(path).await? }; - let client = ApiClient::connect(address).await?; + let client = ApiClient::connect::(address).await?; api_state.set_api(client).await; let mut active_repo = app_state.active_repo.write().await; diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index 5547863..01b21c7 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -1,6 +1,6 @@ use crate::client_api::error::ApiError; use crate::daemon_management::error::DaemonError; -use rmp_ipc::error::Error; +use bromine::error::Error; use serde::Serialize; use std::fmt::{Display, Formatter}; diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index 890437a..e59bb01 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -9,14 +9,13 @@ use parking_lot::RwLock as ParkingRwLock; use tauri::async_runtime::RwLock; use tokio::time::Instant; -use crate::client_api::protocol::ApiProtocolListener; use crate::client_api::ApiClient; use crate::daemon_management::cli::DaemonCli; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{load_settings, Repository, Settings}; pub struct ApiState { - inner: Arc>>>, + inner: Arc>>, } unsafe impl Send for ApiState {} @@ -30,7 +29,7 @@ impl ApiState { } /// Sets the active api client and disconnects the old one - pub async fn set_api(&self, client: ApiClient) { + pub async fn set_api(&self, client: ApiClient) { let mut inner = self.inner.write().await; let old_client = mem::replace(&mut *inner, Some(client)); @@ -49,7 +48,7 @@ impl ApiState { } } - pub async fn api(&self) -> PluginResult> { + pub async fn api(&self) -> PluginResult { let inner = self.inner.read().await; inner .clone() From 6e9d57e5e658ae36d3a9bb0564d871b3a22721ef Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 6 Dec 2021 21:05:48 +0100 Subject: [PATCH 080/116] Fix bromine reexport compiling with all feature combinations Signed-off-by: trivernis --- mediarepo-api/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs index aa1587a..36156c3 100644 --- a/mediarepo-api/src/lib.rs +++ b/mediarepo-api/src/lib.rs @@ -9,4 +9,5 @@ pub mod daemon_management; #[cfg(feature = "tauri-plugin")] pub mod tauri_plugin; +#[cfg(feature = "bromine")] pub use bromine; From 4e2a348a1365a4ba8e1e42537a2907350a71dbdf Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 6 Dec 2021 21:18:12 +0100 Subject: [PATCH 081/116] Update windows code Signed-off-by: trivernis --- mediarepo-api/src/client_api/protocol.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/client_api/protocol.rs b/mediarepo-api/src/client_api/protocol.rs index d51370e..fad6e8f 100644 --- a/mediarepo-api/src/client_api/protocol.rs +++ b/mediarepo-api/src/client_api/protocol.rs @@ -46,7 +46,7 @@ impl AsyncStreamProtocolListener for ApiProtocolListener { } #[cfg(not(unix))] { - use rmp_ipc::prelude::IPCError; + use bromine::prelude::IPCError; Err(IPCError::BuildError( "The address can not be made into a socket address".to_string(), )) @@ -124,7 +124,7 @@ impl AsyncProtocolStream for ApiProtocolStream { } #[cfg(not(unix))] { - use rmp_ipc::prelude::IPCError; + use bromine::prelude::IPCError; Err(IPCError::BuildError( "The address can not be made into a socket address".to_string(), )) From 7c3b2a5051a63d46d463f243e6233d4d35e947b8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 7 Dec 2021 18:01:22 +0100 Subject: [PATCH 082/116] Remove tagged enums Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/types/files.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 8e684e1..604b24b 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.13.0" +version = "0.14.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 66110f3..14a245f 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -36,7 +36,6 @@ pub struct FindFilesRequest { } #[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(tag = "filter_type", content = "filter")] pub enum FilterExpression { OrExpression(Vec), Query(TagQuery), From faa7b4e17ce2ba49192a2eeb3e707cfcbb5e79c3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 7 Dec 2021 18:16:09 +0100 Subject: [PATCH 083/116] Add automated tests Signed-off-by: trivernis --- mediarepo-api/.github/workflows/build.yml | 5 +- mediarepo-api/src/lib.rs | 3 ++ mediarepo-api/src/test/mod.rs | 2 + .../src/test/test_type_serialization.rs | 54 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 mediarepo-api/src/test/mod.rs create mode 100644 mediarepo-api/src/test/test_type_serialization.rs diff --git a/mediarepo-api/.github/workflows/build.yml b/mediarepo-api/.github/workflows/build.yml index bbaa82d..1df7aef 100644 --- a/mediarepo-api/.github/workflows/build.yml +++ b/mediarepo-api/.github/workflows/build.yml @@ -44,4 +44,7 @@ jobs: run: cargo build --features=client-api - name: Build Plugin - run: cargo build --features=tauri-plugin \ No newline at end of file + run: cargo build --features=tauri-plugin + + - name: Test + run: cargo test --all-features \ No newline at end of file diff --git a/mediarepo-api/src/lib.rs b/mediarepo-api/src/lib.rs index 36156c3..5cf790d 100644 --- a/mediarepo-api/src/lib.rs +++ b/mediarepo-api/src/lib.rs @@ -11,3 +11,6 @@ pub mod tauri_plugin; #[cfg(feature = "bromine")] pub use bromine; + +#[cfg(test)] +mod test; diff --git a/mediarepo-api/src/test/mod.rs b/mediarepo-api/src/test/mod.rs new file mode 100644 index 0000000..ce36222 --- /dev/null +++ b/mediarepo-api/src/test/mod.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "bromine")] +mod test_type_serialization; diff --git a/mediarepo-api/src/test/test_type_serialization.rs b/mediarepo-api/src/test/test_type_serialization.rs new file mode 100644 index 0000000..d183d46 --- /dev/null +++ b/mediarepo-api/src/test/test_type_serialization.rs @@ -0,0 +1,54 @@ +use crate::types::files::{ + FilterExpression, GetFileThumbnailOfSizeRequest, SortDirection, SortKey, TagQuery, +}; +use crate::types::identifier::FileIdentifier; +use bromine::payload::DynamicSerializer; +use bromine::prelude::IPCResult; +use serde::de::DeserializeOwned; +use serde::Serialize; + +#[test] +fn it_serializes_file_identifier() { + test_serialization(FileIdentifier::ID(0)).unwrap(); +} + +#[test] +fn it_serializes_get_file_thumbnail_of_size_requests() { + test_serialization(GetFileThumbnailOfSizeRequest { + id: FileIdentifier::ID(0), + max_size: (u32::MAX, u32::MAX), + min_size: (0, 0), + }) + .unwrap(); +} + +#[test] +fn it_serializes_tag_queries() { + test_serialization(TagQuery { + tag: String::from("Hello"), + negate: true, + }) + .unwrap(); +} + +#[test] +fn it_serializes_filter_expressions() { + test_serialization(FilterExpression::Query(TagQuery { + tag: String::from("World"), + negate: false, + })) + .unwrap(); +} + +#[test] +fn it_serializes_sort_keys() { + test_serialization(SortKey::FileName(SortDirection::Descending)).unwrap(); +} + +fn test_serialization(data: T) -> IPCResult<()> { + let serializer = DynamicSerializer::first_available(); + let bytes = serializer.serialize(data)?; + let _: T = serializer.deserialize(&bytes[..])?; + + Ok(()) +} From ce516ca498115b51e55231a154b799ab585cc740 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 10:41:03 +0100 Subject: [PATCH 084/116] Add daemon discovery check Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 3 ++- mediarepo-api/src/daemon_management/mod.rs | 7 +++++++ mediarepo-api/src/tauri_plugin/commands/daemon.rs | 11 +++++++++-- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- mediarepo-api/src/tauri_plugin/settings.rs | 5 +++-- mediarepo-api/src/tauri_plugin/state.rs | 10 +++++++--- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 604b24b..44221e3 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -17,6 +17,7 @@ mime_guess = {version = "2.0.3", optional=true} serde_piecewise_default = "0.2.0" futures = {version = "0.3.17", optional=true} url = {version = "2.2.2", optional=true } +pathsearch = {version="0.2.0", optional=true} [dependencies.bromine] version = "0.16.1" @@ -48,4 +49,4 @@ optional = true [features] tauri-plugin = ["client-api","tauri", "parking_lot", "serde_json", "tokio", "toml", "directories", "mime_guess", "futures", "url"] -client-api = ["bromine", "async-trait", "tokio"] \ No newline at end of file +client-api = ["bromine", "async-trait", "tokio", "pathsearch"] \ No newline at end of file diff --git a/mediarepo-api/src/daemon_management/mod.rs b/mediarepo-api/src/daemon_management/mod.rs index 0414a80..41b2d53 100644 --- a/mediarepo-api/src/daemon_management/mod.rs +++ b/mediarepo-api/src/daemon_management/mod.rs @@ -1,2 +1,9 @@ +use pathsearch::find_executable_in_path; +use std::path::PathBuf; + pub mod cli; pub mod error; + +pub fn find_daemon_executable() -> Option { + find_executable_in_path("mediarepo-daemon") +} diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 3a539bf..af4b9a3 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -6,9 +6,16 @@ use std::io::ErrorKind; use std::net::{SocketAddr, ToSocketAddrs}; use tokio::net::TcpListener; +#[tauri::command] +pub async fn has_executable(app_state: AppAccess<'_>) -> PluginResult { + let settings = app_state.settings.read().await; + + Ok(settings.daemon_path.is_some()) +} + #[tauri::command] pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { - let daemon = app_state.get_daemon_cli(repo_path).await; + let daemon = app_state.get_daemon_cli(repo_path).await?; daemon.init_repo().await?; Ok(()) @@ -16,7 +23,7 @@ pub async fn init_repository(app_state: AppAccess<'_>, repo_path: String) -> Plu #[tauri::command] pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> { - let mut daemon = app_state.get_daemon_cli(repo_path).await; + let mut daemon = app_state.get_daemon_cli(repo_path).await?; daemon.start_daemon()?; app_state.add_started_daemon(daemon).await; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 73110f7..b19f587 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -58,7 +58,8 @@ impl MediarepoPlugin { save_file_locally, delete_thumbnails, read_file, - delete_repository + delete_repository, + has_executable ]), } } diff --git a/mediarepo-api/src/tauri_plugin/settings.rs b/mediarepo-api/src/tauri_plugin/settings.rs index 7c1f53b..b020c95 100644 --- a/mediarepo-api/src/tauri_plugin/settings.rs +++ b/mediarepo-api/src/tauri_plugin/settings.rs @@ -1,3 +1,4 @@ +use crate::daemon_management::find_daemon_executable; use crate::tauri_plugin::error::PluginResult; use directories::ProjectDirs; use serde::{Deserialize, Serialize}; @@ -19,14 +20,14 @@ pub struct Repository { #[derive(DeserializePiecewiseDefault, Debug, Serialize)] pub struct Settings { - pub daemon_path: String, + pub daemon_path: Option, pub repositories: HashMap, } impl Default for Settings { fn default() -> Self { Self { - daemon_path: String::from("mediarepo-daemon"), + daemon_path: find_daemon_executable().map(|e| e.to_string_lossy().to_string()), repositories: HashMap::new(), } } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index e59bb01..f40076f 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -179,11 +179,15 @@ impl AppState { } /// Returns the daemon cli client - pub async fn get_daemon_cli(&self, repo_path: String) -> DaemonCli { + pub async fn get_daemon_cli(&self, repo_path: String) -> PluginResult { let settings = self.settings.read().await; - let path = settings.daemon_path.clone(); + let path = settings + .daemon_path + .clone() + .ok_or_else(|| PluginError::from("Missing daemon executable"))?; + let cli = DaemonCli::new(path, repo_path); - DaemonCli::new(path, repo_path) + Ok(cli) } /// Adds a started daemon to the running daemons From 2cb7e9cb9b515d79b08ca47189022f3abc25b75f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 10:45:33 +0100 Subject: [PATCH 085/116] Add daemon path fallback discovery check Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/state.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index f40076f..c38caaf 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -11,8 +11,9 @@ use tokio::time::Instant; use crate::client_api::ApiClient; use crate::daemon_management::cli::DaemonCli; +use crate::daemon_management::find_daemon_executable; use crate::tauri_plugin::error::{PluginError, PluginResult}; -use crate::tauri_plugin::settings::{load_settings, Repository, Settings}; +use crate::tauri_plugin::settings::{load_settings, save_settings, Repository, Settings}; pub struct ApiState { inner: Arc>>, @@ -180,7 +181,12 @@ impl AppState { /// Returns the daemon cli client pub async fn get_daemon_cli(&self, repo_path: String) -> PluginResult { - let settings = self.settings.read().await; + let mut settings = self.settings.write().await; + if settings.daemon_path.is_none() { + settings.daemon_path = + find_daemon_executable().map(|p| p.to_string_lossy().to_string()); + save_settings(&settings)?; + } let path = settings .daemon_path .clone() From aa568a9365cbe9f613223d045e9b2db60205935c Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 10:49:10 +0100 Subject: [PATCH 086/116] Add daemon discovery to has_executable check Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/daemon.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index af4b9a3..01bff9d 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -1,5 +1,7 @@ +use crate::daemon_management::find_daemon_executable; use crate::tauri_plugin::commands::AppAccess; use crate::tauri_plugin::error::PluginResult; +use crate::tauri_plugin::settings::save_settings; use bromine::prelude::{IPCError, IPCResult}; use bromine::IPCBuilder; use std::io::ErrorKind; @@ -8,7 +10,12 @@ use tokio::net::TcpListener; #[tauri::command] pub async fn has_executable(app_state: AppAccess<'_>) -> PluginResult { - let settings = app_state.settings.read().await; + let mut settings = app_state.settings.write().await; + + if settings.daemon_path.is_none() { + settings.daemon_path = find_daemon_executable().map(|e| e.to_string_lossy().to_string()); + save_settings(&settings)?; + } Ok(settings.daemon_path.is_some()) } From e71ee47e50423ff1adbb5121a1396ab9ae5ea7d5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 13:50:46 +0100 Subject: [PATCH 087/116] Add api to get and set the frontend state Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/mod.rs | 5 +++ mediarepo-api/src/client_api/repo.rs | 41 +++++++++++++++++++ .../src/tauri_plugin/commands/repo.rs | 17 ++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 4 +- mediarepo-api/src/types/mod.rs | 3 +- mediarepo-api/src/types/repo.rs | 6 +++ 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 mediarepo-api/src/client_api/repo.rs create mode 100644 mediarepo-api/src/types/repo.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 44221e3..3a7cb92 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.14.0" +version = "0.15.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 43b577b..a8c499c 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,10 +1,12 @@ pub mod error; pub mod file; pub mod protocol; +pub mod repo; pub mod tag; use crate::client_api::error::{ApiError, ApiResult}; use crate::client_api::file::FileApi; +use crate::client_api::repo::RepoApi; use crate::client_api::tag::TagApi; use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use async_trait::async_trait; @@ -43,6 +45,7 @@ pub struct ApiClient { ctx: PooledContext, pub file: FileApi, pub tag: TagApi, + pub repo: RepoApi, } impl Clone for ApiClient { @@ -51,6 +54,7 @@ impl Clone for ApiClient { ctx: self.ctx.clone(), file: self.file.clone(), tag: self.tag.clone(), + repo: self.repo.clone(), } } } @@ -61,6 +65,7 @@ impl ApiClient { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), + repo: RepoApi::new(ctx.clone()), ctx, } } diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs new file mode 100644 index 0000000..03a0812 --- /dev/null +++ b/mediarepo-api/src/client_api/repo.rs @@ -0,0 +1,41 @@ +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::repo::FrontendState; +use bromine::prelude::*; + +#[derive(Clone)] +pub struct RepoApi { + ctx: PooledContext, +} + +impl IPCApi for RepoApi { + fn namespace() -> &'static str { + "repo" + } + + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() + } +} + +impl RepoApi { + pub fn new(ctx: PooledContext) -> Self { + Self { ctx } + } + + /// Returns the state of the frontend that is stored in the repo + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_frontend_state(&self) -> ApiResult { + let state = self.emit_and_get("frontend_state", ()).await?; + + Ok(state) + } + + /// Sets the state of the frontend + #[tracing::instrument(level = "debug", skip(self))] + pub async fn set_frontend_state(&self, state: FrontendState) -> ApiResult<()> { + self.emit("set_frontend_state", state).await?; + + Ok(()) + } +} diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index d54af95..742a639 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,6 +3,7 @@ use crate::client_api::ApiClient; use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; +use crate::types::repo::FrontendState; use serde::{Deserialize, Serialize}; use std::mem; use std::path::PathBuf; @@ -183,6 +184,22 @@ pub async fn select_repository( Ok(()) } +#[tauri::command] +pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult { + let api = api_state.api().await?; + let state = api.repo.get_frontend_state().await?; + + Ok(state.state) +} + +#[tauri::command] +pub async fn set_frontend_state(api_state: ApiAccess<'_>, state: String) -> PluginResult<()> { + let api = api_state.api().await?; + api.repo.set_frontend_state(FrontendState { state }).await?; + + Ok(()) +} + async fn get_repo_address(path: String) -> PluginResult { let tcp_path = PathBuf::from(&path).join("repo.tcp"); let socket_path = PathBuf::from(&path).join("repo.sock"); diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index b19f587..3254f3a 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -59,7 +59,9 @@ impl MediarepoPlugin { delete_thumbnails, read_file, delete_repository, - has_executable + has_executable, + get_frontend_state, + set_frontend_state ]), } } diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs index 2a5ed7e..11a3de9 100644 --- a/mediarepo-api/src/types/mod.rs +++ b/mediarepo-api/src/types/mod.rs @@ -1,4 +1,5 @@ pub mod files; pub mod identifier; pub mod misc; -pub mod tags; \ No newline at end of file +pub mod repo; +pub mod tags; diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs new file mode 100644 index 0000000..cba1d48 --- /dev/null +++ b/mediarepo-api/src/types/repo.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct FrontendState { + pub state: String, +} From 31eadd2e43cf50f0a14891d56f269b18e78dcc92 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 13:57:29 +0100 Subject: [PATCH 088/116] Make state optional in the frontend state to allow passing an uninitialized state Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/tauri_plugin/commands/repo.rs | 6 ++++-- mediarepo-api/src/types/repo.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 3a7cb92..96e1387 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.15.0" +version = "0.16.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 742a639..32d3b3f 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -185,7 +185,7 @@ pub async fn select_repository( } #[tauri::command] -pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult { +pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult> { let api = api_state.api().await?; let state = api.repo.get_frontend_state().await?; @@ -195,7 +195,9 @@ pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult, state: String) -> PluginResult<()> { let api = api_state.api().await?; - api.repo.set_frontend_state(FrontendState { state }).await?; + api.repo + .set_frontend_state(FrontendState { state: Some(state) }) + .await?; Ok(()) } diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index cba1d48..9a7139f 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -2,5 +2,5 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct FrontendState { - pub state: String, + pub state: Option, } From 9fe670a648b834238eb8b11e17d65a83f21a649b Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 13 Dec 2021 18:10:29 +0100 Subject: [PATCH 089/116] Add clearing of the buffer after closing a repository Signed-off-by: trivernis --- mediarepo-api/src/client_api/mod.rs | 2 +- mediarepo-api/src/tauri_plugin/commands/repo.rs | 8 +++++++- mediarepo-api/src/tauri_plugin/state.rs | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index a8c499c..d6969a4 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -77,7 +77,7 @@ impl ApiClient { ) -> ApiResult { let ctx = IPCBuilder::::new() .address(address) - .timeout(Duration::from_secs(10)) + .timeout(Duration::from_secs(30)) .build_pooled_client(8) .await?; let client = Self::new(ctx); diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 32d3b3f..1912de4 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -1,6 +1,6 @@ use crate::client_api::protocol::ApiProtocolListener; use crate::client_api::ApiClient; -use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; +use crate::tauri_plugin::commands::{ApiAccess, AppAccess, BufferAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; use crate::types::repo::FrontendState; @@ -121,10 +121,12 @@ pub async fn check_local_repository_exists(path: String) -> PluginResult { pub async fn disconnect_repository( app_state: AppAccess<'_>, api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, ) -> PluginResult<()> { api_state.disconnect().await; let mut active_repo = app_state.active_repo.write().await; mem::take(&mut *active_repo); + buffer_state.clear(); Ok(()) } @@ -133,12 +135,16 @@ pub async fn disconnect_repository( pub async fn close_local_repository( app_state: AppAccess<'_>, api_state: ApiAccess<'_>, + buffer_state: BufferAccess<'_>, ) -> PluginResult<()> { let mut active_repo = app_state.active_repo.write().await; + if let Some(path) = mem::take(&mut *active_repo).and_then(|r| r.path) { app_state.stop_running_daemon(&path).await?; } api_state.disconnect().await; + mem::take(&mut *active_repo); + buffer_state.clear(); Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/state.rs b/mediarepo-api/src/tauri_plugin/state.rs index c38caaf..e053325 100644 --- a/mediarepo-api/src/tauri_plugin/state.rs +++ b/mediarepo-api/src/tauri_plugin/state.rs @@ -122,6 +122,12 @@ impl BufferState { } } + /// Clears the buffer completely + pub fn clear(&self) { + let mut buffer = self.buffer.write(); + buffer.clear(); + } + /// Trims the buffer to the given target size pub fn trim_to_size(&self, target_size: usize) { let mut size = self.get_size(); From 445df0962fed62f6e445eadeb665e90df3e4d62e Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 13:40:33 +0100 Subject: [PATCH 090/116] Update dependencies Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 96e1387..69901d2 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.16.0" +version = "0.16.1" edition = "2018" license = "gpl-3" @@ -9,42 +9,42 @@ license = "gpl-3" [dependencies] tracing = "0.1.29" thiserror = "1.0.30" -async-trait = {version = "0.1.51", optional=true} +async-trait = {version = "0.1.52", optional=true} parking_lot = {version="0.11.2", optional=true} -serde_json = {version="1.0.68", optional=true} +serde_json = {version="1.0.73", optional=true} directories = {version="4.0.1", optional=true} mime_guess = {version = "2.0.3", optional=true} serde_piecewise_default = "0.2.0" -futures = {version = "0.3.17", optional=true} +futures = {version = "0.3.19", optional=true} url = {version = "2.2.2", optional=true } pathsearch = {version="0.2.0", optional=true} [dependencies.bromine] -version = "0.16.1" +version = "^0.16.2" optional = true features = ["serialize_bincode"] [dependencies.serde] -version = "1.0.130" +version = "^1.0.132" features = ["serde_derive"] [dependencies.chrono] -version = "0.4.19" +version = "^0.4.19" features = ["serde"] [dependencies.tauri] -version = "1.0.0-beta.8" +version = "^1.0.0-beta.8" optional=true default-features = false features = [] [dependencies.tokio] -version = "1.12.0" +version = "^1.15.0" optional = true features = ["sync", "fs", "net", "io-util", "io-std", "time", "rt", "process"] [dependencies.toml] -version = "0.5.8" +version = "^0.5.8" optional = true [features] From a723ebab95a7b11d51ea44d0e9d395e4681de7f6 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 13:48:07 +0100 Subject: [PATCH 091/116] Add custom timeouts to events Signed-off-by: Trivernis --- mediarepo-api/src/client_api/file.rs | 15 +++++++++------ mediarepo-api/src/client_api/mod.rs | 11 ++++++++--- mediarepo-api/src/client_api/repo.rs | 3 ++- mediarepo-api/src/client_api/tag.rs | 10 ++++++---- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index 419289f..3ef6d4c 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -10,6 +10,7 @@ use async_trait::async_trait; use bromine::context::{PoolGuard, PooledContext}; use bromine::payload::BytePayload; use bromine::prelude::*; +use tokio::time::Duration; pub struct FileApi { ctx: PooledContext, @@ -43,12 +44,12 @@ impl FileApi { /// Returns all known files #[tracing::instrument(level = "debug", skip(self))] pub async fn all_files(&self) -> ApiResult> { - self.emit_and_get("all_files", ()).await + self.emit_and_get("all_files", (), Some(Duration::from_secs(30))).await } /// Returns a file by identifier pub async fn get_file(&self, id: FileIdentifier) -> ApiResult { - self.emit_and_get("get_file", id).await + self.emit_and_get("get_file", id, Some(Duration::from_secs(2))).await } /// Searches for a file by a list of tags @@ -64,6 +65,7 @@ impl FileApi { filters, sort_expression, }, + Some(Duration::from_secs(20)) ) .await } @@ -72,7 +74,7 @@ impl FileApi { #[tracing::instrument(level = "debug", skip(self))] pub async fn read_file(&self, id: FileIdentifier) -> ApiResult> { let payload: BytePayload = self - .emit_and_get("read_file", ReadFileRequest { id }) + .emit_and_get("read_file", ReadFileRequest { id }, Some(Duration::from_secs(10))) .await?; Ok(payload.into_inner()) @@ -84,7 +86,7 @@ impl FileApi { &self, id: FileIdentifier, ) -> ApiResult> { - self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id }) + self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id }, Some(Duration::from_secs(2))) .await } @@ -104,6 +106,7 @@ impl FileApi { min_size, max_size, }, + Some(Duration::from_secs(2)) ) .await?; let (metadata, bytes) = payload.into_inner(); @@ -118,7 +121,7 @@ impl FileApi { file_id: FileIdentifier, name: String, ) -> ApiResult { - self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name }) + self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name }, Some(Duration::from_secs(1))) .await } @@ -135,7 +138,7 @@ impl FileApi { BytePayload::new(bytes), ); - self.emit_and_get("add_file", payload).await + self.emit_and_get("add_file", payload, Some(Duration::from_secs(5))).await } /// Deletes all thumbnails of a file to regenerate them when requested diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index d6969a4..0bc1564 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -12,7 +12,7 @@ use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use async_trait::async_trait; use bromine::ipc::stream_emitter::EmitMetadata; use bromine::prelude::*; -use std::time::Duration; +use tokio::time::Duration; #[async_trait] pub trait IPCApi { @@ -34,8 +34,13 @@ pub trait IPCApi { &self, event_name: &str, data: T, + timeout: Option, ) -> ApiResult { - let meta = self.emit(event_name, data).await?; + let mut meta = self.emit(event_name, data).await?; + + if let Some(timeout) = timeout { + meta = meta.with_timeout(timeout); + } let response = meta.await_reply(&self.ctx()).await?; Ok(response.payload()?) @@ -77,7 +82,7 @@ impl ApiClient { ) -> ApiResult { let ctx = IPCBuilder::::new() .address(address) - .timeout(Duration::from_secs(30)) + .timeout(Duration::from_secs(10)) .build_pooled_client(8) .await?; let client = Self::new(ctx); diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 03a0812..45e234f 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -2,6 +2,7 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::repo::FrontendState; use bromine::prelude::*; +use tokio::time::Duration; #[derive(Clone)] pub struct RepoApi { @@ -26,7 +27,7 @@ impl RepoApi { /// Returns the state of the frontend that is stored in the repo #[tracing::instrument(level = "debug", skip(self))] pub async fn get_frontend_state(&self) -> ApiResult { - let state = self.emit_and_get("frontend_state", ()).await?; + let state = self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await?; Ok(state) } diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 6541b6a..ec030da 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -1,3 +1,4 @@ +use std::time::Duration; use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; @@ -38,27 +39,27 @@ impl TagApi { /// Returns a list of all tags stored in the repo #[tracing::instrument(level = "debug", skip(self))] pub async fn get_all_tags(&self) -> ApiResult> { - self.emit_and_get("all_tags", ()).await + self.emit_and_get("all_tags", (), Some(Duration::from_secs(2))).await } /// Returns a list of all tags for a file #[tracing::instrument(level = "debug", skip(self))] pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult> { - self.emit_and_get("tags_for_file", GetFileTagsRequest { id }) + self.emit_and_get("tags_for_file", GetFileTagsRequest { id }, Some(Duration::from_secs(1))) .await } /// Returns a list of all tags that are assigned to the list of files #[tracing::instrument(level = "debug", skip_all)] pub async fn get_tags_for_files(&self, hashes: Vec) -> ApiResult> { - self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }) + self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }, Some(Duration::from_secs(10))) .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 + self.emit_and_get("create_tags", tags, Some(Duration::from_secs(10))).await } /// Changes the tags of a file @@ -76,6 +77,7 @@ impl TagApi { added_tags, removed_tags, }, + Some(Duration::from_secs(10)) ) .await } From 73c98e44a2eb63afef60ced97b4914a5f897dc6a Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 13:53:09 +0100 Subject: [PATCH 092/116] Add more api calls Add api to get a list of files by identifiers. Add api to get all namespaces stored in the repo. Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 7 +++++++ mediarepo-api/src/client_api/tag.rs | 8 +++++++- mediarepo-api/src/types/tags.rs | 6 ++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 69901d2..a78db67 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.16.1" +version = "0.17.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 3ef6d4c..323fd06 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -48,10 +48,17 @@ impl FileApi { } /// Returns a file by identifier + #[tracing::instrument(level = "debug", skip(self))] pub async fn get_file(&self, id: FileIdentifier) -> ApiResult { self.emit_and_get("get_file", id, Some(Duration::from_secs(2))).await } + /// Returns metadata for a range of files + #[tracing::instrument(level = "debug", skip(self, ids))] + pub async fn get_files(&self, ids: Vec) -> ApiResult> { + self.emit_and_get("get_files", ids, Some(Duration::from_secs(10))).await + } + /// Searches for a file by a list of tags #[tracing::instrument(level = "debug", skip(self))] pub async fn find_files( diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index ec030da..6f7870c 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -3,7 +3,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::{ChangeFileTagsRequest, TagResponse}; +use crate::types::tags::{ChangeFileTagsRequest, NamespaceResponse, TagResponse}; use async_trait::async_trait; use bromine::context::{PoolGuard, PooledContext}; use bromine::ipc::context::Context; @@ -42,6 +42,12 @@ impl TagApi { self.emit_and_get("all_tags", (), Some(Duration::from_secs(2))).await } + /// Returns a list of all namespaces stored in the repo + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_all_namespaces(&self) -> ApiResult> { + self.emit_and_get("all_namespaces", (), Some(Duration::from_secs(2))).await + } + /// Returns a list of all tags for a file #[tracing::instrument(level = "debug", skip(self))] pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult> { diff --git a/mediarepo-api/src/types/tags.rs b/mediarepo-api/src/types/tags.rs index ecb7281..ba34d59 100644 --- a/mediarepo-api/src/types/tags.rs +++ b/mediarepo-api/src/types/tags.rs @@ -8,6 +8,12 @@ pub struct TagResponse { pub name: String, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct NamespaceResponse { + pub id: i64, + pub name: String, +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ChangeFileTagsRequest { pub file_id: FileIdentifier, From e56c7b04eced0730fb265eac1f7f11b13ed56ccb Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 14:52:49 +0100 Subject: [PATCH 093/116] Downgrade futures to solve conflict with tauri version Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index a78db67..d62fa6c 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -7,17 +7,17 @@ license = "gpl-3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tracing = "0.1.29" -thiserror = "1.0.30" -async-trait = {version = "0.1.52", optional=true} -parking_lot = {version="0.11.2", optional=true} -serde_json = {version="1.0.73", optional=true} -directories = {version="4.0.1", optional=true} -mime_guess = {version = "2.0.3", optional=true} -serde_piecewise_default = "0.2.0" -futures = {version = "0.3.19", optional=true} -url = {version = "2.2.2", optional=true } -pathsearch = {version="0.2.0", optional=true} +tracing = "^0.1.29" +thiserror = "^1.0.30" +async-trait = {version = "^0.1.52", optional=true} +parking_lot = {version="^0.11.2", optional=true} +serde_json = {version="^1.0.73", optional=true} +directories = {version="^4.0.1", optional=true} +mime_guess = {version = "^2.0.3", optional=true} +serde_piecewise_default = "^0.2.0" +futures = {version = "^0.3.17", optional=true} +url = {version = "^2.2.2", optional=true } +pathsearch = {version="^0.2.0", optional=true} [dependencies.bromine] version = "^0.16.2" From b9a0935631f663bda596177db326a4fb330cfffe Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 14:56:15 +0100 Subject: [PATCH 094/116] Upgrade futures again Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index d62fa6c..e2135e9 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -15,7 +15,7 @@ serde_json = {version="^1.0.73", optional=true} directories = {version="^4.0.1", optional=true} mime_guess = {version = "^2.0.3", optional=true} serde_piecewise_default = "^0.2.0" -futures = {version = "^0.3.17", optional=true} +futures = {version = "^0.3.19", optional=true} url = {version = "^2.2.2", optional=true } pathsearch = {version="^0.2.0", optional=true} From 60140fcd97b31770e0b3c2edca6bb9ec98510dc7 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 16:16:18 +0100 Subject: [PATCH 095/116] Add missing tauri commands Signed-off-by: Trivernis --- mediarepo-api/src/tauri_plugin/commands/file.rs | 9 +++++++++ mediarepo-api/src/tauri_plugin/commands/tag.rs | 10 +++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 4 +++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index f3d1c20..86121aa 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -25,6 +25,15 @@ pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult, ids: Vec) -> PluginResult> { + let api = api_state.api().await?; + let ids = ids.into_iter().map(|id| FileIdentifier::ID(id)).collect(); + let files = api.file.get_files(ids).await?; + + Ok(files) +} + #[tauri::command] pub async fn add_local_file( api_state: ApiAccess<'_>, diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index d54bfa7..dbd2bdd 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -1,7 +1,7 @@ use crate::tauri_plugin::commands::ApiAccess; use crate::tauri_plugin::error::PluginResult; use crate::types::identifier::FileIdentifier; -use crate::types::tags::TagResponse; +use crate::types::tags::{NamespaceResponse, TagResponse}; #[tauri::command] pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult> { @@ -11,6 +11,14 @@ pub async fn get_all_tags(api_state: ApiAccess<'_>) -> PluginResult) -> PluginResult> { + let api = api_state.api().await?; + let all_namespaces = api.tag.get_all_namespaces().await?; + + Ok(all_namespaces) +} + #[tauri::command] pub async fn get_tags_for_file( id: i64, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 3254f3a..574408e 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -61,7 +61,9 @@ impl MediarepoPlugin { delete_repository, has_executable, get_frontend_state, - set_frontend_state + set_frontend_state, + get_all_namespaces, + get_files ]), } } From d8560a180c9e35bf846b5ea8ec8cc7abb8113e6c Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 16:58:27 +0100 Subject: [PATCH 096/116] Add api to get repository metadata Signed-off-by: Trivernis --- mediarepo-api/src/client_api/repo.rs | 15 ++++++++++++--- mediarepo-api/src/tauri_plugin/commands/repo.rs | 10 +++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- mediarepo-api/src/types/repo.rs | 12 ++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 45e234f..6abd233 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -1,9 +1,10 @@ -use crate::client_api::error::ApiResult; -use crate::client_api::IPCApi; -use crate::types::repo::FrontendState; use bromine::prelude::*; use tokio::time::Duration; +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::repo::{FrontendState, RepositoryMetadata}; + #[derive(Clone)] pub struct RepoApi { ctx: PooledContext, @@ -24,6 +25,14 @@ impl RepoApi { Self { ctx } } + /// Returns metadata about the repository + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_repo_metadata(&self) -> ApiResult { + let metadata = self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(2))).await?; + + Ok(metadata) + } + /// Returns the state of the frontend that is stored in the repo #[tracing::instrument(level = "debug", skip(self))] pub async fn get_frontend_state(&self) -> ApiResult { diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 1912de4..3545b64 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,7 +3,7 @@ use crate::client_api::ApiClient; use crate::tauri_plugin::commands::{ApiAccess, AppAccess, BufferAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; -use crate::types::repo::FrontendState; +use crate::types::repo::{FrontendState, RepositoryMetadata}; use serde::{Deserialize, Serialize}; use std::mem; use std::path::PathBuf; @@ -190,6 +190,14 @@ pub async fn select_repository( Ok(()) } +#[tauri::command] +pub async fn get_repo_metadata(api_state: ApiAccess<'_>) -> PluginResult { + let api = api_state.api().await?; + let metadata = api.repo.get_repo_metadata().await?; + + Ok(metadata) +} + #[tauri::command] pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult> { let api = api_state.api().await?; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 574408e..4f012fc 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -63,7 +63,8 @@ impl MediarepoPlugin { get_frontend_state, set_frontend_state, get_all_namespaces, - get_files + get_files, + get_repo_metadata ]), } } diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index 9a7139f..c17c751 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -4,3 +4,15 @@ use serde::{Deserialize, Serialize}; pub struct FrontendState { pub state: Option, } + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct RepositoryMetadata { + pub version: String, + pub file_count: u64, + pub tag_count: u64, + pub namespace_count: u64, + pub total_size: u64, + pub image_size: u64, + pub database_size: u64, + pub thumbnail_size: u64, +} \ No newline at end of file From 33ceb24972e0369c85011e23f30ac9536b187d26 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 17:36:52 +0100 Subject: [PATCH 097/116] Add missing fields for repo metadata Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/types/repo.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index e2135e9..94d78ce 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.17.0" +version = "0.18.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index c17c751..d00ded5 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -11,8 +11,10 @@ pub struct RepositoryMetadata { pub file_count: u64, pub tag_count: u64, pub namespace_count: u64, + pub mapping_count: u64, + pub hash_count: u64, pub total_size: u64, - pub image_size: u64, + pub file_size: u64, pub database_size: u64, pub thumbnail_size: u64, } \ No newline at end of file From d7e12ebff7de1f1a81d8f843acd7d60721fa082c Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Dec 2021 19:11:58 +0100 Subject: [PATCH 098/116] Increase timeout for fetching repository metadata Signed-off-by: Trivernis --- mediarepo-api/src/client_api/repo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 6abd233..1cb17d0 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -28,7 +28,7 @@ impl RepoApi { /// Returns metadata about the repository #[tracing::instrument(level = "debug", skip(self))] pub async fn get_repo_metadata(&self) -> ApiResult { - let metadata = self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(2))).await?; + let metadata = self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(30))).await?; Ok(metadata) } From bb667a345d0549004608f629b18cbcc7db63e9d8 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 28 Dec 2021 12:50:40 +0100 Subject: [PATCH 099/116] Move size calculation to different api Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/repo.rs | 14 ++++++++------ .../src/tauri_plugin/commands/repo.rs | 10 +++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- mediarepo-api/src/types/repo.rs | 18 ++++++++++++++---- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 94d78ce..76667f1 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.18.0" +version = "0.19.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 1cb17d0..4344323 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -3,7 +3,7 @@ use tokio::time::Duration; use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; -use crate::types::repo::{FrontendState, RepositoryMetadata}; +use crate::types::repo::{FrontendState, RepositoryMetadata, SizeMetadata, SizeType}; #[derive(Clone)] pub struct RepoApi { @@ -28,17 +28,19 @@ impl RepoApi { /// Returns metadata about the repository #[tracing::instrument(level = "debug", skip(self))] pub async fn get_repo_metadata(&self) -> ApiResult { - let metadata = self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(30))).await?; + self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(3))).await + } - Ok(metadata) + /// Returns the size of a given type + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_size(&self, size_type: SizeType) -> ApiResult { + self.emit_and_get("size_metadata", (), Some(Duration::from_secs(30))).await } /// Returns the state of the frontend that is stored in the repo #[tracing::instrument(level = "debug", skip(self))] pub async fn get_frontend_state(&self) -> ApiResult { - let state = self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await?; - - Ok(state) + self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await } /// Sets the state of the frontend diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 3545b64..956b0c3 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,7 +3,7 @@ use crate::client_api::ApiClient; use crate::tauri_plugin::commands::{ApiAccess, AppAccess, BufferAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; -use crate::types::repo::{FrontendState, RepositoryMetadata}; +use crate::types::repo::{FrontendState, RepositoryMetadata, SizeMetadata, SizeType}; use serde::{Deserialize, Serialize}; use std::mem; use std::path::PathBuf; @@ -198,6 +198,14 @@ pub async fn get_repo_metadata(api_state: ApiAccess<'_>) -> PluginResult, size_type: SizeType) -> PluginResult { + let api = api_state.api().await?; + let size = api.repo.get_size(size_type).await?; + + Ok(size) +} + #[tauri::command] pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult> { let api = api_state.api().await?; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 4f012fc..b4c37e2 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -64,7 +64,8 @@ impl MediarepoPlugin { set_frontend_state, get_all_namespaces, get_files, - get_repo_metadata + get_repo_metadata, + get_size ]), } } diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index d00ded5..6fad584 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -13,8 +13,18 @@ pub struct RepositoryMetadata { pub namespace_count: u64, pub mapping_count: u64, pub hash_count: u64, - pub total_size: u64, - pub file_size: u64, - pub database_size: u64, - pub thumbnail_size: u64, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct SizeMetadata { + pub size_type: SizeType, + pub size: u64, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum SizeType { + Total, + FileFolder, + ThumbFolder, + DatabaseFile, } \ No newline at end of file From 60fdcb5d74d1454d38e4a4004e9313b0a87746d1 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 28 Dec 2021 13:28:31 +0100 Subject: [PATCH 100/116] Fix missing request data for size_metadata request Signed-off-by: Trivernis --- mediarepo-api/src/client_api/repo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 4344323..51e71a8 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -34,7 +34,7 @@ impl RepoApi { /// Returns the size of a given type #[tracing::instrument(level = "debug", skip(self))] pub async fn get_size(&self, size_type: SizeType) -> ApiResult { - self.emit_and_get("size_metadata", (), Some(Duration::from_secs(30))).await + self.emit_and_get("size_metadata", size_type, Some(Duration::from_secs(30))).await } /// Returns the state of the frontend that is stored in the repo From 996d6babefa0b75baf723266dd7153414cedcff9 Mon Sep 17 00:00:00 2001 From: Julius Riegel Date: Tue, 28 Dec 2021 12:54:08 +0000 Subject: [PATCH 101/116] Create LICENSE --- mediarepo-api/LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 674 insertions(+) create mode 100644 mediarepo-api/LICENSE diff --git a/mediarepo-api/LICENSE b/mediarepo-api/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/mediarepo-api/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. From ac3bc9cffd66c869fdab72d8b5e32d10bd118626 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Wed, 29 Dec 2021 15:23:14 +0100 Subject: [PATCH 102/116] Update bromine version Signed-off-by: Trivernis --- mediarepo-api/Cargo.toml | 4 ++-- mediarepo-api/src/client_api/mod.rs | 18 ++++++------------ .../src/tauri_plugin/commands/daemon.rs | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 76667f1..61f9c52 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.19.0" +version = "0.20.0" edition = "2018" license = "gpl-3" @@ -20,7 +20,7 @@ url = {version = "^2.2.2", optional=true } pathsearch = {version="^0.2.0", optional=true} [dependencies.bromine] -version = "^0.16.2" +version = "^0.17.1" optional = true features = ["serialize_bincode"] diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 0bc1564..62b8f93 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -19,29 +19,23 @@ pub trait IPCApi { fn namespace() -> &'static str; fn ctx(&self) -> PoolGuard; - async fn emit( - &self, - event_name: &str, - data: T, - ) -> ApiResult { + fn emit(&self, event_name: &str, data: T) -> EmitMetadata { let ctx = self.ctx(); - let meta = ctx.emit_to(Self::namespace(), event_name, data).await?; - - Ok(meta) + ctx.emit_to(Self::namespace(), event_name, data) } - async fn emit_and_get( + async fn emit_and_get( &self, event_name: &str, data: T, timeout: Option, ) -> ApiResult { - let mut meta = self.emit(event_name, data).await?; + let mut meta = self.emit(event_name, data).await_reply(); if let Some(timeout) = timeout { meta = meta.with_timeout(timeout); } - let response = meta.await_reply(&self.ctx()).await?; + let response = meta.await?; Ok(response.payload()?) } @@ -108,7 +102,7 @@ impl ApiClient { #[tracing::instrument(level = "debug", skip(self))] pub async fn info(&self) -> ApiResult { let ctx = self.ctx.acquire(); - let res = ctx.emit("info", ()).await?.await_reply(&ctx).await?; + let res = ctx.emit("info", ()).await_reply().await?; Ok(res.payload::()?) } diff --git a/mediarepo-api/src/tauri_plugin/commands/daemon.rs b/mediarepo-api/src/tauri_plugin/commands/daemon.rs index 01bff9d..6d908cd 100644 --- a/mediarepo-api/src/tauri_plugin/commands/daemon.rs +++ b/mediarepo-api/src/tauri_plugin/commands/daemon.rs @@ -57,7 +57,7 @@ async fn try_connect_daemon(address: String) -> IPCResult<()> { .address(address) .build_client() .await?; - ctx.emit("info", ()).await?.await_reply(&ctx).await?; + ctx.emit("info", ()).await_reply().await?; ctx.stop().await?; Ok(()) } From 6085bed8dec6aa0e82944ac7e5c0b2555b9d8806 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 6 Jan 2022 20:38:28 +0100 Subject: [PATCH 103/116] Update API types for new database schema Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 18 +++++++++------- mediarepo-api/src/client_api/tag.rs | 4 ++-- .../src/tauri_plugin/commands/file.rs | 11 +++++----- .../src/tauri_plugin/commands/tag.rs | 4 ++-- .../src/tauri_plugin/custom_schemes.rs | 8 +++---- mediarepo-api/src/types/files.rs | 21 +++++++++++++++---- mediarepo-api/src/types/identifier.rs | 6 +++--- 8 files changed, 46 insertions(+), 28 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 96e1387..57a9813 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.16.0" +version = "0.17.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 419289f..078663b 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -1,9 +1,9 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ - AddFileRequestHeader, FileMetadataResponse, FileOSMetadata, FilterExpression, FindFilesRequest, - GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, SortKey, - ThumbnailMetadataResponse, UpdateFileNameRequest, + AddFileRequestHeader, FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, + FilterExpression, FindFilesRequest, GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, + ReadFileRequest, SortKey, ThumbnailMetadataResponse, UpdateFileNameRequest, }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; @@ -42,22 +42,26 @@ impl FileApi { /// Returns all known files #[tracing::instrument(level = "debug", skip(self))] - pub async fn all_files(&self) -> ApiResult> { + pub async fn all_files(&self) -> ApiResult> { self.emit_and_get("all_files", ()).await } /// Returns a file by identifier - pub async fn get_file(&self, id: FileIdentifier) -> ApiResult { + pub async fn get_file(&self, id: FileIdentifier) -> ApiResult { self.emit_and_get("get_file", id).await } + pub async fn get_file_metadata(&self, id: FileIdentifier) -> ApiResult { + self.emit_and_get("get_file_metadata", id).await + } + /// Searches for a file by a list of tags #[tracing::instrument(level = "debug", skip(self))] pub async fn find_files( &self, filters: Vec, sort_expression: Vec, - ) -> ApiResult> { + ) -> ApiResult> { self.emit_and_get( "find_files", FindFilesRequest { @@ -129,7 +133,7 @@ impl FileApi { metadata: FileOSMetadata, tags: Vec, bytes: Vec, - ) -> ApiResult { + ) -> ApiResult { let payload = TandemPayload::new( AddFileRequestHeader { metadata, tags }, BytePayload::new(bytes), diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 6541b6a..24dd43e 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -50,8 +50,8 @@ impl TagApi { /// Returns a list of all tags that are assigned to the list of files #[tracing::instrument(level = "debug", skip_all)] - pub async fn get_tags_for_files(&self, hashes: Vec) -> ApiResult> { - self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }) + pub async fn get_tags_for_files(&self, hashes: Vec) -> ApiResult> { + self.emit_and_get("tags_for_files", GetFilesTagsRequest { ids: hashes }) .await } diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index f3d1c20..58c3be4 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -2,7 +2,8 @@ use crate::tauri_plugin::commands::{ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ - FileMetadataResponse, FileOSMetadata, FilterExpression, SortKey, ThumbnailMetadataResponse, + FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, FilterExpression, SortKey, + ThumbnailMetadataResponse, }; use crate::types::identifier::FileIdentifier; use serde::{Deserialize, Serialize}; @@ -18,7 +19,7 @@ pub struct AddFileOptions { } #[tauri::command] -pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { +pub async fn get_all_files(api_state: ApiAccess<'_>) -> PluginResult> { let api = api_state.api().await?; let all_files = api.file.all_files().await?; @@ -30,7 +31,7 @@ pub async fn add_local_file( api_state: ApiAccess<'_>, metadata: FileOSMetadata, options: AddFileOptions, -) -> PluginResult { +) -> PluginResult { let api = api_state.api().await?; let path = PathBuf::from(&metadata.path); let mut tags = Vec::new(); @@ -66,7 +67,7 @@ pub async fn find_files( filters: Vec, sort_by: Vec, api_state: ApiAccess<'_>, -) -> PluginResult> { +) -> PluginResult> { let api = api_state.api().await?; let files = api.file.find_files(filters, sort_by).await?; @@ -111,7 +112,7 @@ pub async fn read_file( let api = api_state.api().await?; let content = api .file - .read_file(FileIdentifier::Hash(hash.clone())) + .read_file(FileIdentifier::CID(hash.clone())) .await?; Ok(content) diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index d54bfa7..4da2a22 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -24,11 +24,11 @@ pub async fn get_tags_for_file( #[tauri::command] pub async fn get_tags_for_files( - hashes: Vec, + ids: Vec, api_state: ApiAccess<'_>, ) -> PluginResult> { let api = api_state.api().await?; - let tags = api.tag.get_tags_for_files(hashes).await?; + let tags = api.tag.get_tags_for_files(ids).await?; Ok(tags) } diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index d02f25f..1419b98 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -69,12 +69,12 @@ async fn content_scheme(app: &AppHandle, request: &Request) -> Re let api = api_state.api().await?; let file = api .file - .get_file(FileIdentifier::Hash(hash.to_string())) + .get_file(FileIdentifier::CID(hash.to_string())) .await?; - let mime = file.mime_type.unwrap_or("image/png".to_string()); + let mime = file.mime_type; let bytes = api .file - .read_file(FileIdentifier::Hash(hash.to_string())) + .read_file(FileIdentifier::CID(hash.to_string())) .await?; tracing::debug!("Received {} content bytes", bytes.len()); buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone()); @@ -121,7 +121,7 @@ async fn thumb_scheme(app: &AppHandle, request: &Request) -> Resu let (thumb, bytes) = api .file .get_thumbnail_of_size( - FileIdentifier::Hash(hash.to_string()), + FileIdentifier::CID(hash.to_string()), ((height as f32 * 0.8) as u32, (width as f32 * 0.8) as u32), ((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32), ) diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 14a245f..7ba65df 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -26,7 +26,7 @@ pub struct GetFileTagsRequest { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GetFilesTagsRequest { - pub hashes: Vec, + pub ids: Vec, } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -74,13 +74,26 @@ pub enum SortDirection { impl Eq for SortDirection {} #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct FileMetadataResponse { +pub struct FileBasicDataResponse { pub id: i64, + pub status: FileStatus, + pub cid: String, + pub mime_type: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum FileStatus { + Imported, + Archived, + Deleted, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FileMetadataResponse { + pub file_id: i64, pub name: Option, pub comment: Option, - pub hash: String, pub file_type: u32, - pub mime_type: Option, pub creation_time: NaiveDateTime, pub change_time: NaiveDateTime, pub import_time: NaiveDateTime, diff --git a/mediarepo-api/src/types/identifier.rs b/mediarepo-api/src/types/identifier.rs index b8df84e..7afef60 100644 --- a/mediarepo-api/src/types/identifier.rs +++ b/mediarepo-api/src/types/identifier.rs @@ -1,7 +1,7 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub enum FileIdentifier { ID(i64), - Hash(String), -} \ No newline at end of file + CID(String), +} From b315024c6aac352877a54bc567327c8150c85618 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 6 Jan 2022 20:49:41 +0100 Subject: [PATCH 104/116] Change file id variant CID to CD Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/tauri_plugin/commands/file.rs | 5 +---- mediarepo-api/src/tauri_plugin/custom_schemes.rs | 6 +++--- mediarepo-api/src/types/identifier.rs | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 494f224..9f74909 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.21.0" +version = "0.22.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index d9a4111..eba42ca 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -122,10 +122,7 @@ pub async fn read_file( Ok(buffer.buf) } else { let api = api_state.api().await?; - let content = api - .file - .read_file(FileIdentifier::CID(hash.clone())) - .await?; + let content = api.file.read_file(FileIdentifier::CD(hash.clone())).await?; Ok(content) } diff --git a/mediarepo-api/src/tauri_plugin/custom_schemes.rs b/mediarepo-api/src/tauri_plugin/custom_schemes.rs index 1419b98..436b8f4 100644 --- a/mediarepo-api/src/tauri_plugin/custom_schemes.rs +++ b/mediarepo-api/src/tauri_plugin/custom_schemes.rs @@ -69,12 +69,12 @@ async fn content_scheme(app: &AppHandle, request: &Request) -> Re let api = api_state.api().await?; let file = api .file - .get_file(FileIdentifier::CID(hash.to_string())) + .get_file(FileIdentifier::CD(hash.to_string())) .await?; let mime = file.mime_type; let bytes = api .file - .read_file(FileIdentifier::CID(hash.to_string())) + .read_file(FileIdentifier::CD(hash.to_string())) .await?; tracing::debug!("Received {} content bytes", bytes.len()); buf_state.add_entry(hash.to_string(), mime.clone(), bytes.clone()); @@ -121,7 +121,7 @@ async fn thumb_scheme(app: &AppHandle, request: &Request) -> Resu let (thumb, bytes) = api .file .get_thumbnail_of_size( - FileIdentifier::CID(hash.to_string()), + FileIdentifier::CD(hash.to_string()), ((height as f32 * 0.8) as u32, (width as f32 * 0.8) as u32), ((height as f32 * 1.2) as u32, (width as f32 * 1.2) as u32), ) diff --git a/mediarepo-api/src/types/identifier.rs b/mediarepo-api/src/types/identifier.rs index 7afef60..3ec9929 100644 --- a/mediarepo-api/src/types/identifier.rs +++ b/mediarepo-api/src/types/identifier.rs @@ -3,5 +3,5 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub enum FileIdentifier { ID(i64), - CID(String), + CD(String), } From a78f1bc65d1630428053d0c5ad1c94826bd4c691 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 6 Jan 2022 21:01:38 +0100 Subject: [PATCH 105/116] Change all tags request back to accept content descriptors Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/tag.rs | 4 ++-- mediarepo-api/src/tauri_plugin/commands/tag.rs | 4 ++-- mediarepo-api/src/types/files.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 9f74909..c688185 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.22.0" +version = "0.23.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 842deac..45e5098 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -63,10 +63,10 @@ impl TagApi { /// Returns a list of all tags that are assigned to the list of files #[tracing::instrument(level = "debug", skip_all)] - pub async fn get_tags_for_files(&self, ids: Vec) -> ApiResult> { + pub async fn get_tags_for_files(&self, cds: Vec) -> ApiResult> { self.emit_and_get( "tags_for_files", - GetFilesTagsRequest { ids }, + GetFilesTagsRequest { cds }, Some(Duration::from_secs(10)), ) .await diff --git a/mediarepo-api/src/tauri_plugin/commands/tag.rs b/mediarepo-api/src/tauri_plugin/commands/tag.rs index 829232f..809e5b0 100644 --- a/mediarepo-api/src/tauri_plugin/commands/tag.rs +++ b/mediarepo-api/src/tauri_plugin/commands/tag.rs @@ -32,11 +32,11 @@ pub async fn get_tags_for_file( #[tauri::command] pub async fn get_tags_for_files( - ids: Vec, + cds: Vec, api_state: ApiAccess<'_>, ) -> PluginResult> { let api = api_state.api().await?; - let tags = api.tag.get_tags_for_files(ids).await?; + let tags = api.tag.get_tags_for_files(cds).await?; Ok(tags) } diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 7ba65df..4b17fe1 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -26,7 +26,7 @@ pub struct GetFileTagsRequest { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GetFilesTagsRequest { - pub ids: Vec, + pub cds: Vec, } #[derive(Clone, Debug, Serialize, Deserialize)] From 673009481011a401fc971e5220cce1b992e51863 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Jan 2022 12:16:23 +0100 Subject: [PATCH 106/116] Fix unused fields and wrong field names Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/types/files.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index c688185..27cbb04 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.23.0" +version = "0.24.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 4b17fe1..3a5c480 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -77,7 +77,7 @@ impl Eq for SortDirection {} pub struct FileBasicDataResponse { pub id: i64, pub status: FileStatus, - pub cid: String, + pub cd: String, pub mime_type: String, } @@ -93,7 +93,6 @@ pub struct FileMetadataResponse { pub file_id: i64, pub name: Option, pub comment: Option, - pub file_type: u32, pub creation_time: NaiveDateTime, pub change_time: NaiveDateTime, pub import_time: NaiveDateTime, From f74bac100bbd122f0b7698e0c57d39256dc1048f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Jan 2022 14:46:02 +0100 Subject: [PATCH 107/116] Add file metadata command Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/tauri_plugin/commands/file.rs | 11 +++++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 3 ++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 27cbb04..eb88c79 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.24.0" +version = "0.24.1" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index eba42ca..697f429 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -97,6 +97,17 @@ pub async fn get_file_thumbnails( Ok(thumbs) } +#[tauri::command] +pub async fn get_file_metadata( + api_state: ApiAccess<'_>, + id: i64, +) -> PluginResult { + let api = api_state.api().await?; + let metadata = api.file.get_file_metadata(FileIdentifier::ID(id)).await?; + + Ok(metadata) +} + #[tauri::command] pub async fn update_file_name( api_state: ApiAccess<'_>, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index b4c37e2..38d84ef 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -65,7 +65,8 @@ impl MediarepoPlugin { get_all_namespaces, get_files, get_repo_metadata, - get_size + get_size, + get_file_metadata ]), } } From f37448876227af2dfbcddde2d9f60176aebbb307 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Jan 2022 18:49:22 +0100 Subject: [PATCH 108/116] Add simple synchronous job API Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/job.rs | 40 +++++++++++++++++++ mediarepo-api/src/client_api/mod.rs | 5 +++ mediarepo-api/src/client_api/repo.rs | 9 +++-- .../src/tauri_plugin/commands/job.rs | 11 +++++ .../src/tauri_plugin/commands/mod.rs | 2 + mediarepo-api/src/tauri_plugin/mod.rs | 3 +- mediarepo-api/src/types/jobs.rs | 14 +++++++ mediarepo-api/src/types/mod.rs | 1 + mediarepo-api/src/types/repo.rs | 2 +- 10 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 mediarepo-api/src/client_api/job.rs create mode 100644 mediarepo-api/src/tauri_plugin/commands/job.rs create mode 100644 mediarepo-api/src/types/jobs.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index eb88c79..b4551b3 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.24.1" +version = "0.24.2" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/job.rs b/mediarepo-api/src/client_api/job.rs new file mode 100644 index 0000000..480c2b3 --- /dev/null +++ b/mediarepo-api/src/client_api/job.rs @@ -0,0 +1,40 @@ +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::jobs::{JobType, RunJobRequest}; +use bromine::context::{Context, PoolGuard, PooledContext}; +use std::time::Duration; + +#[derive(Clone)] +pub struct JobApi { + ctx: PooledContext, +} + +impl IPCApi for JobApi { + fn namespace() -> &'static str { + "jobs" + } + + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() + } +} + +impl JobApi { + pub fn new(ctx: PooledContext) -> Self { + Self { ctx } + } + + /// Runs a job of the given type and returns when it has finished + #[tracing::instrument(level = "debug", skip(self))] + pub async fn run_job(&self, job_type: JobType) -> ApiResult<()> { + self.emit_and_get( + "run_job", + RunJobRequest { + job_type, + sync: true, + }, + Some(Duration::from_secs(300)), + ) + .await + } +} diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 62b8f93..7c52aec 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,11 +1,13 @@ pub mod error; pub mod file; +pub mod job; pub mod protocol; pub mod repo; pub mod tag; use crate::client_api::error::{ApiError, ApiResult}; use crate::client_api::file::FileApi; +use crate::client_api::job::JobApi; use crate::client_api::repo::RepoApi; use crate::client_api::tag::TagApi; use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; @@ -45,6 +47,7 @@ pub struct ApiClient { pub file: FileApi, pub tag: TagApi, pub repo: RepoApi, + pub job: JobApi, } impl Clone for ApiClient { @@ -54,6 +57,7 @@ impl Clone for ApiClient { file: self.file.clone(), tag: self.tag.clone(), repo: self.repo.clone(), + job: self.job.clone(), } } } @@ -65,6 +69,7 @@ impl ApiClient { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), repo: RepoApi::new(ctx.clone()), + job: JobApi::new(ctx.clone()), ctx, } } diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs index 51e71a8..1236f1d 100644 --- a/mediarepo-api/src/client_api/repo.rs +++ b/mediarepo-api/src/client_api/repo.rs @@ -28,19 +28,22 @@ impl RepoApi { /// Returns metadata about the repository #[tracing::instrument(level = "debug", skip(self))] pub async fn get_repo_metadata(&self) -> ApiResult { - self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(3))).await + self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(3))) + .await } /// Returns the size of a given type #[tracing::instrument(level = "debug", skip(self))] pub async fn get_size(&self, size_type: SizeType) -> ApiResult { - self.emit_and_get("size_metadata", size_type, Some(Duration::from_secs(30))).await + self.emit_and_get("size_metadata", size_type, Some(Duration::from_secs(30))) + .await } /// Returns the state of the frontend that is stored in the repo #[tracing::instrument(level = "debug", skip(self))] pub async fn get_frontend_state(&self) -> ApiResult { - self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await + self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))) + .await } /// Sets the state of the frontend diff --git a/mediarepo-api/src/tauri_plugin/commands/job.rs b/mediarepo-api/src/tauri_plugin/commands/job.rs new file mode 100644 index 0000000..8866b8b --- /dev/null +++ b/mediarepo-api/src/tauri_plugin/commands/job.rs @@ -0,0 +1,11 @@ +use crate::tauri_plugin::commands::ApiAccess; +use crate::tauri_plugin::error::PluginResult; +use crate::types::jobs::JobType; + +#[tauri::command] +pub async fn run_job(api_state: ApiAccess<'_>, job_type: JobType) -> PluginResult<()> { + let api = api_state.api().await?; + api.job.run_job(job_type).await?; + + Ok(()) +} diff --git a/mediarepo-api/src/tauri_plugin/commands/mod.rs b/mediarepo-api/src/tauri_plugin/commands/mod.rs index d90e9ff..bd6a259 100644 --- a/mediarepo-api/src/tauri_plugin/commands/mod.rs +++ b/mediarepo-api/src/tauri_plugin/commands/mod.rs @@ -2,6 +2,7 @@ use tauri::State; pub use daemon::*; pub use file::*; +pub use job::*; pub use repo::*; pub use tag::*; @@ -9,6 +10,7 @@ use crate::tauri_plugin::state::{ApiState, AppState, BufferState}; pub mod daemon; pub mod file; +pub mod job; pub mod repo; pub mod tag; diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 38d84ef..95cb06c 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -66,7 +66,8 @@ impl MediarepoPlugin { get_files, get_repo_metadata, get_size, - get_file_metadata + get_file_metadata, + run_job ]), } } diff --git a/mediarepo-api/src/types/jobs.rs b/mediarepo-api/src/types/jobs.rs new file mode 100644 index 0000000..b6260fd --- /dev/null +++ b/mediarepo-api/src/types/jobs.rs @@ -0,0 +1,14 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct RunJobRequest { + pub job_type: JobType, + pub sync: bool, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum JobType { + MigrateContentDescriptors, + CalculateSizes, + CheckIntegrity, +} diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs index 11a3de9..0c05de4 100644 --- a/mediarepo-api/src/types/mod.rs +++ b/mediarepo-api/src/types/mod.rs @@ -1,5 +1,6 @@ pub mod files; pub mod identifier; +pub mod jobs; pub mod misc; pub mod repo; pub mod tags; diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index 6fad584..3bfcd6a 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -27,4 +27,4 @@ pub enum SizeType { FileFolder, ThumbFolder, DatabaseFile, -} \ No newline at end of file +} From 4c6617d86b0c582b190c775917548af36c0ea9c2 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Jan 2022 19:09:13 +0100 Subject: [PATCH 109/116] Add missing trait derives to SizeType Signed-off-by: trivernis --- mediarepo-api/src/types/repo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs index 3bfcd6a..645f7dc 100644 --- a/mediarepo-api/src/types/repo.rs +++ b/mediarepo-api/src/types/repo.rs @@ -21,7 +21,7 @@ pub struct SizeMetadata { pub size: u64, } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, PartialOrd, Eq, Hash)] pub enum SizeType { Total, FileFolder, From 3924fd8f4544e9ec7c820e7f8955af0906a23503 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Jan 2022 20:50:53 +0100 Subject: [PATCH 110/116] Increase job timeout Signed-off-by: trivernis --- mediarepo-api/src/client_api/job.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediarepo-api/src/client_api/job.rs b/mediarepo-api/src/client_api/job.rs index 480c2b3..fe05ac8 100644 --- a/mediarepo-api/src/client_api/job.rs +++ b/mediarepo-api/src/client_api/job.rs @@ -33,7 +33,7 @@ impl JobApi { job_type, sync: true, }, - Some(Duration::from_secs(300)), + Some(Duration::from_secs(3600)), ) .await } From ddde0ea113bac7d228cc9947aeb427a455a25733 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 9 Jan 2022 18:32:16 +0100 Subject: [PATCH 111/116] Add shutdown command to gracefully stop running daemons Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/mod.rs | 13 +++++++++++++ mediarepo-api/src/tauri_plugin/commands/repo.rs | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index b4551b3..7876ed4 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.24.2" +version = "0.25.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 7c52aec..c3fbf2f 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -111,9 +111,22 @@ impl ApiClient { Ok(res.payload::()?) } + /// Shuts down the daemon that the client is connected to. + #[tracing::instrument(level = "debug", skip(self))] + pub async fn shutdown_daemon(&self) -> ApiResult<()> { + self.ctx + .acquire() + .emit("shutdown", ()) + .await_reply() + .with_timeout(Duration::from_secs(5)) + .await?; + Ok(()) + } + #[tracing::instrument(level = "debug", skip(self))] pub async fn exit(self) -> ApiResult<()> { let ctx = (*self.ctx.acquire()).clone(); + ctx.stop().await?; Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 956b0c3..a8a3373 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -140,6 +140,11 @@ pub async fn close_local_repository( let mut active_repo = app_state.active_repo.write().await; if let Some(path) = mem::take(&mut *active_repo).and_then(|r| r.path) { + if let Ok(api) = api_state.api().await { + if let Err(e) = api.shutdown_daemon().await { + tracing::error!("failed to ask the daemon to shut down daemon {:?}", e); + } + } app_state.stop_running_daemon(&path).await?; } api_state.disconnect().await; From 717de6684abe5827ee16a85db44ce525c0bf8eb8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 9 Jan 2022 19:24:52 +0100 Subject: [PATCH 112/116] Add simple sleep after shutdown command to allow a full repository shutdown Signed-off-by: trivernis --- mediarepo-api/src/tauri_plugin/commands/repo.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index a8a3373..beca33a 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -144,6 +144,8 @@ pub async fn close_local_repository( if let Err(e) = api.shutdown_daemon().await { tracing::error!("failed to ask the daemon to shut down daemon {:?}", e); } + // allow the repository to gracefully stop within 1 second + tokio::time::sleep(Duration::from_secs(1)).await; } app_state.stop_running_daemon(&path).await?; } From ed28bc68c88f3231d009d167788f6ea48696cb41 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 9 Jan 2022 20:27:55 +0100 Subject: [PATCH 113/116] Change filter type to accept property comparators too Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 5 +- .../src/tauri_plugin/commands/file.rs | 4 +- .../src/test/test_type_serialization.rs | 15 +++- mediarepo-api/src/types/files.rs | 45 +----------- mediarepo-api/src/types/filtering.rs | 73 +++++++++++++++++++ mediarepo-api/src/types/mod.rs | 1 + 7 files changed, 94 insertions(+), 51 deletions(-) create mode 100644 mediarepo-api/src/types/filtering.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 7876ed4..928d3cc 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.25.0" +version = "0.26.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 7ed1369..1ef4a2f 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -2,9 +2,10 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ AddFileRequestHeader, FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, - FilterExpression, FindFilesRequest, GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, - ReadFileRequest, SortKey, ThumbnailMetadataResponse, UpdateFileNameRequest, + GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, + ThumbnailMetadataResponse, UpdateFileNameRequest, }; +use crate::types::filtering::{FilterExpression, FindFilesRequest, SortKey}; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; use bromine::context::{PoolGuard, PooledContext}; diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 697f429..61ae057 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -2,9 +2,9 @@ use crate::tauri_plugin::commands::{ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ - FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, FilterExpression, SortKey, - ThumbnailMetadataResponse, + FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, ThumbnailMetadataResponse, }; +use crate::types::filtering::{FilterExpression, SortKey}; use crate::types::identifier::FileIdentifier; use serde::{Deserialize, Serialize}; use std::path::PathBuf; diff --git a/mediarepo-api/src/test/test_type_serialization.rs b/mediarepo-api/src/test/test_type_serialization.rs index d183d46..56ecb83 100644 --- a/mediarepo-api/src/test/test_type_serialization.rs +++ b/mediarepo-api/src/test/test_type_serialization.rs @@ -1,9 +1,11 @@ -use crate::types::files::{ - FilterExpression, GetFileThumbnailOfSizeRequest, SortDirection, SortKey, TagQuery, +use crate::types::files::GetFileThumbnailOfSizeRequest; +use crate::types::filtering::{ + FilterExpression, SortDirection, SortKey, TagQuery, ValueComparator, }; use crate::types::identifier::FileIdentifier; use bromine::payload::DynamicSerializer; use bromine::prelude::IPCResult; +use chrono::NaiveDateTime; use serde::de::DeserializeOwned; use serde::Serialize; @@ -45,6 +47,15 @@ fn it_serializes_sort_keys() { test_serialization(SortKey::FileName(SortDirection::Descending)).unwrap(); } +#[test] +fn it_serializes_value_comparators() { + test_serialization(ValueComparator::Between(( + NaiveDateTime::from_timestamp(100, 0), + NaiveDateTime::from_timestamp(100, 10), + ))) + .unwrap(); +} + fn test_serialization(data: T) -> IPCResult<()> { let serializer = DynamicSerializer::first_available(); let bytes = serializer.serialize(data)?; diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 3a5c480..7a67630 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -1,6 +1,7 @@ use crate::types::identifier::FileIdentifier; use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; +use std::fmt::Debug; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ReadFileRequest { @@ -29,50 +30,6 @@ pub struct GetFilesTagsRequest { pub cds: Vec, } -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct FindFilesRequest { - pub filters: Vec, - pub sort_expression: Vec, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub enum FilterExpression { - OrExpression(Vec), - Query(TagQuery), -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TagQuery { - pub negate: bool, - pub tag: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub enum SortKey { - Namespace(SortNamespace), - FileName(SortDirection), - FileSize(SortDirection), - FileImportedTime(SortDirection), - FileCreatedTime(SortDirection), - FileChangeTime(SortDirection), - FileType(SortDirection), - NumTags(SortDirection), -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct SortNamespace { - pub name: String, - pub direction: SortDirection, -} - -#[derive(Clone, Debug, Serialize, Deserialize, Ord, PartialOrd, PartialEq)] -pub enum SortDirection { - Ascending, - Descending, -} - -impl Eq for SortDirection {} - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FileBasicDataResponse { pub id: i64, diff --git a/mediarepo-api/src/types/filtering.rs b/mediarepo-api/src/types/filtering.rs new file mode 100644 index 0000000..916db73 --- /dev/null +++ b/mediarepo-api/src/types/filtering.rs @@ -0,0 +1,73 @@ +use crate::types::files::FileStatus; +use chrono::NaiveDateTime; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FindFilesRequest { + pub filters: Vec, + pub sort_expression: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum FilterExpression { + OrExpression(Vec), + Query(FilterQuery), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum FilterQuery { + Tag(TagQuery), + Property(PropertyQuery), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TagQuery { + pub negate: bool, + pub tag: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum PropertyQuery { + Status(FileStatus), + FileSize(ValueComparator), + ImportedTime(ValueComparator), + ChangedTime(ValueComparator), + CreatedTime(ValueComparator), + TagCount(ValueComparator), + Cd(String), + Id(i64), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum ValueComparator { + Less(T), + Equal(T), + Greater(T), + Between((T, T)), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum SortKey { + Namespace(SortNamespace), + FileName(SortDirection), + FileSize(SortDirection), + FileImportedTime(SortDirection), + FileCreatedTime(SortDirection), + FileChangeTime(SortDirection), + FileType(SortDirection), + NumTags(SortDirection), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SortNamespace { + pub name: String, + pub direction: SortDirection, +} + +#[derive(Clone, Debug, Serialize, Deserialize, Ord, PartialOrd, PartialEq)] +pub enum SortDirection { + Ascending, + Descending, +} + +impl Eq for SortDirection {} diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs index 0c05de4..f76121e 100644 --- a/mediarepo-api/src/types/mod.rs +++ b/mediarepo-api/src/types/mod.rs @@ -1,4 +1,5 @@ pub mod files; +pub mod filtering; pub mod identifier; pub mod jobs; pub mod misc; From 0e1538bdd3aeb7d8414f1d2c80770f5dcc0f50e5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 10 Jan 2022 20:52:34 +0100 Subject: [PATCH 114/116] Fix tests Signed-off-by: trivernis --- mediarepo-api/src/test/test_type_serialization.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mediarepo-api/src/test/test_type_serialization.rs b/mediarepo-api/src/test/test_type_serialization.rs index 56ecb83..838c2d8 100644 --- a/mediarepo-api/src/test/test_type_serialization.rs +++ b/mediarepo-api/src/test/test_type_serialization.rs @@ -1,6 +1,6 @@ use crate::types::files::GetFileThumbnailOfSizeRequest; use crate::types::filtering::{ - FilterExpression, SortDirection, SortKey, TagQuery, ValueComparator, + FilterExpression, FilterQuery, SortDirection, SortKey, TagQuery, ValueComparator, }; use crate::types::identifier::FileIdentifier; use bromine::payload::DynamicSerializer; @@ -35,10 +35,10 @@ fn it_serializes_tag_queries() { #[test] fn it_serializes_filter_expressions() { - test_serialization(FilterExpression::Query(TagQuery { + test_serialization(FilterExpression::Query(FilterQuery::Tag(TagQuery { tag: String::from("World"), negate: false, - })) + }))) .unwrap(); } From b0de5ab10a0daefb7b04c757ab283ed78679f8b1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 15 Jan 2022 18:39:20 +0100 Subject: [PATCH 115/116] Add api to change the status of a file and delete it permanently Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/file.rs | 90 +++++++++++++++++----------- mediarepo-api/src/types/files.rs | 6 ++ 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 928d3cc..e350b59 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.26.0" +version = "0.27.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 1ef4a2f..b0fb29b 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -1,9 +1,9 @@ use crate::client_api::error::ApiResult; use crate::client_api::IPCApi; use crate::types::files::{ - AddFileRequestHeader, FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, + AddFileRequestHeader, FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, FileStatus, GetFileThumbnailOfSizeRequest, GetFileThumbnailsRequest, ReadFileRequest, - ThumbnailMetadataResponse, UpdateFileNameRequest, + ThumbnailMetadataResponse, UpdateFileNameRequest, UpdateFileStatusRequest, }; use crate::types::filtering::{FilterExpression, FindFilesRequest, SortKey}; use crate::types::identifier::FileIdentifier; @@ -103,6 +103,60 @@ impl FileApi { Ok(payload.into_inner()) } + /// Adds a file with predefined tags + #[tracing::instrument(level = "debug", skip(self, bytes))] + pub async fn add_file( + &self, + metadata: FileOSMetadata, + tags: Vec, + bytes: Vec, + ) -> ApiResult { + let payload = TandemPayload::new( + AddFileRequestHeader { metadata, tags }, + BytePayload::new(bytes), + ); + + self.emit_and_get("add_file", payload, Some(Duration::from_secs(5))) + .await + } + + /// Updates a files name + #[tracing::instrument(level = "debug", skip(self))] + pub async fn update_file_name( + &self, + file_id: FileIdentifier, + name: String, + ) -> ApiResult { + self.emit_and_get( + "update_file_name", + UpdateFileNameRequest { file_id, name }, + Some(Duration::from_secs(1)), + ) + .await + } + + /// Updates the status of a file + #[tracing::instrument(level = "debug", skip(self))] + pub async fn update_file_status( + &self, + file_id: FileIdentifier, + status: FileStatus, + ) -> ApiResult { + self.emit_and_get( + "update_file_status", + UpdateFileStatusRequest { status, file_id }, + Some(Duration::from_secs(1)), + ) + .await + } + + /// Permanently deletes a file from the disk and database + #[tracing::instrument(level = "debug", skip(self))] + pub async fn delete_file(&self, file_id: FileIdentifier) -> ApiResult<()> { + self.emit_and_get("delete_file", file_id, Some(Duration::from_secs(10))) + .await + } + /// Returns a list of all thumbnails of the file #[tracing::instrument(level = "debug", skip(self))] pub async fn get_file_thumbnails( @@ -141,38 +195,6 @@ impl FileApi { Ok((metadata.data(), bytes.into_inner())) } - /// Updates a files name - #[tracing::instrument(level = "debug", skip(self))] - pub async fn update_file_name( - &self, - file_id: FileIdentifier, - name: String, - ) -> ApiResult { - self.emit_and_get( - "update_file_name", - UpdateFileNameRequest { file_id, name }, - Some(Duration::from_secs(1)), - ) - .await - } - - /// Adds a file with predefined tags - #[tracing::instrument(level = "debug", skip(self, bytes))] - pub async fn add_file( - &self, - metadata: FileOSMetadata, - tags: Vec, - bytes: Vec, - ) -> ApiResult { - let payload = TandemPayload::new( - AddFileRequestHeader { metadata, tags }, - BytePayload::new(bytes), - ); - - self.emit_and_get("add_file", payload, Some(Duration::from_secs(5))) - .await - } - /// Deletes all thumbnails of a file to regenerate them when requested #[tracing::instrument(level = "debug", skip(self))] pub async fn delete_thumbnails(&self, file_id: FileIdentifier) -> ApiResult<()> { diff --git a/mediarepo-api/src/types/files.rs b/mediarepo-api/src/types/files.rs index 7a67630..bb8146e 100644 --- a/mediarepo-api/src/types/files.rs +++ b/mediarepo-api/src/types/files.rs @@ -78,6 +78,12 @@ pub struct UpdateFileNameRequest { pub name: String, } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct UpdateFileStatusRequest { + pub file_id: FileIdentifier, + pub status: FileStatus, +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AddFileRequestHeader { pub metadata: FileOSMetadata, From cfdb88e304eec8098385c41735254c80b977ccb9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 15 Jan 2022 19:02:49 +0100 Subject: [PATCH 116/116] Add commands for deleting and changing file status Signed-off-by: trivernis --- .../src/tauri_plugin/commands/file.rs | 26 ++++++++++++++++++- mediarepo-api/src/tauri_plugin/mod.rs | 4 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/file.rs b/mediarepo-api/src/tauri_plugin/commands/file.rs index 61ae057..5cf7592 100644 --- a/mediarepo-api/src/tauri_plugin/commands/file.rs +++ b/mediarepo-api/src/tauri_plugin/commands/file.rs @@ -2,7 +2,8 @@ use crate::tauri_plugin::commands::{ApiAccess, BufferAccess}; use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::utils::system_time_to_naive_date_time; use crate::types::files::{ - FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, ThumbnailMetadataResponse, + FileBasicDataResponse, FileMetadataResponse, FileOSMetadata, FileStatus, + ThumbnailMetadataResponse, }; use crate::types::filtering::{FilterExpression, SortKey}; use crate::types::identifier::FileIdentifier; @@ -123,6 +124,29 @@ pub async fn update_file_name( Ok(metadata) } +#[tauri::command] +pub async fn update_file_status( + api_state: ApiAccess<'_>, + id: i64, + status: FileStatus, +) -> PluginResult { + let api = api_state.api().await?; + let file = api + .file + .update_file_status(FileIdentifier::ID(id), status) + .await?; + + Ok(file) +} + +#[tauri::command] +pub async fn delete_file(api_state: ApiAccess<'_>, id: i64) -> PluginResult<()> { + let api = api_state.api().await?; + api.file.delete_file(FileIdentifier::ID(id)).await?; + + Ok(()) +} + #[tauri::command] pub async fn read_file( api_state: ApiAccess<'_>, diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index 95cb06c..5779dbe 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -67,7 +67,9 @@ impl MediarepoPlugin { get_repo_metadata, get_size, get_file_metadata, - run_job + run_job, + update_file_status, + delete_file ]), } }