From 06db56e40820ec0067d18840648ee786163a3862 Mon Sep 17 00:00:00 2001 From: jikstra Date: Sun, 25 Apr 2021 17:48:09 +0200 Subject: Implement a raw rendering mode for recursive folder download - Raw mode only displays file/folders and is more focused on computer processing - Display a banner in footer to recursively download the current folder with wget --- src/renderer.rs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 14 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index d1821dd..ac6b640 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -17,6 +17,7 @@ pub fn page( breadcrumbs: Vec, encoded_dir: &str, conf: &MiniserveConfig, + current_user: Option<&CurrentUser>, ) -> Markup { let upload_route = match conf.random_route { Some(ref random_route) => format!("/{}/upload", random_route), @@ -80,7 +81,7 @@ pub fn page( // wrapped in span so the text doesn't shift slightly when it turns into a link span { bdi { (el.name) } } } @else { - a href=(parametrized_link(&el.link, sort_method, sort_order)) { + a href=(parametrized_link(&el.link, sort_method, sort_order, false)) { bdi { (el.name) } } } @@ -120,22 +121,63 @@ pub fn page( tr { td colspan="3" { span.root-chevron { (chevron_left()) } - a.root href=(parametrized_link("../", sort_method, sort_order)) { + a.root href=(parametrized_link("../", sort_method, sort_order, false)) { "Parent directory" } } } } @for entry in entries { - (entry_row(entry, sort_method, sort_order)) + (entry_row(entry, sort_method, sort_order, false)) } } } a.back href="#top" { (arrow_up()) } - @if !conf.hide_version_footer { - (version_footer()) + div.footer { + (wget_download(&title_path, current_user)) + @if !conf.hide_version_footer { + (version_footer()) + } + } + } + } + } + } +} +/// Renders the file listing +#[allow(clippy::too_many_arguments)] +pub fn raw( + entries: Vec, + is_root: bool, + sort_method: Option, + sort_order: Option, +) -> Markup { + html! { + (DOCTYPE) + html { + body { + table { + thead { + th.name { "Name" } + th.size { "Size" } + th.date { "Last modification" } + } + tbody { + @if !is_root { + tr { + td colspan="3" { + a.root href=(parametrized_link("../", sort_method, sort_order, true)) { + ".." + } + } + } + } + @for entry in entries { + (entry_row(entry, sort_method, sort_order, true)) + } +>>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) } } } @@ -146,12 +188,36 @@ pub fn page( // Partial: version footer fn version_footer() -> Markup { html! { - p.footer { - (format!("{}/{}", crate_name!(), crate_version!())) - } + div.version { + (format!("{}/{}", crate_name!(), crate_version!())) + } } } +fn wget_download(title_path: &str, current_user: Option<&CurrentUser>) -> Markup { + let count = { + let count_slashes = title_path.matches('/').count(); + if count_slashes > 0 { + count_slashes - 1 + } else { + 0 + } + }; + + let user_params = if let Some(user) = current_user { + format!(" --ask-password --user {}", user.name) + } else { + "".to_string() + }; + + return html! { + div.downloadWget { + p { "Download folder:" } + div.cmd { (format!("wget -r -c -nH -np --cut-dirs={} -R \"index.html*\"{} \"http://{}/?raw=true\"", count, user_params, title_path)) } + } + }; +} + /// Build the action of the upload form fn build_upload_action( upload_route: &str, @@ -232,7 +298,7 @@ fn archive_button( } else { format!( "{}&download={}", - parametrized_link("", sort_method, sort_order,), + parametrized_link("", sort_method, sort_order, false), archive_method ) }; @@ -260,14 +326,19 @@ fn parametrized_link( link: &str, sort_method: Option, sort_order: Option, + raw: bool, ) -> String { + if raw { + return format!("{}?raw=true", make_link_with_trailing_slash(link)); + } + if let Some(method) = sort_method { if let Some(order) = sort_order { let parametrized_link = format!( "{}?sort={}&order={}", make_link_with_trailing_slash(link), method, - order + order, ); return parametrized_link; @@ -315,6 +386,7 @@ fn entry_row( entry: Entry, sort_method: Option, sort_order: Option, + raw: bool, ) -> Markup { html! { tr { @@ -322,7 +394,7 @@ fn entry_row( p { @if entry.is_dir() { @if let Some(symlink_dest) = entry.symlink_info { - a.symlink href=(parametrized_link(&entry.link, sort_method, sort_order)) { + a.symlink href=(parametrized_link(&entry.link, sort_method, sort_order, raw)) { (entry.name) "/" span.symlink-symbol { } a.directory {(symlink_dest) "/"} @@ -345,9 +417,11 @@ fn entry_row( } } - @if let Some(size) = entry.size { - span.mobile-info.size { - (size) + @if !raw { + @if let Some(size) = entry.size { + span.mobile-info.size { + (size) + } } } } @@ -477,6 +551,15 @@ pub fn render_error( conf: &MiniserveConfig, return_address: &str, ) -> Markup { +<<<<<<< HEAD +======= + let link = if has_referer { + return_address.to_string() + } else { + parametrized_link(return_address, sort_method, sort_order, false) + }; + +>>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) html! { (DOCTYPE) html { @@ -508,8 +591,15 @@ pub fn render_error( } } } +<<<<<<< HEAD @if !conf.hide_version_footer { (version_footer()) +======= + @if !hide_version_footer { + p.footer { + (version_footer()) + } +>>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) } } } -- cgit v1.2.3 From b94bb361e30e90656edfce5f9a63d41452ba100f Mon Sep 17 00:00:00 2001 From: jikstra Date: Thu, 2 Sep 2021 15:33:58 +0200 Subject: Fix rebase --- src/renderer.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index ac6b640..b18ac07 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -6,6 +6,7 @@ use maud::{html, Markup, PreEscaped, DOCTYPE}; use std::time::SystemTime; use strum::IntoEnumIterator; +use crate::auth::CurrentUser; use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; use crate::{archive::ArchiveMethod, MiniserveConfig}; @@ -19,11 +20,19 @@ pub fn page( conf: &MiniserveConfig, current_user: Option<&CurrentUser>, ) -> Markup { + // If query_params.raw is true, we want render a minimal directory listing + if query_params.raw.is_some() && query_params.raw.unwrap() == true { + return raw(entries, is_root); + } + let upload_route = match conf.random_route { Some(ref random_route) => format!("/{}/upload", random_route), None => "/upload".to_string(), }; let (sort_method, sort_order) = (query_params.sort, query_params.order); + + + let upload_action = build_upload_action(&upload_route, encoded_dir, sort_method, sort_order); let title_path = breadcrumbs @@ -31,6 +40,8 @@ pub fn page( .map(|el| el.name.clone()) .collect::>() .join("/"); + + html! { (DOCTYPE) @@ -150,9 +161,7 @@ pub fn page( #[allow(clippy::too_many_arguments)] pub fn raw( entries: Vec, - is_root: bool, - sort_method: Option, - sort_order: Option, + is_root: bool ) -> Markup { html! { (DOCTYPE) @@ -168,16 +177,15 @@ pub fn raw( @if !is_root { tr { td colspan="3" { - a.root href=(parametrized_link("../", sort_method, sort_order, true)) { + a.root href=(parametrized_link("../", None, None, true)) { ".." } } } } @for entry in entries { - (entry_row(entry, sort_method, sort_order, true)) + (entry_row(entry, None, None, true)) } ->>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) } } } @@ -400,7 +408,7 @@ fn entry_row( a.directory {(symlink_dest) "/"} } }@else { - a.directory href=(parametrized_link(&entry.link, sort_method, sort_order)) { + a.directory href=(parametrized_link(&entry.link, sort_method, sort_order, raw)) { (entry.name) "/" } } @@ -551,15 +559,6 @@ pub fn render_error( conf: &MiniserveConfig, return_address: &str, ) -> Markup { -<<<<<<< HEAD -======= - let link = if has_referer { - return_address.to_string() - } else { - parametrized_link(return_address, sort_method, sort_order, false) - }; - ->>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) html! { (DOCTYPE) html { @@ -591,15 +590,11 @@ pub fn render_error( } } } -<<<<<<< HEAD @if !conf.hide_version_footer { - (version_footer()) -======= - @if !hide_version_footer { p.footer { (version_footer()) } ->>>>>>> 2949329 (Implement a raw rendering mode for recursive folder download) + } } } -- cgit v1.2.3 From 680b0d001d58a32b0caac3263103dbd9ddb5fe84 Mon Sep 17 00:00:00 2001 From: jikstra Date: Thu, 2 Sep 2021 15:50:35 +0200 Subject: cargo fmt & cargo clippy --- src/renderer.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index b18ac07..45cb145 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -21,18 +21,16 @@ pub fn page( current_user: Option<&CurrentUser>, ) -> Markup { // If query_params.raw is true, we want render a minimal directory listing - if query_params.raw.is_some() && query_params.raw.unwrap() == true { + if query_params.raw.is_some() && query_params.raw.unwrap() { return raw(entries, is_root); } - + let upload_route = match conf.random_route { Some(ref random_route) => format!("/{}/upload", random_route), None => "/upload".to_string(), }; let (sort_method, sort_order) = (query_params.sort, query_params.order); - - - + let upload_action = build_upload_action(&upload_route, encoded_dir, sort_method, sort_order); let title_path = breadcrumbs @@ -40,8 +38,6 @@ pub fn page( .map(|el| el.name.clone()) .collect::>() .join("/"); - - html! { (DOCTYPE) @@ -159,10 +155,7 @@ pub fn page( } /// Renders the file listing #[allow(clippy::too_many_arguments)] -pub fn raw( - entries: Vec, - is_root: bool -) -> Markup { +pub fn raw(entries: Vec, is_root: bool) -> Markup { html! { (DOCTYPE) html { @@ -594,7 +587,7 @@ pub fn render_error( p.footer { (version_footer()) } - + } } } -- cgit v1.2.3 From 71ba52ec5dc84591ead1ac64e584f990b7d66615 Mon Sep 17 00:00:00 2001 From: jikstra Date: Fri, 10 Sep 2021 15:51:08 +0200 Subject: Apply requested changes --- src/renderer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index 45cb145..c0a243e 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -153,8 +153,8 @@ pub fn page( } } } + /// Renders the file listing -#[allow(clippy::too_many_arguments)] pub fn raw(entries: Vec, is_root: bool) -> Markup { html! { (DOCTYPE) @@ -212,7 +212,7 @@ fn wget_download(title_path: &str, current_user: Option<&CurrentUser>) -> Markup }; return html! { - div.downloadWget { + div.downloadDirectory { p { "Download folder:" } div.cmd { (format!("wget -r -c -nH -np --cut-dirs={} -R \"index.html*\"{} \"http://{}/?raw=true\"", count, user_params, title_path)) } } -- cgit v1.2.3 From 45650ea93b723db551fec8c77fbac5065c7659a6 Mon Sep 17 00:00:00 2001 From: jikstra Date: Fri, 10 Sep 2021 16:23:26 +0200 Subject: Implement --show-wget-footer argument --- src/renderer.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index c0a243e..3c2d008 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -143,7 +143,9 @@ pub fn page( (arrow_up()) } div.footer { - (wget_download(&title_path, current_user)) + @if conf.show_wget_footer { + (wget_footer(&title_path, current_user)) + } @if !conf.hide_version_footer { (version_footer()) } @@ -195,7 +197,7 @@ fn version_footer() -> Markup { } } -fn wget_download(title_path: &str, current_user: Option<&CurrentUser>) -> Markup { +fn wget_footer(title_path: &str, current_user: Option<&CurrentUser>) -> Markup { let count = { let count_slashes = title_path.matches('/').count(); if count_slashes > 0 { -- cgit v1.2.3