diff --git a/mediarepo-daemon/Cargo.lock b/mediarepo-daemon/Cargo.lock index 9bec627..c3b3c35 100644 --- a/mediarepo-daemon/Cargo.lock +++ b/mediarepo-daemon/Cargo.lock @@ -873,7 +873,6 @@ dependencies = [ "chrono", "mediarepo-core", "mediarepo-model", - "rmp-ipc", "serde", "tokio", ] @@ -1412,9 +1411,9 @@ dependencies = [ [[package]] name = "rmp-ipc" -version = "0.4.3" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2ada2c7329ea55e1e80f2a1ae9ddb123f93ae4b349d493273138118feadfecb" +checksum = "20fd9347d94995cc197ef2267a8c6b82f4519d3cb5c333caf57edb1376077dd1" dependencies = [ "lazy_static", "log", diff --git a/mediarepo-daemon/mediarepo-core/Cargo.lock b/mediarepo-daemon/mediarepo-core/Cargo.lock index 3891482..d3fa356 100644 --- a/mediarepo-daemon/mediarepo-core/Cargo.lock +++ b/mediarepo-daemon/mediarepo-core/Cargo.lock @@ -1034,9 +1034,9 @@ dependencies = [ [[package]] name = "rmp-ipc" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af495ca346adde10163eb920b52b5a3992fb209b31df58ece4ed0f855661978c" +checksum = "20fd9347d94995cc197ef2267a8c6b82f4519d3cb5c333caf57edb1376077dd1" dependencies = [ "lazy_static", "log", diff --git a/mediarepo-daemon/mediarepo-core/Cargo.toml b/mediarepo-daemon/mediarepo-core/Cargo.toml index 9f8cb8c..f727394 100644 --- a/mediarepo-daemon/mediarepo-core/Cargo.toml +++ b/mediarepo-daemon/mediarepo-core/Cargo.toml @@ -12,7 +12,7 @@ multibase = "0.9.1" base64 = "0.13.0" toml = "0.5.8" serde = "1.0.130" -rmp-ipc = "0.4.0" +rmp-ipc = "0.5.0" typemap_rev = "0.1.5" image = "0.23.14" futures = "0.3.17" diff --git a/mediarepo-daemon/mediarepo-core/src/lib.rs b/mediarepo-daemon/mediarepo-core/src/lib.rs index 0e29cc4..b0fae21 100644 --- a/mediarepo-daemon/mediarepo-core/src/lib.rs +++ b/mediarepo-daemon/mediarepo-core/src/lib.rs @@ -7,3 +7,4 @@ pub mod type_keys; pub use futures; pub use image; +pub use rmp_ipc; diff --git a/mediarepo-daemon/mediarepo-database/Cargo.lock b/mediarepo-daemon/mediarepo-database/Cargo.lock index d0390d2..55d66fe 100644 --- a/mediarepo-daemon/mediarepo-database/Cargo.lock +++ b/mediarepo-daemon/mediarepo-database/Cargo.lock @@ -1267,9 +1267,9 @@ dependencies = [ [[package]] name = "rmp-ipc" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af495ca346adde10163eb920b52b5a3992fb209b31df58ece4ed0f855661978c" +checksum = "20fd9347d94995cc197ef2267a8c6b82f4519d3cb5c333caf57edb1376077dd1" dependencies = [ "lazy_static", "log", diff --git a/mediarepo-daemon/mediarepo-model/Cargo.lock b/mediarepo-daemon/mediarepo-model/Cargo.lock index 29c5112..280c6fe 100644 --- a/mediarepo-daemon/mediarepo-model/Cargo.lock +++ b/mediarepo-daemon/mediarepo-model/Cargo.lock @@ -1298,9 +1298,9 @@ dependencies = [ [[package]] name = "rmp-ipc" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af495ca346adde10163eb920b52b5a3992fb209b31df58ece4ed0f855661978c" +checksum = "20fd9347d94995cc197ef2267a8c6b82f4519d3cb5c333caf57edb1376077dd1" dependencies = [ "lazy_static", "log", diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.lock b/mediarepo-daemon/mediarepo-socket/Cargo.lock index 64c2dc4..9686736 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.lock +++ b/mediarepo-daemon/mediarepo-socket/Cargo.lock @@ -789,7 +789,6 @@ dependencies = [ "chrono", "mediarepo-core", "mediarepo-model", - "rmp-ipc", "serde", "tokio", ] @@ -1311,9 +1310,9 @@ dependencies = [ [[package]] name = "rmp-ipc" -version = "0.4.3" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2ada2c7329ea55e1e80f2a1ae9ddb123f93ae4b349d493273138118feadfecb" +checksum = "20fd9347d94995cc197ef2267a8c6b82f4519d3cb5c333caf57edb1376077dd1" dependencies = [ "lazy_static", "log", diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.toml b/mediarepo-daemon/mediarepo-socket/Cargo.toml index f22a635..480d55d 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.toml +++ b/mediarepo-daemon/mediarepo-socket/Cargo.toml @@ -6,7 +6,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rmp-ipc = "0.4.3" serde = "1.0.130" [dependencies.mediarepo-core] diff --git a/mediarepo-daemon/mediarepo-socket/src/lib.rs b/mediarepo-daemon/mediarepo-socket/src/lib.rs index 0afaeee..0cc9bf6 100644 --- a/mediarepo-daemon/mediarepo-socket/src/lib.rs +++ b/mediarepo-daemon/mediarepo-socket/src/lib.rs @@ -1,17 +1,14 @@ use crate::types::responses::InfoResponse; -use rmp_ipc::context::Context; -use rmp_ipc::error::Result; -use rmp_ipc::{Event, IPCBuilder}; +use mediarepo_core::rmp_ipc::prelude::*; mod namespaces; pub mod types; pub fn get_builder(address: &str) -> IPCBuilder { - namespaces::build_namespaces(IPCBuilder::new().address(address)) - .on("info", |c, e| Box::pin(info(c, e))) + namespaces::build_namespaces(IPCBuilder::new().address(address)).on("info", callback!(info)) } -async fn info(ctx: &Context, event: Event) -> Result<()> { +async fn info(ctx: &Context, event: Event) -> IPCResult<()> { let response = InfoResponse { name: env!("CARGO_PKG_NAME").to_string(), version: env!("CARGO_PKG_VERSION").to_string(), diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs index 6fccea4..186e0ed 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs @@ -3,126 +3,126 @@ use crate::types::requests::{ }; use crate::types::responses::{FileResponse, ThumbnailResponse}; use mediarepo_core::error::{RepoError, RepoResult}; +use mediarepo_core::rmp_ipc::prelude::*; use mediarepo_model::file::File; use mediarepo_model::repo::Repo; use mediarepo_model::type_keys::RepoKey; -use rmp_ipc::context::Context; -use rmp_ipc::error::Result; -use rmp_ipc::{Event, NamespaceBuilder}; use std::path::PathBuf; use tokio::io::AsyncReadExt; -pub const FILES_NAMESPACE: &str = "files"; +pub struct FilesNamespace; -pub fn build(builder: NamespaceBuilder) -> NamespaceBuilder { - builder - .on("all_files", |c, e| Box::pin(all_files(c, e))) - .on("add_file", |c, e| Box::pin(add_file(c, e))) - .on("read_file", |c, e| Box::pin(read_file(c, e))) - .on("get_thumbnails", |c, e| Box::pin(get_file_thumbnails(c, e))) - .on("read_thumbnail", |c, e| Box::pin(read_thumbnail(c, e))) -} - -/// Returns a list of all files -async fn all_files(ctx: &Context, event: Event) -> Result<()> { - let files = { - let data = ctx.data.read().await; - let repo = data.get::().unwrap(); - repo.files().await? - }; - let responses: Vec = files.into_iter().map(FileResponse::from).collect(); - ctx.emitter - .emit_response_to(event.id(), FILES_NAMESPACE, "all_files", responses) - .await?; +impl NamespaceProvider for FilesNamespace { + fn name() -> String { + String::from("files") + } - Ok(()) + fn register(handler: &mut EventHandler) { + handler.on("all_files", callback!(Self::all_files)); + handler.on("add_file", callback!(Self::add_file)); + handler.on("read_file", callback!(Self::read_file)); + handler.on("get_thumbnails", callback!(Self::thumbnails)); + handler.on("read_thumbnail", callback!(Self::read_thumbnail)); + } } -/// Adds a file to the repository -async fn add_file(ctx: &Context, event: Event) -> Result<()> { - let request = event.data::()?; - let path = PathBuf::from(request.path); - let file = { - let data = ctx.data.read().await; - let repo = data.get::().unwrap(); - repo.add_file_by_path(path).await? - }; - ctx.emitter - .emit_response_to( - event.id(), - FILES_NAMESPACE, - "add_file", - FileResponse::from(file), - ) - .await?; +impl FilesNamespace { + /// Returns a list of all files + async fn all_files(ctx: &Context, event: Event) -> IPCResult<()> { + let files = { + let data = ctx.data.read().await; + let repo = data.get::().unwrap(); + repo.files().await? + }; + let responses: Vec = files.into_iter().map(FileResponse::from).collect(); + ctx.emitter + .emit_response_to(event.id(), Self::name(), "all_files", responses) + .await?; - Ok(()) -} + Ok(()) + } -/// Reads the binary contents of a file -async fn read_file(ctx: &Context, event: Event) -> Result<()> { - let request = event.data::()?; - let mut reader = { - let data = ctx.data.read().await; - let repo = data.get::().unwrap(); - let file = file_by_identifier(request, repo) - .await? - .ok_or_else(|| RepoError::from("File not found")); - file?.get_reader().await? - }; - let mut buf = Vec::new(); - reader.read_to_end(&mut buf).await?; - ctx.emitter - .emit_response_to(event.id(), FILES_NAMESPACE, "read_file", buf) - .await?; + /// Adds a file to the repository + async fn add_file(ctx: &Context, event: Event) -> IPCResult<()> { + let request = event.data::()?; + let path = PathBuf::from(request.path); + let file = { + let data = ctx.data.read().await; + let repo = data.get::().unwrap(); + repo.add_file_by_path(path).await? + }; + ctx.emitter + .emit_response_to( + event.id(), + Self::name(), + "add_file", + FileResponse::from(file), + ) + .await?; - Ok(()) -} + Ok(()) + } -/// Returns a list of available thumbnails of a file -async fn get_file_thumbnails(ctx: &Context, event: Event) -> Result<()> { - let request = event.data::()?; - let data = ctx.data.read().await; - let repo = data.get::().unwrap(); - let file = file_by_identifier(request, repo) - .await? - .ok_or_else(|| RepoError::from("File not found"))?; - let thumbnails = file.thumbnails().await?; - let thumb_responses: Vec = thumbnails - .into_iter() - .map(ThumbnailResponse::from) - .collect(); - ctx.emitter - .emit_response_to( - event.id(), - FILES_NAMESPACE, - "get_thumbnails", - thumb_responses, - ) - .await?; + /// Reads the binary contents of a file + async fn read_file(ctx: &Context, event: Event) -> IPCResult<()> { + let request = event.data::()?; + let mut reader = { + let data = ctx.data.read().await; + let repo = data.get::().unwrap(); + let file = file_by_identifier(request, repo) + .await? + .ok_or_else(|| RepoError::from("File not found")); + file?.get_reader().await? + }; + let mut buf = Vec::new(); + reader.read_to_end(&mut buf).await?; + ctx.emitter + .emit_response_to(event.id(), Self::name(), "read_file", buf) + .await?; - Ok(()) -} + Ok(()) + } -/// Reads a thumbnail for the given thumbnail hash -async fn read_thumbnail(ctx: &Context, event: Event) -> Result<()> { - let hash = event.data::()?; - let mut reader = { + /// Returns a list of available thumbnails of a file + async fn thumbnails(ctx: &Context, event: Event) -> IPCResult<()> { + let request = event.data::()?; let data = ctx.data.read().await; let repo = data.get::().unwrap(); - let thumbnail = repo - .thumbnail_by_hash(&hash) + let file = file_by_identifier(request, repo) .await? - .ok_or_else(|| RepoError::from("Thumbnail not found"))?; - thumbnail.get_reader().await? - }; - let mut buf = Vec::new(); - reader.read_to_end(&mut buf).await?; - ctx.emitter - .emit_response_to(event.id(), FILES_NAMESPACE, "read_thumbnail", buf) - .await?; + .ok_or_else(|| RepoError::from("File not found"))?; + let thumbnails = file.thumbnails().await?; + let thumb_responses: Vec = thumbnails + .into_iter() + .map(ThumbnailResponse::from) + .collect(); + ctx.emitter + .emit_response_to(event.id(), Self::name(), "get_thumbnails", thumb_responses) + .await?; + + Ok(()) + } - Ok(()) + /// Reads a thumbnail for the given thumbnail hash + async fn read_thumbnail(ctx: &Context, event: Event) -> IPCResult<()> { + let hash = event.data::()?; + let mut reader = { + let data = ctx.data.read().await; + let repo = data.get::().unwrap(); + let thumbnail = repo + .thumbnail_by_hash(&hash) + .await? + .ok_or_else(|| RepoError::from("Thumbnail not found"))?; + thumbnail.get_reader().await? + }; + let mut buf = Vec::new(); + reader.read_to_end(&mut buf).await?; + ctx.emitter + .emit_response_to(event.id(), Self::name(), "read_thumbnail", buf) + .await?; + + Ok(()) + } } async fn file_by_identifier(identifier: FileIdentifier, repo: &Repo) -> RepoResult> { diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/mod.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/mod.rs index 088d5fa..bb68efe 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/mod.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/mod.rs @@ -1,10 +1,7 @@ -use crate::namespaces::files::FILES_NAMESPACE; -use rmp_ipc::IPCBuilder; +use mediarepo_core::rmp_ipc::{namespace, namespace::Namespace, IPCBuilder}; pub mod files; pub fn build_namespaces(builder: IPCBuilder) -> IPCBuilder { - let builder = files::build(builder.namespace(FILES_NAMESPACE)).build(); - - builder + builder.add_namespace(namespace!(files::FilesNamespace)) }