Skip to content

Commit

Permalink
Merge pull request #177 from ghost/fix-#173
Browse files Browse the repository at this point in the history
Fix #173
  • Loading branch information
svenstaro authored Feb 27, 2020
2 parents d490742 + 215447b commit 09f2d24
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 65 deletions.
9 changes: 8 additions & 1 deletion src/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn handle_multipart(
pub fn upload_file(
req: &HttpRequest<crate::MiniserveConfig>,
default_color_scheme: ColorScheme,
uses_random_route: bool
) -> FutureResponse<HttpResponse> {
let return_path = if let Some(header) = req.headers().get(header::REFERER) {
header.to_str().unwrap_or("/").to_owned()
Expand All @@ -146,6 +147,7 @@ pub fn upload_file(
query_params.order,
color_scheme,
default_color_scheme,
uses_random_route
));
}
};
Expand All @@ -165,6 +167,7 @@ pub fn upload_file(
query_params.order,
color_scheme,
default_color_scheme,
uses_random_route
));
}
};
Expand All @@ -184,6 +187,7 @@ pub fn upload_file(
query_params.order,
color_scheme,
default_color_scheme,
uses_random_route
));
}
};
Expand All @@ -208,12 +212,14 @@ pub fn upload_file(
query_params.order,
color_scheme,
default_color_scheme,
uses_random_route
),
}),
)
}

/// Convenience method for creating response errors, if file upload fails.
#[allow(clippy::too_many_arguments)]
fn create_error_response(
description: &str,
error_code: StatusCode,
Expand All @@ -222,6 +228,7 @@ fn create_error_response(
sorting_order: Option<SortingOrder>,
color_scheme: ColorScheme,
default_color_scheme: ColorScheme,
uses_random_route: bool
) -> FutureResult<HttpResponse, actix_web::error::Error> {
errors::log_error_chain(description.to_string());
future::ok(
Expand All @@ -237,7 +244,7 @@ fn create_error_response(
color_scheme,
default_color_scheme,
true,
true,
!uses_random_route,
)
.into_string(),
),
Expand Down
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
};

let random_route = app.state().random_route.clone().unwrap_or_default();
let uses_random_route = app.state().random_route.clone().is_some();
let full_route = format!("/{}", random_route);

if let Some(s) = s {
Expand All @@ -275,7 +276,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
// Allow file upload
app.resource(&upload_route, move |r| {
r.method(Method::POST)
.f(move |file| file_upload::upload_file(file, default_color_scheme))
.f(move |file| file_upload::upload_file(file, default_color_scheme, uses_random_route))
})
// Handle directories
.handler(&full_route, s)
Expand All @@ -295,11 +296,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
fn error_404(req: &HttpRequest<crate::MiniserveConfig>) -> Result<HttpResponse, io::Error> {
let err_404 = ContextualError::RouteNotFoundError(req.path().to_string());
let default_color_scheme = req.state().default_color_scheme;
let return_address = match &req.state().random_route {
Some(random_route) => format!("/{}", random_route),
None => "/".to_string(),
};

let uses_random_route = req.state().random_route.is_some();
let query_params = listing::extract_query_parameters(req);
let color_scheme = query_params.theme.unwrap_or(default_color_scheme);

Expand All @@ -309,13 +306,13 @@ fn error_404(req: &HttpRequest<crate::MiniserveConfig>) -> Result<HttpResponse,
renderer::render_error(
&err_404.to_string(),
StatusCode::NOT_FOUND,
&return_address,
"/",
query_params.sort,
query_params.order,
color_scheme,
default_color_scheme,
false,
true,
!uses_random_route,
)
.into_string(),
))
Expand Down
117 changes: 61 additions & 56 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,61 +33,64 @@ pub fn page(
);

html! {
(page_header(serve_path, color_scheme, file_upload, false))
body#drop-container {
@if file_upload {
div.drag-form {
div.drag-title {
h1 { "Drop your file here to upload it" }
(DOCTYPE)
html {
(page_header(serve_path, color_scheme, file_upload, false))
body#drop-container {
@if file_upload {
div.drag-form {
div.drag-title {
h1 { "Drop your file here to upload it" }
}
}
}
}
(color_scheme_selector(sort_method, sort_order, color_scheme, default_color_scheme, serve_path))
div.container {
span#top { }
h1.title { "Index of " (serve_path) }
div.toolbar {
div.download {
@for compression_method in CompressionMethod::iter() {
(archive_button(compression_method, sort_method, sort_order, color_scheme, default_color_scheme))
(color_scheme_selector(sort_method, sort_order, color_scheme, default_color_scheme, serve_path))
div.container {
span#top { }
h1.title { "Index of " (serve_path) }
div.toolbar {
div.download {
@for compression_method in CompressionMethod::iter() {
(archive_button(compression_method, sort_method, sort_order, color_scheme, default_color_scheme))
}
}
}
@if file_upload {
div.upload {
form id="file_submit" action=(upload_action) method="POST" enctype="multipart/form-data" {
p { "Select a file to upload or drag it anywhere into the window" }
div {
input#file-input type="file" name="file_to_upload" required="" {}
button type="submit" { "Upload file" }
@if file_upload {
div.upload {
form id="file_submit" action=(upload_action) method="POST" enctype="multipart/form-data" {
p { "Select a file to upload or drag it anywhere into the window" }
div {
input#file-input type="file" name="file_to_upload" required="" {}
button type="submit" { "Upload file" }
}
}
}
}
}
}
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)) }
}
tbody {
@if !is_root {
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"
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)) }
}
tbody {
@if !is_root {
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"
}
}
}
}
}
@for entry in entries {
(entry_row(entry, sort_method, sort_order, color_scheme, default_color_scheme))
@for entry in entries {
(entry_row(entry, sort_method, sort_order, color_scheme, default_color_scheme))
}
}
}
}
a.back href="#top" {
(arrow_up())
a.back href="#top" {
(arrow_up())
}
}
}
}
Expand Down Expand Up @@ -803,14 +806,13 @@ fn page_header(
is_error: bool,
) -> Markup {
html! {
(DOCTYPE)
html {
head {
meta charset="utf-8";
meta http-equiv="X-UA-Compatible" content="IE=edge";
meta name="viewport" content="width=device-width, initial-scale=1";
@if is_error {
title { (serve_path) }
} else {
} @else {
title { "Index of " (serve_path) }
}
style { (css(color_scheme)) }
Expand Down Expand Up @@ -905,17 +907,20 @@ pub fn render_error(
};

html! {
body {
(DOCTYPE)
html {
(page_header(&error_code.to_string(), color_scheme, false, true))
div.error {
p { (error_code.to_string()) }
@for error in error_description.lines() {
p { (error) }
}
@if display_back_link {
div.error-nav {
a.error-back href=(link) {
"Go back to file listing"
body {
div.error {
p { (error_code.to_string()) }
@for error in error_description.lines() {
p { (error) }
}
@if display_back_link {
div.error-nav {
a.error-back href=(link) {
"Go back to file listing"
}
}
}
}
Expand Down

0 comments on commit 09f2d24

Please sign in to comment.