From 9857f26bdd4e9e87b13a9755b1e00569e9459238 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 19:59:59 +0100 Subject: Download folder as a tar working --- src/listing.rs | 73 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 19 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 57bef17..565b5bf 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -1,12 +1,15 @@ -use actix_web::{fs, FromRequest, HttpRequest, HttpResponse, Query, Result}; +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; use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; +use yansi::Paint; +use crate::archive; use crate::renderer; /// Query parameters @@ -14,6 +17,7 @@ use crate::renderer; struct QueryParameters { sort: Option, order: Option, + download: Option, } /// Available sorting methods @@ -134,11 +138,16 @@ pub fn directory_listing( let is_root = base.parent().is_none() || req.path() == random_route; let page_parent = base.parent().map(|p| p.display().to_string()); - let (sort_method, sort_order) = if let Ok(query) = Query::::extract(req) { - (query.sort.clone(), query.order.clone()) - } else { - (None, None) - }; + let (sort_method, sort_order, download) = + if let Ok(query) = Query::::extract(req) { + ( + query.sort.clone(), + query.order.clone(), + query.download.clone(), + ) + } else { + (None, None, None) + }; let mut entries: Vec = Vec::new(); @@ -218,17 +227,43 @@ pub fn directory_listing( } } - Ok(HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body( - renderer::page( - &title, - entries, - is_root, - page_parent, - sort_method, - sort_order, - ) - .into_string(), - )) + if let Some(compression_method) = &download { + match archive::create_archive_file(&compression_method, &dir.path) { + Ok((filename, content_length, content)) => Ok(HttpResponse::Ok() + .content_type("application/tar") + .content_length(content_length as u64) + .header("Content-Transfer-Encoding", "binary") + .header( + "Content-Disposition", + format!("attachment; filename={:?}", filename), + ) + .chunked() + .body(Body::Streaming(Box::new(once(Ok(content)))))), + Err(err) => { + println!( + "{error} an error occured while compressing {folder}: {err:?}", + error = Paint::red("error:").bold(), + folder = dir.path.display(), + err = err + ); + Ok(HttpResponse::Ok() + .status(http::StatusCode::INTERNAL_SERVER_ERROR) + .body("")) + } + } + } else { + Ok(HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body( + renderer::page( + &title, + entries, + is_root, + page_parent, + sort_method, + sort_order, + ) + .into_string(), + )) + } } -- cgit v1.2.3 From eec5f353ffe584fcdcd7e43670fbd30d9e9f04e3 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 22:23:33 +0100 Subject: Refactored some code --- src/listing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 565b5bf..c9542b8 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -229,9 +229,9 @@ pub fn directory_listing( if let Some(compression_method) = &download { match archive::create_archive_file(&compression_method, &dir.path) { - Ok((filename, content_length, content)) => Ok(HttpResponse::Ok() + Ok((filename, content)) => Ok(HttpResponse::Ok() .content_type("application/tar") - .content_length(content_length as u64) + .content_length(content.len() as u64) .header("Content-Transfer-Encoding", "binary") .header( "Content-Disposition", -- cgit v1.2.3 From 46932e7d5664c97ad65aefc3f670f7e64e6f8e0d Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 8 Mar 2019 23:57:05 +0100 Subject: Improved HTTP headers --- src/listing.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index c9542b8..f7198a5 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -230,8 +230,9 @@ pub fn directory_listing( if let Some(compression_method) = &download { match archive::create_archive_file(&compression_method, &dir.path) { Ok((filename, content)) => Ok(HttpResponse::Ok() - .content_type("application/tar") + .content_type(compression_method.content_type()) .content_length(content.len() as u64) + .content_encoding(compression_method.content_encoding()) .header("Content-Transfer-Encoding", "binary") .header( "Content-Disposition", -- cgit v1.2.3 From 5cde963bb25c44695a8e72432c739416cbaa1014 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sat, 9 Mar 2019 11:45:34 +0100 Subject: Removed debug message --- src/listing.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index f7198a5..88dab40 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -240,12 +240,11 @@ pub fn directory_listing( ) .chunked() .body(Body::Streaming(Box::new(once(Ok(content)))))), - Err(err) => { + Err(_) => { println!( - "{error} an error occured while compressing {folder}: {err:?}", + "{error} an error occured while compressing {folder}", error = Paint::red("error:").bold(), folder = dir.path.display(), - err = err ); Ok(HttpResponse::Ok() .status(http::StatusCode::INTERNAL_SERVER_ERROR) -- cgit v1.2.3 From aeb51dcf43665741a3438360151a4424e9b243e0 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 00:25:56 +0100 Subject: Started to add helpful messages for errors which could occur during archiving --- src/listing.rs | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 88dab40..ab3b28b 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -7,9 +7,10 @@ use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; -use yansi::Paint; +use yansi::{Color, Paint}; use crate::archive; +use crate::errors; use crate::renderer; /// Query parameters @@ -228,24 +229,37 @@ pub fn directory_listing( } if let Some(compression_method) = &download { + println!( + "{info} Creating an archive ({extension}) of {path}...", + info = Color::Blue.paint("info:").bold(), + extension = Color::White.paint(compression_method.extension()).bold(), + path = Color::White.paint(&dir.path.display().to_string()).bold() + ); match archive::create_archive_file(&compression_method, &dir.path) { - Ok((filename, content)) => Ok(HttpResponse::Ok() - .content_type(compression_method.content_type()) - .content_length(content.len() as u64) - .content_encoding(compression_method.content_encoding()) - .header("Content-Transfer-Encoding", "binary") - .header( - "Content-Disposition", - format!("attachment; filename={:?}", filename), - ) - .chunked() - .body(Body::Streaming(Box::new(once(Ok(content)))))), - Err(_) => { + Ok((filename, content)) => { + println!( + "{success} Archive successfully created !", + success = Color::Green.paint("success:").bold() + ); + Ok(HttpResponse::Ok() + .content_type(compression_method.content_type()) + .content_length(content.len() as u64) + .content_encoding(compression_method.content_encoding()) + .header("Content-Transfer-Encoding", "binary") + .header( + "Content-Disposition", + format!("attachment; filename={:?}", filename), + ) + .chunked() + .body(Body::Streaming(Box::new(once(Ok(content)))))) + } + Err(err) => { println!( - "{error} an error occured while compressing {folder}", + "{error} {err}", error = Paint::red("error:").bold(), - folder = dir.path.display(), + err = Paint::white(&err).bold() ); + errors::print_chain(err); Ok(HttpResponse::Ok() .status(http::StatusCode::INTERNAL_SERVER_ERROR) .body("")) -- cgit v1.2.3 From 67d771fc3a954ea439e77afac092c84c5489d074 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 19:23:22 +0100 Subject: Added some error messages + reworked the print_error_chain method --- src/listing.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index ab3b28b..a9f0f19 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -7,7 +7,7 @@ use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; -use yansi::{Color, Paint}; +use yansi::Color; use crate::archive; use crate::errors; @@ -238,8 +238,9 @@ pub fn directory_listing( match archive::create_archive_file(&compression_method, &dir.path) { Ok((filename, content)) => { println!( - "{success} Archive successfully created !", - success = Color::Green.paint("success:").bold() + "{success} {file} successfully created !", + success = Color::Green.paint("success:").bold(), + file = Color::White.paint(&filename).bold(), ); Ok(HttpResponse::Ok() .content_type(compression_method.content_type()) @@ -254,12 +255,7 @@ pub fn directory_listing( .body(Body::Streaming(Box::new(once(Ok(content)))))) } Err(err) => { - println!( - "{error} {err}", - error = Paint::red("error:").bold(), - err = Paint::white(&err).bold() - ); - errors::print_chain(err); + errors::print_error_chain(err); Ok(HttpResponse::Ok() .status(http::StatusCode::INTERNAL_SERVER_ERROR) .body("")) -- cgit v1.2.3 From 17ef61a126e81c9ecfd1ebdd89537e854a06cae6 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 13 Mar 2019 19:30:54 +0100 Subject: Switched to standard Rust logging system --- src/listing.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index a9f0f19..b820aa4 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -7,7 +7,6 @@ use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; -use yansi::Color; use crate::archive; use crate::errors; @@ -229,19 +228,14 @@ pub fn directory_listing( } if let Some(compression_method) = &download { - println!( - "{info} Creating an archive ({extension}) of {path}...", - info = Color::Blue.paint("info:").bold(), - extension = Color::White.paint(compression_method.extension()).bold(), - path = Color::White.paint(&dir.path.display().to_string()).bold() + log::info!( + "Creating an archive ({extension}) of {path}...", + extension = compression_method.extension(), + path = &dir.path.display().to_string() ); match archive::create_archive_file(&compression_method, &dir.path) { Ok((filename, content)) => { - println!( - "{success} {file} successfully created !", - success = Color::Green.paint("success:").bold(), - file = Color::White.paint(&filename).bold(), - ); + log::info!("{file} successfully created !", file = &filename); Ok(HttpResponse::Ok() .content_type(compression_method.content_type()) .content_length(content.len() as u64) -- cgit v1.2.3 From 20283096380f86595c959de8d5f045437e84c964 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Thu, 14 Mar 2019 20:30:06 +0100 Subject: Switched to tar-rs 0.4.22 and propagate no-symlink argument to tar generation --- 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 b820aa4..a243c22 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -233,7 +233,7 @@ pub fn directory_listing( extension = compression_method.extension(), path = &dir.path.display().to_string() ); - match archive::create_archive_file(&compression_method, &dir.path) { + match archive::create_archive_file(&compression_method, &dir.path, skip_symlinks) { Ok((filename, content)) => { log::info!("{file} successfully created !", file = &filename); Ok(HttpResponse::Ok() -- cgit v1.2.3 From bc293fa54a612ff7e2d6088ba5d91890cad75b81 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Fri, 15 Mar 2019 17:50:59 +0100 Subject: Renamed create_archive_file function and added documentation --- 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 a243c22..ed3b63d 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -233,7 +233,7 @@ pub fn directory_listing( extension = compression_method.extension(), path = &dir.path.display().to_string() ); - match archive::create_archive_file(&compression_method, &dir.path, skip_symlinks) { + match archive::create_archive(&compression_method, &dir.path, skip_symlinks) { Ok((filename, content)) => { log::info!("{file} successfully created !", file = &filename); Ok(HttpResponse::Ok() -- cgit v1.2.3 From 6a3db431ef0eb0d235bf5ae319076b2ce229c583 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 19 Mar 2019 20:08:26 +0100 Subject: Removed Content-Length --- src/listing.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index ed3b63d..c4daf88 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -238,7 +238,6 @@ pub fn directory_listing( log::info!("{file} successfully created !", file = &filename); Ok(HttpResponse::Ok() .content_type(compression_method.content_type()) - .content_length(content.len() as u64) .content_encoding(compression_method.content_encoding()) .header("Content-Transfer-Encoding", "binary") .header( -- cgit v1.2.3