Skip to content

Commit

Permalink
url-decode path in title and heading
Browse files Browse the repository at this point in the history
  • Loading branch information
ahti committed Aug 21, 2020
1 parent bb266c0 commit d8196e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{utf8_percent_encode, percent_decode_str, AsciiSet, CONTROLS};
use qrcodegen::{QrCode, QrCodeEcc};
use serde::Deserialize;
use std::io;
Expand Down Expand Up @@ -165,9 +165,17 @@ 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);
Expand Down Expand Up @@ -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,
)
Expand Down
13 changes: 7 additions & 6 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit d8196e7

Please sign in to comment.