use crate::{ library::LibraryConfig, prisma::statistics, volume::{get_volumes, save_volume}, }; use super::{utils::LibraryRequest, RouterBuilder}; use chrono::Utc; use fs_extra::dir::get_size; // TODO: Remove this dependency as it is sync instead of async use rspc::Type; use sd_crypto::{ crypto::stream::Algorithm, keys::hashing::HashingAlgorithm, primitives::types::OnboardingConfig, Protected, }; use serde::Deserialize; use tokio::fs; use uuid::Uuid; pub(crate) fn mount() -> RouterBuilder { ::new() .query("list", |t| { t(|ctx, _: ()| async move { ctx.library_manager.get_all_libraries_config().await }) }) .library_query("getStatistics", |t| { t(|_, _: (), library| async move { let _statistics = library .db .statistics() .find_unique(statistics::id::equals(library.node_local_id)) .exec() .await?; // TODO: get from database, not sys let volumes = get_volumes(); save_volume(&library).await?; let mut available_capacity: u64 = 0; let mut total_capacity: u64 = 0; if volumes.is_ok() { for volume in volumes? { total_capacity += volume.total_capacity; available_capacity += volume.available_capacity; } } let library_db_size = match fs::metadata(library.config().data_directory()).await { Ok(metadata) => metadata.len(), Err(_) => 0, }; let thumbnail_folder_size = get_size(library.config().data_directory().join("thumbnails")); use statistics::*; let params = vec![ id::set(1), // Each library is a database so only one of these ever exists date_captured::set(Utc::now().into()), total_object_count::set(0), library_db_size::set(library_db_size.to_string()), total_bytes_used::set(0.to_string()), total_bytes_capacity::set(total_capacity.to_string()), total_unique_bytes::set(0.to_string()), total_bytes_free::set(available_capacity.to_string()), preview_media_bytes::set(thumbnail_folder_size.unwrap_or(0).to_string()), ]; Ok(library .db .statistics() .upsert( statistics::id::equals(1), // Each library is a database so only one of these ever exists params.clone(), params, ) .exec() .await?) }) }) .mutation("create", |t| { #[derive(Deserialize, Type)] pub struct CreateLibraryArgs { name: String, password: Protected, algorithm: Algorithm, hashing_algorithm: HashingAlgorithm, } t(|ctx, args: CreateLibraryArgs| async move { Ok(ctx .library_manager .create( LibraryConfig { name: args.name.to_string(), ..Default::default() }, OnboardingConfig { password: args.password, algorithm: args.algorithm, hashing_algorithm: args.hashing_algorithm, }, ) .await?) }) }) .mutation("edit", |t| { #[derive(Type, Deserialize)] pub struct EditLibraryArgs { pub id: Uuid, pub name: Option, pub description: Option, } t(|ctx, args: EditLibraryArgs| async move { Ok(ctx .library_manager .edit(args.id, args.name, args.description) .await?) }) }) .mutation("delete", |t| { t(|ctx, id: Uuid| async move { Ok(ctx.library_manager.delete_library(id).await?) }) }) }