Skip to content

Commit

Permalink
feat: webui displays subdirectory items (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Sep 25, 2024
1 parent 2cf6d39 commit bb5a556
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function addPath(file, index) {
${actionEdit}
</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", `
<tr id="addPath${index}">
Expand Down
35 changes: 15 additions & 20 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1416,7 +1423,7 @@ struct PathItem {
path_type: PathType,
name: String,
mtime: u64,
size: Option<u64>,
size: u64,
}

impl PathItem {
Expand Down Expand Up @@ -1450,21 +1457,18 @@ impl PathItem {
),
PathType::File | PathType::SymlinkFile => format!(
r#"<D:response>
<D:href>{}</D:href>
<D:href>{href}</D:href>
<D:propstat>
<D:prop>
<D:displayname>{}</D:displayname>
<D:displayname>{displayname}</D:displayname>
<D:getcontentlength>{}</D:getcontentlength>
<D:getlastmodified>{}</D:getlastmodified>
<D:getlastmodified>{mtime}</D:getlastmodified>
<D:resourcetype></D:resourcetype>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>"#,
href,
displayname,
self.size.unwrap_or_default(),
mtime
self.size
),
}
}
Expand All @@ -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,
}
}
Expand Down

0 comments on commit bb5a556

Please sign in to comment.