Add directory listing (need fix, because display full path to file)

This commit is contained in:
g1n 2021-07-16 18:33:03 +00:00
parent 419b116f93
commit b486102fe6
2 changed files with 64 additions and 5 deletions

View File

@ -8,3 +8,4 @@ edition = "2018"
[dependencies]
argparse = "0.2.2"
glob = "0.3.0"

View File

@ -1,4 +1,5 @@
use std::fs;
use std::{fs, io};
use std::borrow::Cow;
use std::path::Path;
use std::io::prelude::*;
use std::net::TcpListener;
@ -105,10 +106,54 @@ fn handle_connection(mut stream: TcpStream) {
if exists {
_contents = fs::read_to_string(format!("{}{}index.gmi", "/home/g1n/public_spartan", _path)).unwrap();
} else {
/*
let mut entries = fs::read_dir(full_path);
let mut lines = vec![];
println!("{:?}", entries);
for entry in entries {
let mut name = entry
.file_name()
.into_string()
.or(Err("Non-Unicode filename"));
//entry.to_string().clone();
/*.into_string()
.or(Err("Non-Unicode filename"));*/
if name.starts_with('.') {
continue;
}
if Path::new(&entry).is_dir() {
name += "/";
}
/*let line = match /*percent_encode(*/name.as_bytes().into() {
Cow::Owned(url) => format!("=> {:?} {}\n", url, name),
Cow::Borrowed(url) => format!("=> {:?}\n", url), // url and name are identical
};*/
let line = format!("=> {} {}\n", name, name);
println!("{}", line);
lines.push(line);
}
lines.sort();
for line in lines {
_contents += &format!("{}", line).to_string();
println!("{}" , line.to_string());
}*/
/*
let listing = fs::read_dir(full_path).unwrap();
println!("listing!");
for file in listing {
println!("{}\n", file.unwrap().path().display());
println!("=> {} {}\n", file.unwrap().path().display(), "name_of_file");
}
*/
//println!("{:?}", directory_listing());
_contents = format!("=> ..\n");
let files = fs::read_dir(full_path).unwrap();
for file in files {
let path = file.unwrap().path();
let file = path.display();
if path.is_dir() {
_contents += &format!("=> {}/ {}\n", file, file).to_string();
} else {
_contents += &format!("=> {} {}\n", file, file).to_string();
}
}
}
}
@ -120,9 +165,22 @@ fn handle_connection(mut stream: TcpStream) {
status_line,
_contents
);
println!("{}", response);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
}/*
fn directory_listing() -> io::Result<()> {
let mut entries = fs::read_dir(".")?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?;
// The order in which `read_dir` returns entries is not guaranteed. If reproducible
// // ordering is required the entries should be explicitly sorted.
entries.sort();
println!("{:?}", entries);
// The entries have now been sorted by their path.
Ok(())
}*/