sort index
This commit is contained in:
parent
c408a56830
commit
1c5824a6fb
1 changed files with 16 additions and 12 deletions
26
src/main.rs
26
src/main.rs
|
@ -105,18 +105,25 @@ fn get_file(request: Request) -> Option<Content> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_index(relative_path: &str, path: &Path) -> Option<Content> {
|
fn generate_index(relative_path: &str, path: &Path) -> Option<Content> {
|
||||||
let list = path
|
let mut dirs: Vec<_> = path
|
||||||
.read_dir()
|
.read_dir()
|
||||||
.ok()?
|
.ok()?
|
||||||
.flatten()
|
.flatten()
|
||||||
.filter_map(|e| {
|
.filter_map(|d| {
|
||||||
let target = e.file_name().to_str()?.to_string();
|
let is_dir = d.file_type().ok()?.is_dir();
|
||||||
|
d.file_name().to_str().map(|s| (s.to_owned(), is_dir))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
dirs.sort_by(|(name_a, dir_a), (name_b, dir_b)| dir_b.cmp(dir_a).then(name_a.cmp(name_b)));
|
||||||
|
let list = dirs
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(name, is_dir)| {
|
||||||
let mut s = format!(
|
let mut s = format!(
|
||||||
" <li><a href=\"{}\"> {}",
|
" <li><a href=\"{}\">{}",
|
||||||
PathBuf::from(relative_path).join(&target).display(),
|
PathBuf::from(relative_path).join(&name).display(),
|
||||||
target
|
name
|
||||||
);
|
);
|
||||||
if e.file_type().ok()?.is_dir() {
|
if is_dir {
|
||||||
s.push('/');
|
s.push('/');
|
||||||
}
|
}
|
||||||
s.push_str("</a></li>\n");
|
s.push_str("</a></li>\n");
|
||||||
|
@ -128,18 +135,15 @@ fn generate_index(relative_path: &str, path: &Path) -> Option<Content> {
|
||||||
});
|
});
|
||||||
let page = format!(
|
let page = format!(
|
||||||
r#"<!DOCTYPE html>
|
r#"<!DOCTYPE html>
|
||||||
<html lang="en">
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Index of {relative_path}</title>
|
<title>Index of {relative_path}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3>Index of {relative_path}</h3>
|
<h3>Index of {relative_path}</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="..">../</a></li>
|
<li><a href="..">../</a></li>
|
||||||
{list}
|
{list} </ul>
|
||||||
</ul>
|
|
||||||
</body>
|
</body>
|
||||||
</html>"#,
|
</html>"#,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue