From 957e618d84ced67660c1e565604a75f6cfff5a5d Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 10 Oct 2021 10:55:26 +0200 Subject: [PATCH] Expose socket types Signed-off-by: trivernis --- mediarepo-daemon/Cargo.toml | 9 ++++++ .../mediarepo-model/src/file_type.rs | 2 +- .../mediarepo-socket/src/namespaces/files.rs | 29 +++++++++++++++++-- .../mediarepo-socket/src/types/mod.rs | 3 +- .../mediarepo-socket/src/types/requests.rs | 6 ++++ .../types/{file_response.rs => responses.rs} | 4 +-- mediarepo-daemon/src/lib.rs | 1 + 7 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-socket/src/types/requests.rs rename mediarepo-daemon/mediarepo-socket/src/types/{file_response.rs => responses.rs} (90%) create mode 100644 mediarepo-daemon/src/lib.rs diff --git a/mediarepo-daemon/Cargo.toml b/mediarepo-daemon/Cargo.toml index a32bf64..c85e78d 100644 --- a/mediarepo-daemon/Cargo.toml +++ b/mediarepo-daemon/Cargo.toml @@ -5,6 +5,15 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "mediarepo" +path = "src/main.rs" + +[lib] +name = "mediarepo" +crate-type = ["lib"] + [dependencies] toml = "0.5.8" structopt = "0.3.23" diff --git a/mediarepo-daemon/mediarepo-model/src/file_type.rs b/mediarepo-daemon/mediarepo-model/src/file_type.rs index 11088d6..1c34c24 100644 --- a/mediarepo-daemon/mediarepo-model/src/file_type.rs +++ b/mediarepo-daemon/mediarepo-model/src/file_type.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize, PartialOrd, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq)] pub enum FileType { Unknown = 0, Image = 1, diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs index f5ff283..3d4c29d 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs @@ -1,13 +1,17 @@ -use crate::types::file_response::FileResponse; +use crate::types::requests::AddFileRequest; +use crate::types::responses::FileResponse; 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; pub const FILES_NAMESPACE: &str = "files"; pub fn build(builder: NamespaceBuilder) -> NamespaceBuilder { - builder.on("all_files", |c, e| Box::pin(all_files(c, e))) + builder + .on("all_files", |c, e| Box::pin(all_files(c, e))) + .on("add_file", |c, e| Box::pin(add_file(c, e))) } /// Returns a list of all files @@ -24,3 +28,24 @@ async fn all_files(ctx: &Context, event: Event) -> Result<()> { Ok(()) } + +/// 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?; + + Ok(()) +} diff --git a/mediarepo-daemon/mediarepo-socket/src/types/mod.rs b/mediarepo-daemon/mediarepo-socket/src/types/mod.rs index 40d7a94..116da0f 100644 --- a/mediarepo-daemon/mediarepo-socket/src/types/mod.rs +++ b/mediarepo-daemon/mediarepo-socket/src/types/mod.rs @@ -1 +1,2 @@ -pub mod file_response; +pub mod requests; +pub mod responses; diff --git a/mediarepo-daemon/mediarepo-socket/src/types/requests.rs b/mediarepo-daemon/mediarepo-socket/src/types/requests.rs new file mode 100644 index 0000000..0472571 --- /dev/null +++ b/mediarepo-daemon/mediarepo-socket/src/types/requests.rs @@ -0,0 +1,6 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct AddFileRequest { + pub path: String, +} diff --git a/mediarepo-daemon/mediarepo-socket/src/types/file_response.rs b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs similarity index 90% rename from mediarepo-daemon/mediarepo-socket/src/types/file_response.rs rename to mediarepo-daemon/mediarepo-socket/src/types/responses.rs index 3f9ca9a..285fdd9 100644 --- a/mediarepo-daemon/mediarepo-socket/src/types/file_response.rs +++ b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs @@ -1,9 +1,9 @@ use chrono::NaiveDateTime; use mediarepo_model::file::File; use mediarepo_model::file_type::FileType; -use serde::{Deserialize, Serialize}; +use serde::Serialize; -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct FileResponse { pub name: Option, pub comment: Option, diff --git a/mediarepo-daemon/src/lib.rs b/mediarepo-daemon/src/lib.rs new file mode 100644 index 0000000..0af28f4 --- /dev/null +++ b/mediarepo-daemon/src/lib.rs @@ -0,0 +1 @@ +pub use mediarepo_socket::types::*;