From 9fde0f08e2165c77651c5cabec49d8791c31789b Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 24 Oct 2021 13:09:54 +0200 Subject: [PATCH] Use mediarepo-api for api types and remove lib config Signed-off-by: trivernis --- mediarepo-daemon/Cargo.lock | 12 +++ mediarepo-daemon/Cargo.toml | 37 ++------- mediarepo-daemon/mediarepo-socket/Cargo.lock | 12 +++ mediarepo-daemon/mediarepo-socket/Cargo.toml | 6 +- .../mediarepo-socket/src/from_model.rs | 47 ++++++++++++ mediarepo-daemon/mediarepo-socket/src/lib.rs | 4 +- .../mediarepo-socket/src/namespaces/files.rs | 32 +++++--- .../mediarepo-socket/src/namespaces/tags.rs | 11 +-- .../mediarepo-socket/src/types/mod.rs | 2 - .../mediarepo-socket/src/types/requests.rs | 21 ------ .../mediarepo-socket/src/types/responses.rs | 75 ------------------- .../mediarepo-socket/src/utils.rs | 2 +- mediarepo-daemon/src/lib.rs | 1 - 13 files changed, 112 insertions(+), 150 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-socket/src/from_model.rs delete mode 100644 mediarepo-daemon/mediarepo-socket/src/types/mod.rs delete mode 100644 mediarepo-daemon/mediarepo-socket/src/types/requests.rs delete mode 100644 mediarepo-daemon/mediarepo-socket/src/types/responses.rs delete mode 100644 mediarepo-daemon/src/lib.rs diff --git a/mediarepo-daemon/Cargo.lock b/mediarepo-daemon/Cargo.lock index 140964b..563e0f3 100644 --- a/mediarepo-daemon/Cargo.lock +++ b/mediarepo-daemon/Cargo.lock @@ -838,6 +838,17 @@ dependencies = [ "tracing-subscriber 0.2.25", ] +[[package]] +name = "mediarepo-api" +version = "0.1.0" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=af90986e88cc4ef7d797ecc9cfd0c306b2d4c7cd#af90986e88cc4ef7d797ecc9cfd0c306b2d4c7cd" +dependencies = [ + "chrono", + "serde", + "thiserror", + "tracing", +] + [[package]] name = "mediarepo-core" version = "0.1.0" @@ -888,6 +899,7 @@ name = "mediarepo-socket" version = "0.1.0" dependencies = [ "chrono", + "mediarepo-api", "mediarepo-core", "mediarepo-model", "serde", diff --git a/mediarepo-daemon/Cargo.toml b/mediarepo-daemon/Cargo.toml index 31daa2d..3f4848e 100644 --- a/mediarepo-daemon/Cargo.toml +++ b/mediarepo-daemon/Cargo.toml @@ -12,18 +12,14 @@ repository = "https://github.com/Trivernis/mediarepo-daemon" name = "mediarepo" path = "src/main.rs" -[lib] -name = "mediarepo" -crate-type = ["lib"] - [dependencies] tracing = "0.1.29" -toml = {version = "0.5.8", optional=true} -structopt = {version="0.3.23", optional=true} -glob = {version="0.3.0", optional=true} -log = {version="0.4.14", optional=true} -tracing-flame = {version = "0.1.0", optional=true} -tracing-appender = {version="0.2.0", optional=true} +toml = "0.5.8" +structopt ="0.3.23" +glob = "0.3.0" +log = "0.4.14" +tracing-flame = "0.1.0" +tracing-appender = "0.2.0" [dependencies.mediarepo-core] @@ -31,11 +27,9 @@ path = "./mediarepo-core" [dependencies.mediarepo-model] path = "./mediarepo-model" -optional=true [dependencies.mediarepo-socket] path = "./mediarepo-socket" -optional=true [dependencies.tokio] version = "1.12.0" @@ -43,21 +37,4 @@ features = ["macros", "rt-multi-thread", "io-std", "io-util"] [dependencies.tracing-subscriber] version="0.2.3" -optional=true -features = ["env-filter"] - - -[features] -default = ["runtime"] -runtime = [ - "toml", - "structopt", - "mediarepo-model", - "mediarepo-socket", - "tracing-subscriber", - "log", - "glob", - "tracing-flame", - "tracing-appender" -] -library = ["mediarepo-socket"] \ No newline at end of file +features = ["env-filter"] \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.lock b/mediarepo-daemon/mediarepo-socket/Cargo.lock index f5d1e75..7f4acd6 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.lock +++ b/mediarepo-daemon/mediarepo-socket/Cargo.lock @@ -761,6 +761,17 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" +[[package]] +name = "mediarepo-api" +version = "0.1.0" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=af90986e88cc4ef7d797ecc9cfd0c306b2d4c7cd#af90986e88cc4ef7d797ecc9cfd0c306b2d4c7cd" +dependencies = [ + "chrono", + "serde", + "thiserror", + "tracing", +] + [[package]] name = "mediarepo-core" version = "0.1.0" @@ -811,6 +822,7 @@ name = "mediarepo-socket" version = "0.1.0" dependencies = [ "chrono", + "mediarepo-api", "mediarepo-core", "mediarepo-model", "serde", diff --git a/mediarepo-daemon/mediarepo-socket/Cargo.toml b/mediarepo-daemon/mediarepo-socket/Cargo.toml index 20ba9f6..d1f1cb5 100644 --- a/mediarepo-daemon/mediarepo-socket/Cargo.toml +++ b/mediarepo-daemon/mediarepo-socket/Cargo.toml @@ -25,4 +25,8 @@ features = ["serde"] [dependencies.tracing-futures] version = "0.2.5" -features = ["tokio-executor"] \ No newline at end of file +features = ["tokio-executor"] + +[dependencies.mediarepo-api] +git = "https://github.com/Trivernis/mediarepo-api.git" +rev = "af90986e88cc4ef7d797ecc9cfd0c306b2d4c7cd" \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-socket/src/from_model.rs b/mediarepo-daemon/mediarepo-socket/src/from_model.rs new file mode 100644 index 0000000..f89a2ff --- /dev/null +++ b/mediarepo-daemon/mediarepo-socket/src/from_model.rs @@ -0,0 +1,47 @@ +use mediarepo_api::types::files::{FileMetadataResponse, ThumbnailMetadataResponse}; +use mediarepo_api::types::tags::TagResponse; +use mediarepo_model::file::File; +use mediarepo_model::tag::Tag; +use mediarepo_model::thumbnail::Thumbnail; + +pub trait FromModel { + fn from_model(model: M) -> Self; +} + +impl FromModel for FileMetadataResponse { + fn from_model(file: File) -> Self { + Self { + id: file.id(), + name: file.name().to_owned(), + comment: file.comment().to_owned(), + hash: file.hash().to_owned(), + file_type: file.file_type() as u32, + mime_type: file.mime_type().to_owned(), + creation_time: file.creation_time().to_owned(), + change_time: file.change_time().to_owned(), + import_time: file.import_time().to_owned(), + } + } +} + +impl FromModel for TagResponse { + fn from_model(model: Tag) -> Self { + Self { + id: model.id(), + namespace: model.namespace().map(|n| n.name().to_owned()), + name: model.name().to_owned(), + } + } +} + +impl FromModel for ThumbnailMetadataResponse { + fn from_model(model: Thumbnail) -> Self { + Self { + id: model.id(), + hash: model.hash().to_owned(), + height: model.height(), + width: model.width(), + mime_type: model.mime_type().to_owned(), + } + } +} diff --git a/mediarepo-daemon/mediarepo-socket/src/lib.rs b/mediarepo-daemon/mediarepo-socket/src/lib.rs index 8e2e902..3eb6b8a 100644 --- a/mediarepo-daemon/mediarepo-socket/src/lib.rs +++ b/mediarepo-daemon/mediarepo-socket/src/lib.rs @@ -1,8 +1,8 @@ -use crate::types::responses::InfoResponse; +use mediarepo_api::types::misc::InfoResponse; use mediarepo_core::rmp_ipc::prelude::*; +mod from_model; mod namespaces; -pub mod types; mod utils; pub fn get_builder(address: &str) -> IPCBuilder { diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs index 92de96d..2b0cd44 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs @@ -1,8 +1,9 @@ -use crate::types::requests::{ - AddFileRequest, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest, -}; -use crate::types::responses::{FileResponse, ThumbnailResponse}; +use crate::from_model::FromModel; use crate::utils::{file_by_identifier, get_repo_from_context}; +use mediarepo_api::types::files::{ + AddFileRequest, FileMetadataResponse, FindFilesByTagsRequest, GetFileThumbnailsRequest, + ReadFileRequest, ThumbnailMetadataResponse, +}; use mediarepo_core::error::RepoError; use mediarepo_core::rmp_ipc::prelude::*; use std::path::PathBuf; @@ -33,7 +34,10 @@ impl FilesNamespace { async fn all_files(ctx: &Context, event: Event) -> IPCResult<()> { let repo = get_repo_from_context(ctx).await; let files = repo.files().await?; - let responses: Vec = files.into_iter().map(FileResponse::from).collect(); + let responses: Vec = files + .into_iter() + .map(FileMetadataResponse::from_model) + .collect(); ctx.emitter .emit_response_to(event.id(), Self::name(), "all_files", responses) @@ -47,8 +51,12 @@ impl FilesNamespace { async fn find_files(ctx: &Context, event: Event) -> IPCResult<()> { let tags = event.data::()?; let repo = get_repo_from_context(ctx).await; - let files = repo.find_files_by_tags(tags.tags).await?; - let responses: Vec = files.into_iter().map(FileResponse::from).collect(); + let tags = tags.tags.into_iter().map(|t| t.name).collect(); + let files = repo.find_files_by_tags(tags).await?; + let responses: Vec = files + .into_iter() + .map(FileMetadataResponse::from_model) + .collect(); ctx.emitter .emit_response_to(event.id(), Self::name(), "find_files", responses) .await?; @@ -68,7 +76,7 @@ impl FilesNamespace { event.id(), Self::name(), "add_file", - FileResponse::from(file), + FileMetadataResponse::from_model(file), ) .await?; @@ -81,7 +89,7 @@ impl FilesNamespace { let request = event.data::()?; let repo = get_repo_from_context(ctx).await; - let file = file_by_identifier(request, &repo).await?; + let file = file_by_identifier(request.id, &repo).await?; let mut reader = file.get_reader().await?; let mut buf = Vec::new(); reader.read_to_end(&mut buf).await?; @@ -98,12 +106,12 @@ impl FilesNamespace { async fn thumbnails(ctx: &Context, event: Event) -> IPCResult<()> { let request = event.data::()?; let repo = get_repo_from_context(ctx).await; - let file = file_by_identifier(request, &repo).await?; + let file = file_by_identifier(request.id, &repo).await?; let thumbnails = file.thumbnails().await?; - let thumb_responses: Vec = thumbnails + let thumb_responses: Vec = thumbnails .into_iter() - .map(ThumbnailResponse::from) + .map(ThumbnailMetadataResponse::from_model) .collect(); ctx.emitter .emit_response_to(event.id(), Self::name(), "get_thumbnails", thumb_responses) diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs index 248f794..a83c9f7 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs @@ -1,6 +1,7 @@ -use crate::types::requests::GetFileTagsRequest; -use crate::types::responses::TagResponse; +use crate::from_model::FromModel; use crate::utils::{file_by_identifier, get_repo_from_context}; +use mediarepo_api::types::files::GetFileTagsRequest; +use mediarepo_api::types::tags::TagResponse; use mediarepo_core::rmp_ipc::prelude::*; pub struct TagsNamespace; @@ -27,7 +28,7 @@ impl TagsNamespace { .tags() .await? .into_iter() - .map(TagResponse::from) + .map(TagResponse::from_model) .collect(); ctx.emitter .emit_response_to(event.id(), Self::name(), "all_tags", tags) @@ -41,9 +42,9 @@ impl TagsNamespace { async fn tags_for_file(ctx: &Context, event: Event) -> IPCResult<()> { let repo = get_repo_from_context(ctx).await; let request = event.data::()?; - let file = file_by_identifier(request, &repo).await?; + let file = file_by_identifier(request.id, &repo).await?; let tags = file.tags().await?; - let responses: Vec = tags.into_iter().map(TagResponse::from).collect(); + let responses: Vec = tags.into_iter().map(TagResponse::from_model).collect(); ctx.emitter .emit_response_to(event.id(), Self::name(), "tags_for_file", responses) diff --git a/mediarepo-daemon/mediarepo-socket/src/types/mod.rs b/mediarepo-daemon/mediarepo-socket/src/types/mod.rs deleted file mode 100644 index 116da0f..0000000 --- a/mediarepo-daemon/mediarepo-socket/src/types/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index 0e4a7ca..0000000 --- a/mediarepo-daemon/mediarepo-socket/src/types/requests.rs +++ /dev/null @@ -1,21 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize)] -pub struct AddFileRequest { - pub path: String, -} - -#[derive(Serialize, Deserialize)] -pub enum FileIdentifier { - ID(i64), - Hash(String), -} - -pub type ReadFileRequest = FileIdentifier; -pub type GetFileThumbnailsRequest = FileIdentifier; -pub type GetFileTagsRequest = FileIdentifier; - -#[derive(Serialize, Deserialize)] -pub struct FindFilesByTagsRequest { - pub tags: Vec, -} diff --git a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs b/mediarepo-daemon/mediarepo-socket/src/types/responses.rs deleted file mode 100644 index d8a4183..0000000 --- a/mediarepo-daemon/mediarepo-socket/src/types/responses.rs +++ /dev/null @@ -1,75 +0,0 @@ -use chrono::NaiveDateTime; -use mediarepo_model::file::File; -use mediarepo_model::file_type::FileType; -use mediarepo_model::tag::Tag; -use mediarepo_model::thumbnail::Thumbnail; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct FileResponse { - pub name: Option, - pub comment: Option, - pub hash: String, - pub file_type: FileType, - pub mime_type: Option, - pub creation_time: NaiveDateTime, - pub change_time: NaiveDateTime, - pub import_time: NaiveDateTime, -} - -impl From for FileResponse { - fn from(file: File) -> Self { - FileResponse { - hash: file.hash().to_owned(), - file_type: file.file_type(), - mime_type: file.mime_type().clone(), - name: file.name().to_owned(), - creation_time: file.creation_time().to_owned(), - change_time: file.change_time().to_owned(), - import_time: file.import_time().to_owned(), - comment: file.comment().to_owned(), - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ThumbnailResponse { - hash: String, - height: i32, - width: i32, - mime: Option, -} - -impl From for ThumbnailResponse { - fn from(thumb: Thumbnail) -> Self { - Self { - hash: thumb.hash().to_owned(), - height: thumb.height(), - width: thumb.width(), - mime: thumb.mime_type().to_owned(), - } - } -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct InfoResponse { - pub name: String, - pub version: String, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct TagResponse { - pub id: i64, - pub name: String, - pub namespace: Option, -} - -impl From for TagResponse { - fn from(tag: Tag) -> Self { - Self { - id: tag.id(), - name: tag.name().to_owned(), - namespace: tag.namespace().map(|n| n.name().to_owned()), - } - } -} diff --git a/mediarepo-daemon/mediarepo-socket/src/utils.rs b/mediarepo-daemon/mediarepo-socket/src/utils.rs index f65b62b..6038554 100644 --- a/mediarepo-daemon/mediarepo-socket/src/utils.rs +++ b/mediarepo-daemon/mediarepo-socket/src/utils.rs @@ -1,4 +1,4 @@ -use crate::types::requests::FileIdentifier; +use mediarepo_api::types::identifier::FileIdentifier; use mediarepo_core::error::{RepoError, RepoResult}; use mediarepo_core::rmp_ipc::ipc::context::Context; use mediarepo_model::file::File; diff --git a/mediarepo-daemon/src/lib.rs b/mediarepo-daemon/src/lib.rs deleted file mode 100644 index 0af28f4..0000000 --- a/mediarepo-daemon/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub use mediarepo_socket::types::*;