From d75af48844d36a26badd6cef2e07c9eca1b45ae8 Mon Sep 17 00:00:00 2001 From: Kirk Strauser Date: Fri, 14 Aug 2020 17:03:17 -0700 Subject: [PATCH] Sort directory listings, grouped by dirs then files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/main.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 46843bd..a32ebf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,8 +75,9 @@ async fn get_content(path: PathBuf, u: url::Url) -> Result { return Ok(tokio::fs::read_to_string(path).await?); } - let mut list = String::from("# Directory Listing\r\n\r\n"); - list.push_str(&format!("Path: {}\r\n\r\n", u.path())); + let mut dirs: Vec = Vec::new(); + let mut files: Vec = Vec::new(); + // needs work for file in fs::read_dir(&path)? { if let Ok(file) = file { @@ -96,12 +97,26 @@ async fn get_content(path: PathBuf, u: url::Url) -> Result { _ => continue, }; if m.is_dir() { - list.push_str(&format!("=> {}/ {}/\r\n", ep, p.display())); + dirs.push(format!("=> {}/ {}/\r\n", ep, p.display())); } 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); }