From dd1684f1aa4066805001b4b02eb7619ad1b6fe7d Mon Sep 17 00:00:00 2001 From: marawan ragab Date: Sun, 3 May 2020 19:26:21 -0400 Subject: Add zip download functionality for windows users --- src/archive.rs | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) (limited to 'src') diff --git a/src/archive.rs b/src/archive.rs index 268bb47..6808907 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,9 +1,14 @@ use actix_web::http::ContentEncoding; use libflate::gzip::Encoder; +use zip::{ZipWriter, write}; +use std::io::BufWriter; +use std::fs::File; +use std::path::PathBuf; use serde::Deserialize; use std::path::Path; use strum_macros::{Display, EnumIter, EnumString}; use tar::Builder; +use std::io::{Cursor, Write, Read}; use crate::errors::ContextualError; @@ -17,6 +22,9 @@ pub enum CompressionMethod { /// Regular tarball Tar, + + /// Regular zip + Zip, } impl CompressionMethod { @@ -24,6 +32,7 @@ impl CompressionMethod { match self { CompressionMethod::TarGz => "tar.gz", CompressionMethod::Tar => "tar", + CompressionMethod::Zip => "zip", } .to_string() } @@ -32,6 +41,7 @@ impl CompressionMethod { match self { CompressionMethod::TarGz => "application/gzip", CompressionMethod::Tar => "application/tar", + CompressionMethod::Zip => "application/zip", } .to_string() } @@ -40,6 +50,7 @@ impl CompressionMethod { match self { CompressionMethod::TarGz => ContentEncoding::Gzip, CompressionMethod::Tar => ContentEncoding::Identity, + CompressionMethod::Zip => ContentEncoding::Identity, } } @@ -62,6 +73,7 @@ impl CompressionMethod { match self { CompressionMethod::TarGz => tar_gz(dir, skip_symlinks, out), CompressionMethod::Tar => tar_dir(dir, skip_symlinks, out), + CompressionMethod::Zip => zip_dir(dir, skip_symlinks, out), } } } @@ -159,3 +171,142 @@ where Ok(()) } + +/// Write a zip of `dir` in `out`. +/// +/// The target directory will be saved as a top-level directory in the archive. +/// +/// For example, consider this directory structure: +/// +/// ``` +/// a +/// └── b +/// └── c +/// ├── e +/// ├── f +/// └── g +/// ``` +/// +/// Making a zip out of `"a/b/c"` will result in this archive content: +/// +/// ``` +/// c +/// ├── e +/// ├── f +/// └── g +/// ``` +fn create_zip_from_directory(out: W, directory: &PathBuf, skip_symlinks: bool,) -> Result<(), ContextualError> +where + W: std::io::Write + std::io::Seek +{ + let options = write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); + let mut paths_queue: Vec = vec![]; + paths_queue.push(directory.clone()); + let zip_root_folder_name = directory.file_name().ok_or_else(|| { + ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + })?; + + let mut zip_writer = ZipWriter::new(out); + let mut buffer = Vec::new(); + while paths_queue.len() > 0 { + let next = paths_queue.pop().ok_or(ContextualError::CustomError("Could not get path from queue".to_string()))?; + let current_dir = next.as_path(); + let directory_entry_iterator = std::fs::read_dir(current_dir).map_err(|e|{ + ContextualError::IOError("Could not read directory".to_string(), e) + })?; + let zip_directory = Path::new(zip_root_folder_name) + .join(current_dir.strip_prefix(directory).map_err(|_|{ + ContextualError::CustomError("Could not append base directory".to_string()) + })?); + + for entry in directory_entry_iterator { + let entry_path = entry.ok().ok_or( + ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + )?.path(); + let entry_metadata = std::fs::metadata(entry_path.clone()).map_err(|e|{ + ContextualError::IOError("Could not get file metadata".to_string(), e) + })?; + + if entry_metadata.file_type().is_symlink() && skip_symlinks { + continue; + } + let current_entry_name = entry_path.file_name().ok_or_else(|| { + ContextualError::InvalidPathError("Invalid file or direcotory name".to_string()) + })?; + if entry_metadata.is_file() { + let mut f = File::open(&entry_path).map_err(|e| { + ContextualError::IOError("Could not open file".to_string(), e) + })?; + f.read_to_end(&mut buffer).map_err(|e| { + ContextualError::IOError("Could not read from file".to_string(), e) + })?; + let relative_path = zip_directory.join(current_entry_name); + zip_writer.start_file_from_path(Path::new(&relative_path), options).map_err(|_| { + ContextualError::CustomError("Could not add file path to ZIP".to_string()) + })?; + zip_writer.write(buffer.as_ref()).map_err(|_| { + ContextualError::CustomError("Could not write file to ZIP".to_string()) + })?; + buffer.clear(); + } else if entry_metadata.is_dir() { + let relative_path = zip_directory.join(current_entry_name); + zip_writer.add_directory_from_path(Path::new(&relative_path), options).map_err(|_| { + ContextualError::CustomError("Could not add directory path to ZIP".to_string()) + })?; + paths_queue.push(entry_path.clone()); + } + } + } + + zip_writer.finish().unwrap(); + Ok(()) +} + +/// Writes a zip of `dir` in `out`. +/// +/// The content of `src_dir` will be saved in the archive as the folder named . +fn zip_data( + src_dir: &Path, + skip_symlinks: bool, + out: W, +) -> Result<(), ContextualError> +where + W: std::io::Write, +{ + let mut data = Vec::new(); + { + let memory_file = Cursor::new(&mut data); + create_zip_from_directory(memory_file, &src_dir.to_path_buf(), skip_symlinks).map_err(|e| { + ContextualError::ArchiveCreationError("Failed to create the ZIP archive".to_string(), Box::new(e)) + })?; + } + + let mut buffer = BufWriter::new(out); + buffer.write_all(&mut data).map_err(|e| { + ContextualError::IOError("Failed to write the ZIP archive".to_string(), e) + })?; + + buffer.flush().map_err(|e| { + ContextualError::IOError("Failed to finish writing the ZIP archive".to_string(), e) + })?; + + Ok(()) +} + +fn zip_dir(dir: &Path, skip_symlinks: bool, out: W) -> Result<(), ContextualError> +where + W: std::io::Write, +{ + let inner_folder = dir.file_name().ok_or_else(|| { + ContextualError::InvalidPathError("Directory name terminates in \"..\"".to_string()) + })?; + + inner_folder.to_str().ok_or_else(|| { + ContextualError::InvalidPathError( + "Directory name contains invalid UTF-8 characters".to_string(), + ) + })?; + + zip_data(dir, skip_symlinks, out) + .map_err(|e| ContextualError::ArchiveCreationError("zip".to_string(), Box::new(e))) +} \ No newline at end of file -- cgit v1.2.3 From 3290edfbfe4c628553663243cbd439d9164c9029 Mon Sep 17 00:00:00 2001 From: marawan ragab Date: Fri, 8 May 2020 19:52:19 -0400 Subject: small improvment to buffer usage --- src/archive.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/archive.rs b/src/archive.rs index 6808907..785e1e8 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -268,26 +268,19 @@ where fn zip_data( src_dir: &Path, skip_symlinks: bool, - out: W, + mut out: W, ) -> Result<(), ContextualError> where W: std::io::Write, { let mut data = Vec::new(); - { - let memory_file = Cursor::new(&mut data); - create_zip_from_directory(memory_file, &src_dir.to_path_buf(), skip_symlinks).map_err(|e| { - ContextualError::ArchiveCreationError("Failed to create the ZIP archive".to_string(), Box::new(e)) - })?; - } - - let mut buffer = BufWriter::new(out); - buffer.write_all(&mut data).map_err(|e| { - ContextualError::IOError("Failed to write the ZIP archive".to_string(), e) + let memory_file = Cursor::new(&mut data); + create_zip_from_directory(memory_file, &src_dir.to_path_buf(), skip_symlinks).map_err(|e| { + ContextualError::ArchiveCreationError("Failed to create the ZIP archive".to_string(), Box::new(e)) })?; - buffer.flush().map_err(|e| { - ContextualError::IOError("Failed to finish writing the ZIP archive".to_string(), e) + out.write_all(data.as_mut_slice()).map_err(|e| { + ContextualError::IOError("Failed to write the ZIP archive".to_string(), e) })?; Ok(()) -- cgit v1.2.3 From 848557762c1c4eed2ba16713ead97308d1841220 Mon Sep 17 00:00:00 2001 From: marawan ragab Date: Sun, 10 May 2020 17:14:51 -0400 Subject: make sure archiving is opt-in --- src/archive.rs | 13 +++++++++++-- src/args.rs | 15 +++++++++++---- src/listing.rs | 8 +++++--- src/main.rs | 13 +++++++++---- src/renderer.rs | 9 ++++++--- 5 files changed, 42 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/archive.rs b/src/archive.rs index 785e1e8..c96814f 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,7 +1,6 @@ use actix_web::http::ContentEncoding; use libflate::gzip::Encoder; use zip::{ZipWriter, write}; -use std::io::BufWriter; use std::fs::File; use std::path::PathBuf; use serde::Deserialize; @@ -54,6 +53,14 @@ impl CompressionMethod { } } + pub fn is_enabled(self, tar_enabled: bool, zip_enabled: bool,) -> bool { + match self { + CompressionMethod::TarGz => tar_enabled, + CompressionMethod::Tar => tar_enabled, + CompressionMethod::Zip => zip_enabled, + } + } + /// Make an archive out of the given directory, and write the output to the given writer. /// /// Recursively includes all files and subdirectories. @@ -258,7 +265,9 @@ where } } - zip_writer.finish().unwrap(); + zip_writer.finish().map_err(|_| { + ContextualError::CustomError("Could not finish writing ZIP archive".to_string()) + })?; Ok(()) } diff --git a/src/args.rs b/src/args.rs index 49fe276..1525b41 100644 --- a/src/args.rs +++ b/src/args.rs @@ -84,9 +84,15 @@ struct CLIArgs { #[structopt(short = "o", long = "overwrite-files")] overwrite_files: bool, - /// Disable archive generation - #[structopt(short = "r", long = "disable-archives")] - disable_archives: bool, + /// Enable tar archive generation + #[structopt(short = "r", long = "tar-enabled")] + tar_enabled: bool, + + /// Enable zip archive generation + /// Zipping large directories can result in out-of-memory exception + /// because zip generation is done in memory and cannot be sent on the fly + #[structopt(short = "z", long = "zip-enabled")] + zip_enabled: bool, } /// Checks wether an interface is valid, i.e. it can be parsed into an IP address @@ -176,7 +182,8 @@ pub fn parse_args() -> crate::MiniserveConfig { index: args.index, overwrite_files: args.overwrite_files, file_upload: args.file_upload, - archives: !args.disable_archives, + tar_enabled: args.tar_enabled, + zip_enabled: args.zip_enabled, } } diff --git a/src/listing.rs b/src/listing.rs index d28824c..45b3732 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -136,7 +136,8 @@ pub fn directory_listing( random_route: Option, default_color_scheme: ColorScheme, upload_route: String, - archives_enabled: bool, + tar_enabled: bool, + zip_enabled: bool, ) -> Result { let serve_path = req.path(); @@ -250,7 +251,7 @@ pub fn directory_listing( let color_scheme = query_params.theme.unwrap_or(default_color_scheme); if let Some(compression_method) = query_params.download { - if !archives_enabled { + if !compression_method.is_enabled(tar_enabled, zip_enabled) { return Ok(HttpResponse::Forbidden() .content_type("text/html; charset=utf-8") .body( @@ -332,7 +333,8 @@ pub fn directory_listing( file_upload, &upload_route, ¤t_dir.display().to_string(), - archives_enabled, + tar_enabled, + zip_enabled, ) .into_string(), )) diff --git a/src/main.rs b/src/main.rs index a1ee303..3ff35c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,8 +63,11 @@ pub struct MiniserveConfig { /// Enable upload to override existing files pub overwrite_files: bool, - /// If false, creation of archives is disabled - pub archives: bool, + /// If false, creation of tar archives is disabled + pub tar_enabled: bool, + + /// If false, creation of zip archives is disabled + pub zip_enabled: bool, } fn main() { @@ -256,7 +259,8 @@ fn configure_app(app: App) -> App { let random_route = app.state().random_route.clone(); let default_color_scheme = app.state().default_color_scheme; let file_upload = app.state().file_upload; - let archives_enabled = app.state().archives; + let tar_enabled = app.state().tar_enabled; + let zip_enabled = app.state().zip_enabled; upload_route = if let Some(random_route) = app.state().random_route.clone() { format!("/{}/upload", random_route) } else { @@ -285,7 +289,8 @@ fn configure_app(app: App) -> App { random_route.clone(), default_color_scheme, u_r.clone(), - archives_enabled, + tar_enabled, + zip_enabled, ) }) .default_handler(error_404), diff --git a/src/renderer.rs b/src/renderer.rs index face6ff..accb49b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -22,7 +22,8 @@ pub fn page( file_upload: bool, upload_route: &str, current_dir: &str, - archives: bool, + tar_enabled: bool, + zip_enabled: bool, ) -> Markup { let upload_action = build_upload_action( upload_route, @@ -50,10 +51,12 @@ pub fn page( span#top { } h1.title { "Index of " (serve_path) } div.toolbar { - @if archives { + @if tar_enabled || zip_enabled { div.download { @for compression_method in CompressionMethod::iter() { - (archive_button(compression_method, sort_method, sort_order, color_scheme, default_color_scheme)) + @if compression_method.is_enabled(tar_enabled, zip_enabled) { + (archive_button(compression_method, sort_method, sort_order, color_scheme, default_color_scheme)) + } } } } -- cgit v1.2.3 From 97cf52b64edf23a6b5391569219eeebca86d52a0 Mon Sep 17 00:00:00 2001 From: marawan ragab Date: Sun, 10 May 2020 17:34:22 -0400 Subject: rename variables correctly --- src/args.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 1525b41..ea4e90f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -85,14 +85,14 @@ struct CLIArgs { overwrite_files: bool, /// Enable tar archive generation - #[structopt(short = "r", long = "tar-enabled")] - tar_enabled: bool, + #[structopt(short = "r", long = "enable-tar")] + enable_tar: bool, /// Enable zip archive generation /// Zipping large directories can result in out-of-memory exception /// because zip generation is done in memory and cannot be sent on the fly - #[structopt(short = "z", long = "zip-enabled")] - zip_enabled: bool, + #[structopt(short = "z", long = "enable-zip")] + enable_zip: bool, } /// Checks wether an interface is valid, i.e. it can be parsed into an IP address @@ -182,8 +182,8 @@ pub fn parse_args() -> crate::MiniserveConfig { index: args.index, overwrite_files: args.overwrite_files, file_upload: args.file_upload, - tar_enabled: args.tar_enabled, - zip_enabled: args.zip_enabled, + tar_enabled: args.enable_tar, + zip_enabled: args.enable_zip, } } -- cgit v1.2.3