aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven-Hendrik Haase <svenstaro@gmail.com>2020-08-24 20:24:14 +0000
committerGitHub <noreply@github.com>2020-08-24 20:24:14 +0000
commit5a8b7677076cb89d3fc7d57a89a51245e3dc66eb (patch)
treecc5c186014c24c8dd0697a7b759c79acd66d6593
parentBump some deps (diff)
parentmake linter happy (diff)
downloadminiserve-5a8b7677076cb89d3fc7d57a89a51245e3dc66eb.tar.gz
miniserve-5a8b7677076cb89d3fc7d57a89a51245e3dc66eb.zip
Merge pull request #362 from ahti/master
visual and sorting fixes
-rw-r--r--src/listing.rs61
-rw-r--r--src/renderer.rs32
2 files changed, 55 insertions, 38 deletions
diff --git a/src/listing.rs b/src/listing.rs
index 388153f..0ffa2df 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -5,7 +5,7 @@ use actix_web::web::Query;
use actix_web::{HttpRequest, HttpResponse, Result};
use bytesize::ByteSize;
use htmlescape::encode_minimal as escape_html_entity;
-use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
+use percent_encoding::{percent_decode_str, utf8_percent_encode, AsciiSet, CONTROLS};
use qrcodegen::{QrCode, QrCodeEcc};
use serde::Deserialize;
use std::io;
@@ -165,9 +165,21 @@ pub fn directory_listing(
let base = Path::new(serve_path);
let random_route = format!("/{}", random_route.unwrap_or_default());
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route);
- let current_dir = match base.strip_prefix(random_route) {
+
+ let encoded_dir = match base.strip_prefix(random_route) {
Ok(c_d) => Path::new("/").join(c_d),
Err(_) => base.to_path_buf(),
+ }
+ .display()
+ .to_string();
+
+ let display_dir = {
+ let decoded = percent_decode_str(&encoded_dir).decode_utf8_lossy();
+ if is_root {
+ decoded.to_string()
+ } else {
+ format!("{}/", decoded)
+ }
};
let query_params = extract_query_parameters(req);
@@ -241,29 +253,25 @@ pub fn directory_listing(
}
}
- if let Some(sorting_method) = query_params.sort {
- match sorting_method {
- SortingMethod::Name => entries
- .sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())),
- SortingMethod::Size => entries.sort_by(|e1, e2| {
- // If we can't get the size of the entry (directory for instance)
- // let's consider it's 0b
- e2.size
- .unwrap_or_else(|| ByteSize::b(0))
- .cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
- }),
- SortingMethod::Date => entries.sort_by(|e1, e2| {
- // If, for some reason, we can't get the last modification date of an entry
- // let's consider it was modified on UNIX_EPOCH (01/01/19270 00:00:00)
- e2.last_modification_date
- .unwrap_or(SystemTime::UNIX_EPOCH)
- .cmp(&e1.last_modification_date.unwrap_or(SystemTime::UNIX_EPOCH))
- }),
- };
- } else {
- // Sort in alphanumeric order by default
- entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone()))
- }
+ match query_params.sort.unwrap_or(SortingMethod::Name) {
+ SortingMethod::Name => entries.sort_by(|e1, e2| {
+ alphanumeric_sort::compare_str(e1.name.to_lowercase(), e2.name.to_lowercase())
+ }),
+ SortingMethod::Size => entries.sort_by(|e1, e2| {
+ // If we can't get the size of the entry (directory for instance)
+ // let's consider it's 0b
+ e2.size
+ .unwrap_or_else(|| ByteSize::b(0))
+ .cmp(&e1.size.unwrap_or_else(|| ByteSize::b(0)))
+ }),
+ SortingMethod::Date => entries.sort_by(|e1, e2| {
+ // If, for some reason, we can't get the last modification date of an entry
+ // let's consider it was modified on UNIX_EPOCH (01/01/19270 00:00:00)
+ e2.last_modification_date
+ .unwrap_or(SystemTime::UNIX_EPOCH)
+ .cmp(&e1.last_modification_date.unwrap_or(SystemTime::UNIX_EPOCH))
+ }),
+ };
if let Some(sorting_order) = query_params.order {
if let SortingOrder::Descending = sorting_order {
@@ -350,7 +358,8 @@ pub fn directory_listing(
show_qrcode,
file_upload,
&upload_route,
- &current_dir.display().to_string(),
+ &encoded_dir,
+ &display_dir,
tar_enabled,
zip_enabled,
)
diff --git a/src/renderer.rs b/src/renderer.rs
index 7270e8f..17e0119 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -22,13 +22,14 @@ pub fn page(
show_qrcode: bool,
file_upload: bool,
upload_route: &str,
- current_dir: &str,
+ encoded_dir: &str,
+ display_dir: &str,
tar_enabled: bool,
zip_enabled: bool,
) -> Markup {
let upload_action = build_upload_action(
upload_route,
- current_dir,
+ encoded_dir,
sort_method,
sort_order,
color_scheme,
@@ -38,7 +39,7 @@ pub fn page(
html! {
(DOCTYPE)
html {
- (page_header(serve_path, color_scheme, file_upload, false))
+ (page_header(display_dir, color_scheme, file_upload, false))
body#drop-container {
@if file_upload {
div.drag-form {
@@ -50,7 +51,7 @@ pub fn page(
(color_scheme_selector(sort_method, sort_order, color_scheme, default_color_scheme, serve_path, show_qrcode))
div.container {
span#top { }
- h1.title { "Index of " (serve_path) }
+ h1.title { "Index of " (display_dir) }
div.toolbar {
@if tar_enabled || zip_enabled {
div.download {
@@ -75,9 +76,9 @@ pub fn page(
}
table {
thead {
- th { (build_link("name", "Name", sort_method, sort_order, color_scheme, default_color_scheme)) }
- th { (build_link("size", "Size", sort_method, sort_order, color_scheme, default_color_scheme)) }
- th { (build_link("date", "Last modification", sort_method, sort_order, color_scheme, default_color_scheme)) }
+ th.name { (build_link("name", "Name", sort_method, sort_order, color_scheme, default_color_scheme)) }
+ th.size { (build_link("size", "Size", sort_method, sort_order, color_scheme, default_color_scheme)) }
+ th.date { (build_link("date", "Last modification", sort_method, sort_order, color_scheme, default_color_scheme)) }
}
tbody {
@if !is_root {
@@ -107,13 +108,13 @@ pub fn page(
/// Build the action of the upload form
fn build_upload_action(
upload_route: &str,
- current_dir: &str,
+ encoded_dir: &str,
sort_method: Option<SortingMethod>,
sort_order: Option<SortingOrder>,
color_scheme: ColorScheme,
default_color_scheme: ColorScheme,
) -> String {
- let mut upload_action = format!("{}?path={}", upload_route, current_dir);
+ let mut upload_action = format!("{}?path={}", upload_route, encoded_dir);
if let Some(sorting_method) = sort_method {
upload_action = format!("{}&sort={}", upload_action, &sorting_method);
}
@@ -352,7 +353,7 @@ fn entry_row(
}
}
}
- td {
+ td.size-cell {
@if let Some(size) = entry.size {
(size)
}
@@ -533,12 +534,17 @@ fn css(color_scheme: ColorScheme) -> Markup {
color: {table_text_color};
text-align: left;
line-height: 1.125rem;
- width: 33.333%;
}}
table thead tr th {{
padding: 0.5rem 0.625rem 0.625rem;
font-weight: bold;
}}
+ table thead th.size {{
+ width: 6em;
+ }}
+ table thead th.date {{
+ width: 15em;
+ }}
table tbody tr:nth-child(odd) {{
background: {odd_row_background};
}}
@@ -551,9 +557,11 @@ fn css(color_scheme: ColorScheme) -> Markup {
table tbody tr:hover {{
background: {active_row_color};
}}
+ td.size-cell {{
+ text-align: right;
+ }}
td.date-cell {{
display: flex;
- width: calc(100% - 1.25rem);
justify-content: space-between;
}}
.at {{