From bb5a5564b4bf98d0a8422829599d1e5ffa353bcb Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 25 Sep 2024 22:19:25 +0800 Subject: [PATCH] feat: webui displays subdirectory items (#457) --- assets/index.js | 2 +- src/server.rs | 35 +++++++++++++++-------------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/assets/index.js b/assets/index.js index 91d2cba..8d8d470 100644 --- a/assets/index.js +++ b/assets/index.js @@ -478,7 +478,7 @@ function addPath(file, index) { ${actionEdit} `; - let sizeDisplay = isDir ? "" : formatSize(file.size).join(" "); + let sizeDisplay = isDir ? `${file.size} ${file.size === 1 ? "item" : "items"}` : formatSize(file.size).join(" "); $pathsTableBody.insertAdjacentHTML("beforeend", ` diff --git a/src/server.rs b/src/server.rs index 4641fbe..3aa172d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1363,8 +1363,15 @@ impl Server { }; let mtime = to_timestamp(&meta.modified()?); let size = match path_type { - PathType::Dir | PathType::SymlinkDir => None, - PathType::File | PathType::SymlinkFile => Some(meta.len()), + PathType::Dir | PathType::SymlinkDir => { + let mut count = 0; + let mut entries = tokio::fs::read_dir(&path).await?; + while entries.next_entry().await?.is_some() { + count += 1; + } + count + } + PathType::File | PathType::SymlinkFile => meta.len(), }; let rel_path = path.strip_prefix(base_path)?; let name = normalize_path(rel_path); @@ -1416,7 +1423,7 @@ struct PathItem { path_type: PathType, name: String, mtime: u64, - size: Option, + size: u64, } impl PathItem { @@ -1450,21 +1457,18 @@ impl PathItem { ), PathType::File | PathType::SymlinkFile => format!( r#" -{} +{href} -{} +{displayname} {} -{} +{mtime} HTTP/1.1 200 OK "#, - href, - displayname, - self.size.unwrap_or_default(), - mtime + self.size ), } } @@ -1491,16 +1495,7 @@ impl PathItem { pub fn sort_by_size(&self, other: &Self) -> Ordering { match self.path_type.cmp(&other.path_type) { - Ordering::Equal => { - if self.is_dir() { - alphanumeric_sort::compare_str( - self.name.to_lowercase(), - other.name.to_lowercase(), - ) - } else { - self.size.unwrap_or(0).cmp(&other.size.unwrap_or(0)) - } - } + Ordering::Equal => self.size.cmp(&other.size), v => v, } }