feat: webui displays subdirectory items (#457)

This commit is contained in:
sigoden 2024-09-25 22:19:25 +08:00 committed by GitHub
parent 2cf6d39032
commit bb5a5564b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 21 deletions

View file

@ -478,7 +478,7 @@ function addPath(file, index) {
${actionEdit} ${actionEdit}
</td>`; </td>`;
let sizeDisplay = isDir ? "" : formatSize(file.size).join(" "); let sizeDisplay = isDir ? `${file.size} ${file.size === 1 ? "item" : "items"}` : formatSize(file.size).join(" ");
$pathsTableBody.insertAdjacentHTML("beforeend", ` $pathsTableBody.insertAdjacentHTML("beforeend", `
<tr id="addPath${index}"> <tr id="addPath${index}">

View file

@ -1363,8 +1363,15 @@ impl Server {
}; };
let mtime = to_timestamp(&meta.modified()?); let mtime = to_timestamp(&meta.modified()?);
let size = match path_type { let size = match path_type {
PathType::Dir | PathType::SymlinkDir => None, PathType::Dir | PathType::SymlinkDir => {
PathType::File | PathType::SymlinkFile => Some(meta.len()), 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 rel_path = path.strip_prefix(base_path)?;
let name = normalize_path(rel_path); let name = normalize_path(rel_path);
@ -1416,7 +1423,7 @@ struct PathItem {
path_type: PathType, path_type: PathType,
name: String, name: String,
mtime: u64, mtime: u64,
size: Option<u64>, size: u64,
} }
impl PathItem { impl PathItem {
@ -1450,21 +1457,18 @@ impl PathItem {
), ),
PathType::File | PathType::SymlinkFile => format!( PathType::File | PathType::SymlinkFile => format!(
r#"<D:response> r#"<D:response>
<D:href>{}</D:href> <D:href>{href}</D:href>
<D:propstat> <D:propstat>
<D:prop> <D:prop>
<D:displayname>{}</D:displayname> <D:displayname>{displayname}</D:displayname>
<D:getcontentlength>{}</D:getcontentlength> <D:getcontentlength>{}</D:getcontentlength>
<D:getlastmodified>{}</D:getlastmodified> <D:getlastmodified>{mtime}</D:getlastmodified>
<D:resourcetype></D:resourcetype> <D:resourcetype></D:resourcetype>
</D:prop> </D:prop>
<D:status>HTTP/1.1 200 OK</D:status> <D:status>HTTP/1.1 200 OK</D:status>
</D:propstat> </D:propstat>
</D:response>"#, </D:response>"#,
href, self.size
displayname,
self.size.unwrap_or_default(),
mtime
), ),
} }
} }
@ -1491,16 +1495,7 @@ impl PathItem {
pub fn sort_by_size(&self, other: &Self) -> Ordering { pub fn sort_by_size(&self, other: &Self) -> Ordering {
match self.path_type.cmp(&other.path_type) { match self.path_type.cmp(&other.path_type) {
Ordering::Equal => { Ordering::Equal => self.size.cmp(&other.size),
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))
}
}
v => v, v => v,
} }
} }