💄 Switched to a handlebars template for the whole dir index
parent
55b3e9f520
commit
1fb0c2aafa
|
@ -1,4 +1,5 @@
|
|||
<ul>
|
||||
<li><a href="https://example.com">..</a></li>
|
||||
{{#each entries}}
|
||||
<li>
|
||||
<a href="{{this.encoded_file_name}}{{this.trailing_slash}}">
|
||||
|
|
|
@ -92,7 +92,12 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
|||
|
||||
let mut handlebars = handlebars::Handlebars::new ();
|
||||
|
||||
handlebars.register_template_file ("file_server_dir_entry", "ptth_handlebars/file_server_dir_entry.html")?;
|
||||
for (k, v) in vec! [
|
||||
("file_server_dir_entry", "ptth_handlebars/file_server_dir_entry.html"),
|
||||
("file_server_dir", "ptth_handlebars/file_server_dir.html"),
|
||||
].into_iter () {
|
||||
handlebars.register_template_file (k, v)?;
|
||||
}
|
||||
|
||||
let handlebars = Arc::new (handlebars);
|
||||
|
||||
|
|
|
@ -47,26 +47,22 @@ fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
|
|||
(start, end)
|
||||
}
|
||||
|
||||
async fn serve_dir (
|
||||
handlebars: Arc <Handlebars <'static>>,
|
||||
mut dir: ReadDir
|
||||
) -> http_serde::Response {
|
||||
let (tx, rx) = channel (2);
|
||||
use serde::Serialize;
|
||||
use tokio::fs::DirEntry;
|
||||
|
||||
let handlebars = handlebars.clone ();
|
||||
tokio::spawn (async move {
|
||||
let mut tx = tx;
|
||||
// This could probably be done with borrows, if I owned the
|
||||
// tokio::fs::DirEntry instead of consuming it
|
||||
|
||||
tx.send (Ok::<_, Infallible> (String::from ("<style>body {font-family:sans;}</style><ul>").into_bytes ())).await.unwrap ();
|
||||
|
||||
let handlebars = handlebars.clone ();
|
||||
while let Ok (entry) = dir.next_entry ().await {
|
||||
let handlebars = handlebars.clone ();
|
||||
let entry: tokio::fs::DirEntry = match entry {
|
||||
Some (x) => x,
|
||||
None => break,
|
||||
};
|
||||
#[derive (Serialize)]
|
||||
struct TemplateDirEntry {
|
||||
trailing_slash: &'static str,
|
||||
file_name: String,
|
||||
encoded_file_name: String,
|
||||
error: bool,
|
||||
}
|
||||
|
||||
async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
||||
{
|
||||
let trailing_slash = match entry.file_type ().await {
|
||||
Ok (t) => if t.is_dir () {
|
||||
"/"
|
||||
|
@ -79,51 +75,52 @@ async fn serve_dir (
|
|||
|
||||
let file_name = match entry.file_name ().into_string () {
|
||||
Ok (x) => x,
|
||||
Err (_) => {
|
||||
let s = format! ("<li>(file_name error)</li>\n");
|
||||
if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Err (_) => return TemplateDirEntry {
|
||||
trailing_slash: "",
|
||||
file_name: "".into (),
|
||||
encoded_file_name: "".into (),
|
||||
error: true,
|
||||
},
|
||||
};
|
||||
|
||||
use std::borrow::Cow;
|
||||
use percent_encoding::*;
|
||||
|
||||
let encoded_file_name: Cow <str> = utf8_percent_encode (&file_name, CONTROLS).into ();
|
||||
let encoded_file_name = utf8_percent_encode (&file_name, CONTROLS).to_string ();
|
||||
|
||||
#[derive (serde::Serialize)]
|
||||
struct TemplateDirEntry <'a> {
|
||||
trailing_slash: &'a str,
|
||||
file_name: &'a str,
|
||||
encoded_file_name: Cow <'a, str>,
|
||||
TemplateDirEntry {
|
||||
trailing_slash: &trailing_slash,
|
||||
file_name,
|
||||
encoded_file_name,
|
||||
error: false,
|
||||
}
|
||||
}
|
||||
|
||||
let s = handlebars.render (
|
||||
"file_server_dir_entry",
|
||||
&TemplateDirEntry {
|
||||
trailing_slash: &trailing_slash,
|
||||
file_name: &file_name,
|
||||
encoded_file_name,
|
||||
async fn serve_dir (
|
||||
handlebars: Arc <Handlebars <'static>>,
|
||||
mut dir: ReadDir
|
||||
) -> http_serde::Response
|
||||
{
|
||||
let mut entries = vec! [];
|
||||
|
||||
while let Ok (Some (entry)) = dir.next_entry ().await {
|
||||
entries.push (read_dir_entry (entry).await);
|
||||
}
|
||||
|
||||
#[derive (Serialize)]
|
||||
struct TemplateDirPage {
|
||||
entries: Vec <TemplateDirEntry>,
|
||||
}
|
||||
|
||||
let s = handlebars.render ("file_server_dir", &TemplateDirPage {
|
||||
entries,
|
||||
}).unwrap ();
|
||||
|
||||
if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tx.send (Ok::<_, Infallible> (String::from ("</ul>").into_bytes ())).await.unwrap ();
|
||||
});
|
||||
|
||||
let mut response = http_serde::Response::default ();
|
||||
|
||||
response.header ("content-type".into (), String::from ("text/html").into_bytes ());
|
||||
response.body (rx);
|
||||
|
||||
response
|
||||
let mut resp = http_serde::Response::default ();
|
||||
resp
|
||||
.header ("content-type".to_string (), "text/html".to_string ().into_bytes ())
|
||||
.body_bytes (s.into_bytes ())
|
||||
;
|
||||
resp
|
||||
}
|
||||
|
||||
async fn serve_file (
|
||||
|
|
Loading…
Reference in New Issue