From 8d108c699e03bd8d23ba180096e8c503503cc483 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 28 Dec 2021 13:39:12 +0100 Subject: [PATCH] Implement changes to size fetching api Signed-off-by: Trivernis --- mediarepo-daemon/Cargo.lock | 4 +- mediarepo-daemon/mediarepo-core/Cargo.toml | 2 +- .../mediarepo-socket/src/namespaces/repo.rs | 54 ++++++++++++------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/mediarepo-daemon/Cargo.lock b/mediarepo-daemon/Cargo.lock index 946839c..4493864 100644 --- a/mediarepo-daemon/Cargo.lock +++ b/mediarepo-daemon/Cargo.lock @@ -1136,8 +1136,8 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "mediarepo-api" -version = "0.18.0" -source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=cb4510c57fb25bd39f2e994e928359599e7cbfaa#cb4510c57fb25bd39f2e994e928359599e7cbfaa" +version = "0.19.0" +source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=35f1e1f39f477f4ecaab454b01d787349582ee12#35f1e1f39f477f4ecaab454b01d787349582ee12" dependencies = [ "bromine", "chrono", diff --git a/mediarepo-daemon/mediarepo-core/Cargo.toml b/mediarepo-daemon/mediarepo-core/Cargo.toml index 4940757..8f1960a 100644 --- a/mediarepo-daemon/mediarepo-core/Cargo.toml +++ b/mediarepo-daemon/mediarepo-core/Cargo.toml @@ -38,7 +38,7 @@ features = ["fs", "io-util", "io-std"] [dependencies.mediarepo-api] git = "https://github.com/Trivernis/mediarepo-api.git" -rev = "cb4510c57fb25bd39f2e994e928359599e7cbfaa" +rev = "35f1e1f39f477f4ecaab454b01d787349582ee12" features = ["bromine"] [features] diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/repo.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/repo.rs index ce7a300..8ca4fef 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/repo.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/repo.rs @@ -3,7 +3,9 @@ use std::path::PathBuf; use tokio::fs; use mediarepo_core::bromine::prelude::*; -use mediarepo_core::mediarepo_api::types::repo::{FrontendState, RepositoryMetadata}; +use mediarepo_core::mediarepo_api::types::repo::{ + FrontendState, RepositoryMetadata, SizeMetadata, SizeType, +}; use mediarepo_core::type_keys::{RepoPathKey, SettingsKey}; use mediarepo_core::utils::get_folder_size; @@ -19,6 +21,7 @@ impl NamespaceProvider for RepoNamespace { fn register(handler: &mut EventHandler) { events!(handler, "repository_metadata" => Self::get_metadata, + "size_metadata" => Self::get_size_metadata, "frontend_state" => Self::frontend_state, "set_frontend_state" => Self::set_frontend_state ); @@ -30,20 +33,6 @@ impl RepoNamespace { async fn get_metadata(ctx: &Context, _: Event) -> IPCResult<()> { let repo = get_repo_from_context(ctx).await; let counts = repo.get_counts().await?; - let file_size = repo.get_main_store_size().await?; - let thumbnail_size = repo.get_thumb_store_size().await?; - - let (repo_path, settings) = { - let data = ctx.data.read().await; - ( - data.get::().unwrap().clone(), - data.get::().unwrap().clone(), - ) - }; - let db_path = repo_path.join(settings.database_path); - let total_size = get_folder_size(repo_path).await?; - let database_metadata = fs::metadata(db_path).await?; - let database_size = database_metadata.len(); let metadata = RepositoryMetadata { version: env!("CARGO_PKG_VERSION").to_string(), @@ -52,20 +41,45 @@ impl RepoNamespace { namespace_count: counts.namespace_count as u64, mapping_count: counts.mapping_count as u64, hash_count: counts.hash_count as u64, - total_size, - file_size, - database_size, - thumbnail_size, }; tracing::debug!("metadata = {:?}", metadata); - ctx.emit_to(Self::name(), "repository_metadata", metadata) .await?; Ok(()) } + #[tracing::instrument(skip_all)] + async fn get_size_metadata(ctx: &Context, event: Event) -> IPCResult<()> { + let size_type = event.payload::()?; + let repo = get_repo_from_context(ctx).await; + let (repo_path, settings) = { + let data = ctx.data.read().await; + ( + data.get::().unwrap().clone(), + data.get::().unwrap().clone(), + ) + }; + let size = match &size_type { + SizeType::Total => get_folder_size(repo_path).await?, + SizeType::FileFolder => repo.get_main_store_size().await?, + SizeType::ThumbFolder => repo.get_thumb_store_size().await?, + SizeType::DatabaseFile => { + let db_path = repo_path.join(settings.database_path); + + let database_metadata = fs::metadata(db_path).await?; + database_metadata.len() + } + }; + let response = SizeMetadata { size, size_type }; + tracing::debug!("size response = {:?}", response); + + ctx.emit_to(Self::name(), "size_metadata", response).await?; + + Ok(()) + } + #[tracing::instrument(skip_all)] async fn frontend_state(ctx: &Context, _: Event) -> IPCResult<()> { let path = get_frontend_state_path(ctx).await?;