aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/listing.rs15
-rw-r--r--src/renderer.rs59
2 files changed, 44 insertions, 30 deletions
diff --git a/src/listing.rs b/src/listing.rs
index ee9c581..4a69108 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -138,7 +138,6 @@ pub fn directory_listing<S>(
let base = Path::new(serve_path);
let random_route = format!("/{}", random_route.unwrap_or_default());
let is_root = base.parent().is_none() || req.path() == random_route;
- let page_parent = base.parent().map(|p| p.display().to_string());
let current_dir = match base.strip_prefix(random_route) {
Ok(c_d) => Path::new("/").join(c_d),
Err(_) => base.to_path_buf(),
@@ -285,6 +284,19 @@ pub fn directory_listing<S>(
.chunked()
.body(Body::Streaming(Box::new(rx))))
} else {
+ // Redirect to directory
+ if !renderer::has_trailing(&serve_path) {
+ let query = match req.query_string() {
+ "" => String::new(),
+ _ => format!("?{}", req.query_string())
+ };
+ return Ok(
+ HttpResponse::MovedPermanenty()
+ .header("Location", format!("{}/{}", serve_path, query))
+ .body("301")
+ );
+ }
+
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(
@@ -292,7 +304,6 @@ pub fn directory_listing<S>(
serve_path,
entries,
is_root,
- page_parent,
query_params.sort,
query_params.order,
default_color_scheme,
diff --git a/src/renderer.rs b/src/renderer.rs
index e08b9d4..4852df1 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -15,7 +15,7 @@ pub fn page(
serve_path: &str,
entries: Vec<Entry>,
is_root: bool,
- page_parent: Option<String>,
+ // page_parent: Option<String>,
sort_method: Option<SortingMethod>,
sort_order: Option<SortingOrder>,
default_color_scheme: ColorScheme,
@@ -73,13 +73,11 @@ pub fn page(
}
tbody {
@if !is_root {
- @if let Some(parent) = page_parent {
- tr {
- td colspan="3" {
- span.root-chevron { (chevron_left()) }
- a.root href=(parametrized_link(&parent, sort_method, sort_order, color_scheme, default_color_scheme)) {
- "Parent directory"
- }
+ tr {
+ td colspan="3" {
+ span.root-chevron { (chevron_left()) }
+ a.root href=(parametrized_link("../", sort_method, sort_order, color_scheme, default_color_scheme)) {
+ "Parent directory"
}
}
}
@@ -218,17 +216,20 @@ fn archive_button(
}
}
-// Append to the string after the link
-fn get_dir_last(link: &str) -> &str {
- match link.chars().last() {
- Some(d) => {
- if d == '/' {
- ""
- } else {
- "/"
- }
- },
- None => "/"
+// Is there a trailing "/"
+pub fn has_trailing(s: &str) -> bool {
+ match s.chars().last() {
+ Some(d) => d == '/',
+ None => false
+ }
+}
+
+// Add trailing "/" if conditions permit
+fn add_trailing(link: &str) -> String {
+ if has_trailing(&link) {
+ link.to_string()
+ }else {
+ format!("{}/", link)
}
}
@@ -243,9 +244,8 @@ fn parametrized_link(
if let Some(method) = sort_method {
if let Some(order) = sort_order {
let parametrized_link = format!(
- "{}{}?sort={}&order={}",
- link,
- get_dir_last(&link),
+ "{}?sort={}&order={}",
+ add_trailing(&link),
method,
order
);
@@ -260,14 +260,13 @@ fn parametrized_link(
if color_scheme != default_color_scheme {
return format!(
- "{}{}?theme={}",
- link,
- get_dir_last(&link),
+ "{}?theme={}",
+ add_trailing(&link),
color_scheme.to_slug()
);
}
- format!("{}/", link)
+ add_trailing(&link)
}
/// Partial: table header link
@@ -698,7 +697,7 @@ fn css(color_scheme: ColorScheme) -> Markup {
display: block;
}}
table tbody tr td {{
- padding-top: 10px;
+ padding-top: 0;
padding-bottom: 0;
}}
a.directory {{
@@ -708,10 +707,14 @@ fn css(color_scheme: ColorScheme) -> Markup {
.file-entry {{
align-items: center;
}}
- a.file {{
+ a.root, a.file, a.symlink {{
+ display: inline-block;
flex: 1;
padding: 0.5625rem 0;
}}
+ a.symlink {{
+ width: 100%;
+ }}
.back {{
display: flex;
}}