mirror of
https://github.com/MedzikUser/HomeDisk.git
synced 2024-08-14 21:46:53 +00:00
feat(server): add size and modification time in /api/fs/list
Added directory size and directory modification date.
This commit is contained in:
parent
75f523f116
commit
e614b64bba
1 changed files with 33 additions and 16 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::{fs, time::SystemTime};
|
use std::{fs, io, path::PathBuf, time::SystemTime};
|
||||||
|
|
||||||
use axum::{Extension, Json};
|
use axum::{Extension, Json};
|
||||||
use byte_unit::Byte;
|
use byte_unit::Byte;
|
||||||
|
@ -15,6 +15,22 @@ use crate::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Get directory size on disk (size of all files in directory).
|
||||||
|
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 list(
|
pub async fn list(
|
||||||
Extension(config): Extension<Config>,
|
Extension(config): Extension<Config>,
|
||||||
path: PathQuery,
|
path: PathQuery,
|
||||||
|
@ -26,28 +42,18 @@ pub async fn list(
|
||||||
|
|
||||||
let path = format!("{}/{}", user.user_dir(&config.storage.path), path);
|
let path = format!("{}/{}", user.user_dir(&config.storage.path), path);
|
||||||
|
|
||||||
let paths = fs::read_dir(&path).map_err(|_| Error::FailedReadDirectory)?;
|
let mut paths = fs::read_dir(&path).map_err(|_| Error::FailedReadDirectory)?;
|
||||||
|
|
||||||
for dir_entry in paths {
|
while let Some(Ok(entry)) = paths.next() {
|
||||||
let dir_entry = dir_entry.unwrap();
|
let metadata = entry.metadata().unwrap();
|
||||||
|
|
||||||
let metadata = dir_entry.metadata().unwrap();
|
let name = entry
|
||||||
|
|
||||||
let name = dir_entry
|
|
||||||
.path()
|
.path()
|
||||||
.display()
|
.display()
|
||||||
.to_string()
|
.to_string()
|
||||||
.replace(&format!("{path}/"), "")
|
.replace(&format!("{path}/"), "")
|
||||||
.replace(&path, "");
|
.replace(&path, "");
|
||||||
|
|
||||||
if metadata.is_dir() {
|
|
||||||
// TODO: add size and modification date
|
|
||||||
response.dirs.push(Entry::new(name, 0.to_string(), 0))
|
|
||||||
} else {
|
|
||||||
let size = Byte::from_bytes(metadata.len().into())
|
|
||||||
.get_appropriate_unit(true)
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let modified = metadata
|
let modified = metadata
|
||||||
.modified()
|
.modified()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -55,6 +61,17 @@ pub async fn list(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_secs();
|
.as_secs();
|
||||||
|
|
||||||
|
if metadata.is_dir() {
|
||||||
|
let size = Byte::from_bytes(dir_size(entry.path()).unwrap().into())
|
||||||
|
.get_appropriate_unit(true)
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
response.dirs.push(Entry::new(name, size, modified))
|
||||||
|
} else {
|
||||||
|
let size = Byte::from_bytes(metadata.len().into())
|
||||||
|
.get_appropriate_unit(true)
|
||||||
|
.to_string();
|
||||||
|
|
||||||
response.files.push(Entry::new(name, size, modified));
|
response.files.push(Entry::new(name, size, modified));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue