mirror of
https://github.com/MedzikUser/HomeDisk.git
synced 2024-08-14 21:46:53 +00:00
server (/fs/list): add directory size
This commit is contained in:
parent
44e746b068
commit
75854e5965
2 changed files with 31 additions and 3 deletions
|
@ -1,10 +1,12 @@
|
|||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, io};
|
||||
|
||||
use crate::fs::validate_path;
|
||||
use axum::{extract::rejection::JsonRejection, Extension, Json};
|
||||
use axum_auth::AuthBearer;
|
||||
use byte_unit::Byte;
|
||||
use homedisk_database::Database;
|
||||
use homedisk_types::fs::list::DirInfo;
|
||||
use homedisk_types::{
|
||||
config::types::Config,
|
||||
errors::{FsError, ServerError},
|
||||
|
@ -13,6 +15,21 @@ use homedisk_types::{
|
|||
|
||||
use crate::middleware::{find_user, validate_json, validate_jwt};
|
||||
|
||||
fn dir_size(path: impl Into<PathBuf>) -> io::Result<u64> {
|
||||
fn dir_size(mut dir: fs::ReadDir) -> io::Result<u64> {
|
||||
dir.try_fold(0, |acc, file| {
|
||||
let file = file?;
|
||||
let size = match file.metadata()? {
|
||||
data if data.is_dir() => dir_size(fs::read_dir(file.path())?)?,
|
||||
data => data.len(),
|
||||
};
|
||||
Ok(acc + size)
|
||||
})
|
||||
}
|
||||
|
||||
dir_size(fs::read_dir(path.into())?)
|
||||
}
|
||||
|
||||
pub async fn handle(
|
||||
Extension(db): Extension<Database>,
|
||||
Extension(config): Extension<Config>,
|
||||
|
@ -51,7 +68,12 @@ pub async fn handle(
|
|||
let file_size = Byte::from_bytes(metadata.len().into()).get_appropriate_unit(true);
|
||||
|
||||
if metadata.is_dir() {
|
||||
dirs.push(name)
|
||||
dirs.push(DirInfo {
|
||||
name,
|
||||
size: Byte::from_bytes(dir_size(f.path().display().to_string()).unwrap() as u128)
|
||||
.get_appropriate_unit(true)
|
||||
.to_string(),
|
||||
})
|
||||
} else {
|
||||
files.push(FileInfo {
|
||||
name,
|
||||
|
|
|
@ -8,7 +8,7 @@ pub struct Request {
|
|||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Response {
|
||||
pub files: Vec<FileInfo>,
|
||||
pub dirs: Vec<String>,
|
||||
pub dirs: Vec<DirInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
|
@ -16,3 +16,9 @@ pub struct FileInfo {
|
|||
pub name: String,
|
||||
pub size: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct DirInfo {
|
||||
pub name: String,
|
||||
pub size: String,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue