~int80h/gemserv unlisted

d75af48844d36a26badd6cef2e07c9eca1b45ae8 — Kirk Strauser 3 months ago b3879e3
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
1 files changed, 19 insertions(+), 4 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +19 -4
@@ 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?);
    }

    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<String> = Vec::new();
    let mut files: Vec<String> = 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<String, io::Error> {
                _ => 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);
}