Sort directory listings, grouped by dirs then files
Heya! Thanks for putting Gemserv out there!
This changes directory listings so that all directories are listed (sorted), then all filenames are listed (sorted). You can see the output of this patch running at gemini://gemini.freeradical.zone/log/ . The downside is that now you’ll temporarily have 2 copies of the directory listing in RAM during the request: one in `list`, and a second copy divided across `dirs` and `files`. It seems unlikely that anyone’s currently using Gemserv to serve directories that just barely fit in RAM, so that seems pretty low risk. 🙂
- Kirk
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -75,8 +75,9 @@ async fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
|||||||
return Ok(tokio::fs::read_to_string(path).await?);
|
return Ok(tokio::fs::read_to_string(path).await?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut list = String::from("# Directory Listing\r\n\r\n");
|
let mut dirs: Vec<String> = Vec::new();
|
||||||
list.push_str(&format!("Path: {}\r\n\r\n", u.path()));
|
let mut files: Vec<String> = Vec::new();
|
||||||
|
|
||||||
// needs work
|
// needs work
|
||||||
for file in fs::read_dir(&path)? {
|
for file in fs::read_dir(&path)? {
|
||||||
if let Ok(file) = file {
|
if let Ok(file) = file {
|
||||||
@@ -96,12 +97,26 @@ async fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
|||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
if m.is_dir() {
|
if m.is_dir() {
|
||||||
list.push_str(&format!("=> {}/ {}/\r\n", ep, p.display()));
|
dirs.push(format!("=> {}/ {}/\r\n", ep, p.display()));
|
||||||
} else {
|
} else {
|
||||||
list.push_str(&format!("=> {} {}\r\n", ep, p.display()));
|
files.push(format!("=> {}/ {}\r\n", ep, p.display()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dirs.sort();
|
||||||
|
files.sort();
|
||||||
|
|
||||||
|
let mut list = String::from("# Directory Listing\r\n\r\n");
|
||||||
|
list.push_str(&format!("Path: {}\r\n\r\n", u.path()));
|
||||||
|
|
||||||
|
for dir in dirs {
|
||||||
|
list.push_str(&dir);
|
||||||
|
}
|
||||||
|
for file in files {
|
||||||
|
list.push_str(&file);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(list);
|
return Ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user