💄 Make directory indexes prettier

main
_ 2020-10-31 15:28:27 +00:00
parent 1fb0c2aafa
commit 0572de2ad0
3 changed files with 85 additions and 8 deletions

View File

@ -1,10 +1,39 @@
<ul> <html>
<li><a href="https://example.com">..</a></li> <head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: sans;
}
.entry {
display: inline-block;
padding: 10px;
min-width: 50%;
}
.entry_list div:nth-child(odd) {
background-color: #ddd;
}
</style>
<title>{{path}}</title>
</head>
<body>
<div class="entry_list">
<div>
<a class="entry" href="https://example.com">../</a>
</div>
{{#each entries}} {{#each entries}}
<li> <div>
<a href="{{this.encoded_file_name}}{{this.trailing_slash}}"> <a class="entry" href="{{this.encoded_file_name}}{{this.trailing_slash}}">
{{this.file_name}}{{this.trailing_slash}} {{this.file_name}}{{this.trailing_slash}}
</a> </a>
</li> </div>
{{/each}} {{/each}}
</ul>
</div>
</body>
</html>

View File

@ -0,0 +1,43 @@
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: sans;
}
.entry {
display: inline-block;
padding: 20px;
min-width: 50%;
}
.entry_list div:nth-child(odd) {
background-color: #ddd;
}
</style>
<title>/home/user/stuff</title>
</head>
<body>
<div class="entry_list">
<div>
<a class="entry" href="https://example.com">../</a>
</div>
<div>
<a class="entry" href="some_dir/">some_dir/</a>
</div>
<div>
<a class="entry" href="alice">alice</a>
</div>
<div>
<a class="entry" href="alice">alice</a>
</div>
</div>
</body>
</html>

View File

@ -95,8 +95,11 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
} }
} }
use std::borrow::Cow;
async fn serve_dir ( async fn serve_dir (
handlebars: Arc <Handlebars <'static>>, handlebars: Arc <Handlebars <'static>>,
path: Cow <'_, str>,
mut dir: ReadDir mut dir: ReadDir
) -> http_serde::Response ) -> http_serde::Response
{ {
@ -107,11 +110,13 @@ async fn serve_dir (
} }
#[derive (Serialize)] #[derive (Serialize)]
struct TemplateDirPage { struct TemplateDirPage <'a> {
path: Cow <'a, str>,
entries: Vec <TemplateDirEntry>, entries: Vec <TemplateDirEntry>,
} }
let s = handlebars.render ("file_server_dir", &TemplateDirPage { let s = handlebars.render ("file_server_dir", &TemplateDirPage {
path,
entries, entries,
}).unwrap (); }).unwrap ();
@ -258,7 +263,7 @@ pub async fn serve_all (
full_path.push (&*path); full_path.push (&*path);
if let Ok (dir) = read_dir (&full_path).await { if let Ok (dir) = read_dir (&full_path).await {
serve_dir (handlebars, dir).await serve_dir (handlebars, full_path.to_string_lossy (), dir).await
} }
else if let Ok (file) = File::open (&full_path).await { else if let Ok (file) = File::open (&full_path).await {
serve_file ( serve_file (