From e8f8a4b0070c2d29d9572eee82b4a434a974b8a1 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Fri, 29 Apr 2022 21:33:44 +0200 Subject: [PATCH] server (/fs/list): return filesize --- Cargo.lock | 16 ++++++++++++++++ server/Cargo.toml | 1 + server/src/fs/list.rs | 9 +++++++-- server/src/lib.rs | 2 +- types/src/errors/mod.rs | 2 +- types/src/fs/list.rs | 8 +++++++- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ce1e1d..83cd0b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,6 +218,15 @@ version = "3.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" +[[package]] +name = "byte-unit" +version = "4.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ebf10dda65f19ff0f42ea15572a359ed60d7fc74fdc984d90310937be0014b" +dependencies = [ + "utf8-width", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -623,6 +632,7 @@ dependencies = [ "axum", "axum-auth", "base64", + "byte-unit", "homedisk-database", "homedisk-types", "hyper", @@ -1828,6 +1838,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" + [[package]] name = "uuid" version = "1.0.0" diff --git a/server/Cargo.toml b/server/Cargo.toml index 006b390..f364ecb 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -16,3 +16,4 @@ homedisk-types = { path = "../types", features = ["axum"] } axum-auth = "0.2.0" jsonwebtoken = "8.1.0" base64 = "0.13.0" +byte-unit = "4.0.14" diff --git a/server/src/fs/list.rs b/server/src/fs/list.rs index dc7b474..a1baab6 100644 --- a/server/src/fs/list.rs +++ b/server/src/fs/list.rs @@ -2,11 +2,12 @@ use std::fs; use axum::{extract::rejection::JsonRejection, Extension, Json}; use axum_auth::AuthBearer; +use byte_unit::Byte; use homedisk_database::{Database, Error}; use homedisk_types::{ config::types::Config, errors::{AuthError, FsError, ServerError}, - fs::list::{Request, Response}, + fs::list::{FileInfo, Request, Response}, }; use crate::middleware::{validate_json, validate_jwt}; @@ -43,11 +44,15 @@ pub async fn handle( .map_err(|err| ServerError::FsError(FsError::UnknowError(err.to_string())))?; let name = path.path().display().to_string().replace(&user_path, ""); + let filesize = Byte::from_bytes(metadata.len().into()).get_appropriate_unit(true); if metadata.is_dir() { dirs.push(name) } else { - files.push(name) + files.push(FileInfo { + name, + size: filesize.to_string(), + }) } } diff --git a/server/src/lib.rs b/server/src/lib.rs index e74f090..fb70226 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -8,7 +8,7 @@ use axum::{http::HeaderValue, routing::get, Extension, Router, Server}; use homedisk_database::Database; use homedisk_types::config::types::Config; use log::{debug, info}; -use tower_http::cors::{CorsLayer, AllowOrigin}; +use tower_http::cors::{AllowOrigin, CorsLayer}; async fn health_check() -> &'static str { "I'm alive!" diff --git a/types/src/errors/mod.rs b/types/src/errors/mod.rs index fc92624..b9692a6 100644 --- a/types/src/errors/mod.rs +++ b/types/src/errors/mod.rs @@ -7,7 +7,7 @@ pub use fs::Error as FsError; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)] -#[serde(tag = "error", content = "error-message", rename_all = "kebab-case")] +#[serde(tag = "error", content = "error_message", rename_all = "kebab-case")] pub enum ServerError { #[error("auth error: {0}")] AuthError(#[from] AuthError), diff --git a/types/src/fs/list.rs b/types/src/fs/list.rs index 6b69503..37728aa 100644 --- a/types/src/fs/list.rs +++ b/types/src/fs/list.rs @@ -7,6 +7,12 @@ pub struct Request { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Response { - pub files: Vec, + pub files: Vec, pub dirs: Vec, } + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct FileInfo { + pub name: String, + pub size: String, +}