From 111a3dc44730e0e24a5aa4218e8e385b236a619d Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Fri, 26 Apr 2019 19:09:17 +0200 Subject: Merged query parameter structs + improved file upload errors --- src/listing.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index a030feb..e718702 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -5,7 +5,7 @@ use htmlescape::encode_minimal as escape_html_entity; use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; use serde::Deserialize; use std::io; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::time::SystemTime; use strum_macros::{Display, EnumString}; @@ -16,7 +16,8 @@ use crate::themes; /// Query parameters #[derive(Deserialize)] -struct QueryParameters { +pub struct QueryParameters { + pub path: Option, sort: Option, order: Option, download: Option, -- cgit v1.2.3 From 4a9711b71674c6b62512dd6286869690d4497bcc Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Fri, 26 Apr 2019 20:08:16 +0200 Subject: Made ColorScheme, SortingMethod and SortingOrder enums derive Copy --- src/listing.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index e718702..7cc125d 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -25,7 +25,7 @@ pub struct QueryParameters { } /// Available sorting methods -#[derive(Deserialize, Clone, EnumString, Display)] +#[derive(Deserialize, Clone, EnumString, Display, Copy)] #[serde(rename_all = "snake_case")] #[strum(serialize_all = "snake_case")] pub enum SortingMethod { @@ -40,7 +40,7 @@ pub enum SortingMethod { } /// Available sorting orders -#[derive(Deserialize, Clone, EnumString, Display)] +#[derive(Deserialize, Clone, EnumString, Display, Copy)] pub enum SortingOrder { /// Ascending order #[serde(alias = "asc")] @@ -146,10 +146,10 @@ pub fn directory_listing( let (sort_method, sort_order, download, color_scheme) = if let Ok(query) = Query::::extract(req) { ( - query.sort.clone(), - query.order.clone(), + query.sort, + query.order, query.download.clone(), - query.theme.clone(), + query.theme, ) } else { (None, None, None, None) @@ -211,7 +211,7 @@ pub fn directory_listing( } } - if let Some(sorting_method) = &sort_method { + if let Some(sorting_method) = sort_method { match sorting_method { SortingMethod::Name => entries .sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())), @@ -235,13 +235,13 @@ pub fn directory_listing( entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())) } - if let Some(sorting_order) = &sort_order { + if let Some(sorting_order) = sort_order { if let SortingOrder::Descending = sorting_order { entries.reverse() } } - let color_scheme = color_scheme.unwrap_or_else(|| default_color_scheme.clone()); + let color_scheme = color_scheme.unwrap_or_else(|| default_color_scheme); if let Some(compression_method) = &download { log::info!( -- cgit v1.2.3 From fd3392636ce013cf8193a0881fa08680a8239698 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Sat, 27 Apr 2019 12:12:42 +0200 Subject: Themed errors --- src/listing.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 7cc125d..cadbb99 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -18,10 +18,10 @@ use crate::themes; #[derive(Deserialize)] pub struct QueryParameters { pub path: Option, - sort: Option, - order: Option, + pub sort: Option, + pub order: Option, + pub theme: Option, download: Option, - theme: Option, } /// Available sorting methods @@ -145,12 +145,7 @@ pub fn directory_listing( let (sort_method, sort_order, download, color_scheme) = if let Ok(query) = Query::::extract(req) { - ( - query.sort, - query.order, - query.download.clone(), - query.theme, - ) + (query.sort, query.order, query.download.clone(), query.theme) } else { (None, None, None, None) }; @@ -159,7 +154,7 @@ pub fn directory_listing( for entry in dir.path.read_dir()? { if dir.is_visible(&entry) { - let entry = entry.unwrap(); + let entry = entry?; let p = match entry.path().strip_prefix(&dir.path) { Ok(p) => base.join(p), Err(_) => continue, @@ -267,7 +262,18 @@ pub fn directory_listing( errors::log_error_chain(err.to_string()); Ok(HttpResponse::Ok() .status(http::StatusCode::INTERNAL_SERVER_ERROR) - .body(renderer::render_error(&err.to_string(), serve_path).into_string())) + .body( + renderer::render_error( + &err.to_string(), + serve_path, + sort_method, + sort_order, + color_scheme, + default_color_scheme, + false, + ) + .into_string(), + )) } } } else { -- cgit v1.2.3 From fa0c2865366b1bb65a2977c4b9608c9a92fc5889 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Wed, 1 May 2019 18:23:29 +0200 Subject: Use HTTP StatusCode for error title --- src/listing.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index cadbb99..a37563b 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -1,4 +1,5 @@ use actix_web::{fs, http, Body, FromRequest, HttpRequest, HttpResponse, Query, Result}; +use actix_web::http::StatusCode; use bytesize::ByteSize; use futures::stream::once; use htmlescape::encode_minimal as escape_html_entity; @@ -265,6 +266,7 @@ pub fn directory_listing( .body( renderer::render_error( &err.to_string(), + StatusCode::INTERNAL_SERVER_ERROR, serve_path, sort_method, sort_order, -- cgit v1.2.3 From 321b0ce7a693780830780f19de1cdec31657d2db Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Thu, 2 May 2019 07:23:38 +0200 Subject: Cargo fmt --- src/listing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index a37563b..22e43f0 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -1,5 +1,5 @@ -use actix_web::{fs, http, Body, FromRequest, HttpRequest, HttpResponse, Query, Result}; use actix_web::http::StatusCode; +use actix_web::{fs, http, Body, FromRequest, HttpRequest, HttpResponse, Query, Result}; use bytesize::ByteSize; use futures::stream::once; use htmlescape::encode_minimal as escape_html_entity; -- cgit v1.2.3 From dd8d11c698435217c370b940b41d060a614892c1 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Thu, 2 May 2019 21:36:47 +0200 Subject: Read query params to handle error back link --- src/listing.rs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 22e43f0..2e3093d 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -10,10 +10,10 @@ use std::path::{Path, PathBuf}; use std::time::SystemTime; use strum_macros::{Display, EnumString}; -use crate::archive; +use crate::archive::{self, CompressionMethod}; use crate::errors; use crate::renderer; -use crate::themes; +use crate::themes::ColorScheme; /// Query parameters #[derive(Deserialize)] @@ -21,8 +21,8 @@ pub struct QueryParameters { pub path: Option, pub sort: Option, pub order: Option, - pub theme: Option, - download: Option, + pub theme: Option, + download: Option, } /// Available sorting methods @@ -131,7 +131,7 @@ pub fn directory_listing( skip_symlinks: bool, file_upload: bool, random_route: Option, - default_color_scheme: themes::ColorScheme, + default_color_scheme: ColorScheme, upload_route: String, ) -> Result { let serve_path = req.path(); @@ -144,12 +144,7 @@ pub fn directory_listing( Err(_) => base.to_path_buf(), }; - let (sort_method, sort_order, download, color_scheme) = - if let Ok(query) = Query::::extract(req) { - (query.sort, query.order, query.download.clone(), query.theme) - } else { - (None, None, None, None) - }; + let (sort_method, sort_order, download, color_scheme, _) = extract_query_parameters(req); let mut entries: Vec = Vec::new(); @@ -273,6 +268,7 @@ pub fn directory_listing( color_scheme, default_color_scheme, false, + true, ) .into_string(), )) @@ -299,3 +295,25 @@ pub fn directory_listing( )) } } + +pub fn extract_query_parameters( + req: &HttpRequest, +) -> ( + Option, + Option, + Option, + Option, + Option, +) { + if let Ok(query) = Query::::extract(req) { + ( + query.sort, + query.order, + query.download.clone(), + query.theme, + query.path.clone(), + ) + } else { + (None, None, None, None, None) + } +} -- cgit v1.2.3 From 74b95a37ee569e5d06577d80703fbb246a254bf5 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Thu, 2 May 2019 22:00:44 +0200 Subject: Print error when parsing query parameters fail --- src/listing.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 2e3093d..2d00836 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -11,7 +11,7 @@ use std::time::SystemTime; use strum_macros::{Display, EnumString}; use crate::archive::{self, CompressionMethod}; -use crate::errors; +use crate::errors::{self, ContextualError}; use crate::renderer; use crate::themes::ColorScheme; @@ -305,15 +305,18 @@ pub fn extract_query_parameters( Option, Option, ) { - if let Ok(query) = Query::::extract(req) { - ( + match Query::::extract(req) { + Ok(query) => ( query.sort, query.order, query.download.clone(), query.theme, query.path.clone(), - ) - } else { - (None, None, None, None, None) + ), + Err(e) => { + let err = ContextualError::ParseError("query parameters".to_string(), e.to_string()); + errors::log_error_chain(err.to_string()); + (None, None, None, None, None) + } } } -- cgit v1.2.3 From a73a74283f64986ff6a0b6da4c234a828bc52522 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Fri, 3 May 2019 19:32:51 +0200 Subject: Return QueryParameters struct instead of tuple --- src/listing.rs | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index b3c21ae..49802bc 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -145,7 +145,7 @@ pub fn directory_listing( Err(_) => base.to_path_buf(), }; - let (sort_method, sort_order, download, color_scheme, _) = extract_query_parameters(req); + let query_params = extract_query_parameters(req); let mut entries: Vec = Vec::new(); @@ -203,7 +203,7 @@ pub fn directory_listing( } } - if let Some(sorting_method) = sort_method { + 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())), @@ -227,15 +227,15 @@ pub fn directory_listing( entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())) } - if let Some(sorting_order) = sort_order { + if let Some(sorting_order) = query_params.order { if let SortingOrder::Descending = sorting_order { entries.reverse() } } - let color_scheme = color_scheme.unwrap_or_else(|| default_color_scheme); + let color_scheme = query_params.theme.unwrap_or(default_color_scheme); - if let Some(compression_method) = &download { + if let Some(compression_method) = &query_params.download { log::info!( "Creating an archive ({extension}) of {path}...", extension = compression_method.extension(), @@ -264,8 +264,8 @@ pub fn directory_listing( &err.to_string(), StatusCode::INTERNAL_SERVER_ERROR, serve_path, - sort_method, - sort_order, + query_params.sort, + query_params.order, color_scheme, default_color_scheme, false, @@ -284,8 +284,8 @@ pub fn directory_listing( entries, is_root, page_parent, - sort_method, - sort_order, + query_params.sort, + query_params.order, default_color_scheme, color_scheme, file_upload, @@ -297,27 +297,25 @@ pub fn directory_listing( } } -pub fn extract_query_parameters( - req: &HttpRequest, -) -> ( - Option, - Option, - Option, - Option, - Option, -) { +pub fn extract_query_parameters(req: &HttpRequest) -> QueryParameters { match Query::::extract(req) { - Ok(query) => ( - query.sort, - query.order, - query.download.clone(), - query.theme, - query.path.clone(), - ), + Ok(query) => QueryParameters { + sort: query.sort, + order: query.order, + download: query.download.clone(), + theme: query.theme, + path: query.path.clone(), + }, Err(e) => { let err = ContextualError::ParseError("query parameters".to_string(), e.to_string()); errors::log_error_chain(err.to_string()); - (None, None, None, None, None) + QueryParameters { + sort: None, + order: None, + download: None, + theme: None, + path: None, + } } } } -- cgit v1.2.3