diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/archive.rs | 46 | ||||
-rw-r--r-- | src/listing.rs | 4 | ||||
-rw-r--r-- | src/themes.rs | 421 |
3 files changed, 236 insertions, 235 deletions
diff --git a/src/archive.rs b/src/archive.rs index 02300c5..b62c3bd 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -3,7 +3,7 @@ use bytes::Bytes; use libflate::gzip::Encoder; use serde::Deserialize; use std::io; -use std::path::PathBuf; +use std::path::Path; use strum_macros::{Display, EnumIter, EnumString}; use tar::Builder; @@ -38,32 +38,34 @@ impl CompressionMethod { CompressionMethod::TarGz => ContentEncoding::Gzip, } } -} - -/// Creates an archive of a folder, using the algorithm the user chose from the web interface -/// This method returns the archive as a stream of bytes -pub fn create_archive( - method: &CompressionMethod, - dir: &PathBuf, - skip_symlinks: bool, -) -> Result<(String, Bytes), ContextualError> { - match method { - CompressionMethod::TarGz => tgz_compress(&dir, skip_symlinks), + /// Creates an archive of a folder, using the algorithm the user chose from the web interface + /// This method returns the archive as a stream of bytes + pub fn create_archive<T: AsRef<Path>>( + &self, + dir: T, + skip_symlinks: bool, + ) -> Result<(String, Bytes), ContextualError> { + match self { + CompressionMethod::TarGz => tgz_compress(dir, skip_symlinks), + } } } /// Compresses a given folder in .tar.gz format, and returns the result as a stream of bytes -fn tgz_compress(dir: &PathBuf, skip_symlinks: bool) -> Result<(String, Bytes), ContextualError> { - let src_dir = dir.display().to_string(); - if let Some(inner_folder) = dir.file_name() { +fn tgz_compress<T: AsRef<Path>>( + dir: T, + skip_symlinks: bool, +) -> Result<(String, Bytes), ContextualError> { + if let Some(inner_folder) = dir.as_ref().file_name() { if let Some(directory) = inner_folder.to_str() { let dst_filename = format!("{}.tar", directory); let dst_tgz_filename = format!("{}.gz", dst_filename); let mut tgz_data = Bytes::new(); - let tar_data = tar(src_dir, directory.to_string(), skip_symlinks).map_err(|e| { - ContextualError::ArchiveCreationError("tarball".to_string(), Box::new(e)) - })?; + let tar_data = + tar(dir.as_ref(), directory.to_string(), skip_symlinks).map_err(|e| { + ContextualError::ArchiveCreationError("tarball".to_string(), Box::new(e)) + })?; let gz_data = gzip(&tar_data).map_err(|e| { ContextualError::ArchiveCreationError("GZIP archive".to_string(), Box::new(e)) @@ -87,8 +89,8 @@ fn tgz_compress(dir: &PathBuf, skip_symlinks: bool) -> Result<(String, Bytes), C } /// Creates a TAR archive of a folder, and returns it as a stream of bytes -fn tar( - src_dir: String, +fn tar<T: AsRef<Path>>( + src_dir: T, inner_folder: String, skip_symlinks: bool, ) -> Result<Vec<u8>, ContextualError> { @@ -97,12 +99,12 @@ fn tar( tar_builder.follow_symlinks(!skip_symlinks); // Recursively adds the content of src_dir into the archive stream tar_builder - .append_dir_all(inner_folder, &src_dir) + .append_dir_all(inner_folder, src_dir.as_ref()) .map_err(|e| { ContextualError::IOError( format!( "Failed to append the content of {} to the TAR archive", - &src_dir + src_dir.as_ref().to_str().unwrap_or("file") ), e, ) diff --git a/src/listing.rs b/src/listing.rs index 49802bc..2ffcf2f 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; use std::time::SystemTime; use strum_macros::{Display, EnumString}; -use crate::archive::{self, CompressionMethod}; +use crate::archive::CompressionMethod; use crate::errors::{self, ContextualError}; use crate::renderer; use crate::themes::ColorScheme; @@ -241,7 +241,7 @@ pub fn directory_listing<S>( extension = compression_method.extension(), path = &dir.path.display().to_string() ); - match archive::create_archive(&compression_method, &dir.path, skip_symlinks) { + match compression_method.create_archive(&dir.path, skip_symlinks) { Ok((filename, content)) => { log::info!("{file} successfully created !", file = &filename); Ok(HttpResponse::Ok() diff --git a/src/themes.rs b/src/themes.rs index 65e9ab2..12ae7e3 100644 --- a/src/themes.rs +++ b/src/themes.rs @@ -2,6 +2,51 @@ use serde::Deserialize; use structopt::clap::arg_enum; use strum_macros::EnumIter; +/// Describes a theme +pub struct Theme { + pub background: &'static str, + pub text_color: &'static str, + pub directory_link_color: &'static str, + pub file_link_color: &'static str, + pub symlink_link_color: &'static str, + pub table_background: &'static str, + pub table_text_color: &'static str, + pub table_header_background: &'static str, + pub table_header_text_color: &'static str, + pub table_header_active_color: &'static str, + pub active_row_color: &'static str, + pub odd_row_background: &'static str, + pub even_row_background: &'static str, + pub root_link_color: &'static str, + pub download_button_background: &'static str, + pub download_button_background_hover: &'static str, + pub download_button_link_color: &'static str, + pub download_button_link_color_hover: &'static str, + pub back_button_background: &'static str, + pub back_button_background_hover: &'static str, + pub back_button_link_color: &'static str, + pub back_button_link_color_hover: &'static str, + pub date_text_color: &'static str, + pub at_color: &'static str, + pub switch_theme_background: &'static str, + pub switch_theme_link_color: &'static str, + pub switch_theme_active: &'static str, + pub switch_theme_border: &'static str, + pub change_theme_link_color: &'static str, + pub change_theme_link_color_hover: &'static str, + pub upload_text_color: &'static str, + pub upload_form_border_color: &'static str, + pub upload_form_background: &'static str, + pub upload_button_background: &'static str, + pub upload_button_text_color: &'static str, + pub drag_background: &'static str, + pub drag_border_color: &'static str, + pub drag_text_color: &'static str, + pub size_background_color: &'static str, + pub size_text_color: &'static str, + pub error_color: &'static str, +} + arg_enum! { #[derive(PartialEq, Deserialize, Clone, EnumIter, Copy)] #[serde(rename_all = "lowercase")] @@ -17,14 +62,13 @@ impl ColorScheme { /// Returns the URL-compatible name of a color scheme /// This must correspond to the name of the variant, in lowercase /// See https://github.com/svenstaro/miniserve/pull/55 for explanations - pub fn to_slug(self) -> String { + pub fn to_slug(self) -> &'static str { match self { ColorScheme::Archlinux => "archlinux", ColorScheme::Zenburn => "zenburn", ColorScheme::Monokai => "monokai", ColorScheme::Squirrel => "squirrel", } - .to_string() } /// Returns wether a color scheme is dark @@ -41,222 +85,177 @@ impl ColorScheme { pub fn get_theme(self) -> Theme { match self { ColorScheme::Archlinux => Theme { - background: "#383c4a".to_string(), - text_color: "#fefefe".to_string(), - directory_link_color: "#03a9f4".to_string(), - file_link_color: "#ea95ff".to_string(), - symlink_link_color: "#ff9800".to_string(), - table_background: "#353946".to_string(), - table_text_color: "#eeeeee".to_string(), - table_header_background: "#5294e2".to_string(), - table_header_text_color: "#eeeeee".to_string(), - table_header_active_color: "#ffffff".to_string(), - active_row_color: "#5194e259".to_string(), - odd_row_background: "#404552".to_string(), - even_row_background: "#4b5162".to_string(), - root_link_color: "#abb2bb".to_string(), - download_button_background: "#ea95ff".to_string(), - download_button_background_hover: "#eea7ff".to_string(), - download_button_link_color: "#ffffff".to_string(), - download_button_link_color_hover: "#ffffff".to_string(), - back_button_background: "#ea95ff".to_string(), - back_button_background_hover: "#ea95ff".to_string(), - back_button_link_color: "#ffffff".to_string(), - back_button_link_color_hover: "#ffffff".to_string(), - date_text_color: "#9ebbdc".to_string(), - at_color: "#9ebbdc".to_string(), - switch_theme_background: "#4b5162".to_string(), - switch_theme_link_color: "#fefefe".to_string(), - switch_theme_active: "#ea95ff".to_string(), - switch_theme_border: "#6a728a".to_string(), - change_theme_link_color: "#fefefe".to_string(), - change_theme_link_color_hover: "#fefefe".to_string(), - upload_text_color: "#fefefe".to_string(), - upload_form_border_color: "#353946".to_string(), - upload_form_background: "#4b5162".to_string(), - upload_button_background: "#ea95ff".to_string(), - upload_button_text_color: "#ffffff".to_string(), - drag_background: "#3333338f".to_string(), - drag_border_color: "#fefefe".to_string(), - drag_text_color: "#fefefe".to_string(), - size_background_color: "#5294e2".to_string(), - size_text_color: "#fefefe".to_string(), - error_color: "#e44b4b".to_string(), + background: "#383c4a", + text_color: "#fefefe", + directory_link_color: "#03a9f4", + file_link_color: "#ea95ff", + symlink_link_color: "#ff9800", + table_background: "#353946", + table_text_color: "#eeeeee", + table_header_background: "#5294e2", + table_header_text_color: "#eeeeee", + table_header_active_color: "#ffffff", + active_row_color: "#5194e259", + odd_row_background: "#404552", + even_row_background: "#4b5162", + root_link_color: "#abb2bb", + download_button_background: "#ea95ff", + download_button_background_hover: "#eea7ff", + download_button_link_color: "#ffffff", + download_button_link_color_hover: "#ffffff", + back_button_background: "#ea95ff", + back_button_background_hover: "#ea95ff", + back_button_link_color: "#ffffff", + back_button_link_color_hover: "#ffffff", + date_text_color: "#9ebbdc", + at_color: "#9ebbdc", + switch_theme_background: "#4b5162", + switch_theme_link_color: "#fefefe", + switch_theme_active: "#ea95ff", + switch_theme_border: "#6a728a", + change_theme_link_color: "#fefefe", + change_theme_link_color_hover: "#fefefe", + upload_text_color: "#fefefe", + upload_form_border_color: "#353946", + upload_form_background: "#4b5162", + upload_button_background: "#ea95ff", + upload_button_text_color: "#ffffff", + drag_background: "#3333338f", + drag_border_color: "#fefefe", + drag_text_color: "#fefefe", + size_background_color: "#5294e2", + size_text_color: "#fefefe", + error_color: "#e44b4b", }, ColorScheme::Zenburn => Theme { - background: "#3f3f3f".to_string(), - text_color: "#efefef".to_string(), - directory_link_color: "#f0dfaf".to_string(), - file_link_color: "#87D6D5".to_string(), - symlink_link_color: "#FFCCEE".to_string(), - table_background: "#4a4949".to_string(), - table_text_color: "#efefef".to_string(), - table_header_background: "#7f9f7f".to_string(), - table_header_text_color: "#efefef".to_string(), - table_header_active_color: "#efef8f".to_string(), - active_row_color: "#7e9f7f9c".to_string(), - odd_row_background: "#777777".to_string(), - even_row_background: "#5a5a5a".to_string(), - root_link_color: "#dca3a3".to_string(), - download_button_background: "#cc9393".to_string(), - download_button_background_hover: "#dca3a3".to_string(), - download_button_link_color: "#efefef".to_string(), - download_button_link_color_hover: "#efefef".to_string(), - back_button_background: "#cc9393".to_string(), - back_button_background_hover: "#cc9393".to_string(), - back_button_link_color: "#efefef".to_string(), - back_button_link_color_hover: "#efefef".to_string(), - date_text_color: "#cfbfaf".to_string(), - at_color: "#cfbfaf".to_string(), - switch_theme_background: "#4a4949".to_string(), - switch_theme_link_color: "#efefef".to_string(), - switch_theme_active: "#efef8f".to_string(), - switch_theme_border: "#5a5a5a".to_string(), - change_theme_link_color: "#efefef".to_string(), - change_theme_link_color_hover: "#efefef".to_string(), - upload_text_color: "#efefef".to_string(), - upload_form_border_color: "#4a4949".to_string(), - upload_form_background: "#777777".to_string(), - upload_button_background: "#cc9393".to_string(), - upload_button_text_color: "#efefef".to_string(), - drag_background: "#3333338f".to_string(), - drag_border_color: "#efefef".to_string(), - drag_text_color: "#efefef".to_string(), - size_background_color: "#7f9f7f".to_string(), - size_text_color: "#efefef".to_string(), - error_color: "#d06565".to_string(), + background: "#3f3f3f", + text_color: "#efefef", + directory_link_color: "#f0dfaf", + file_link_color: "#87D6D5", + symlink_link_color: "#FFCCEE", + table_background: "#4a4949", + table_text_color: "#efefef", + table_header_background: "#7f9f7f", + table_header_text_color: "#efefef", + table_header_active_color: "#efef8f", + active_row_color: "#7e9f7f9c", + odd_row_background: "#777777", + even_row_background: "#5a5a5a", + root_link_color: "#dca3a3", + download_button_background: "#cc9393", + download_button_background_hover: "#dca3a3", + download_button_link_color: "#efefef", + download_button_link_color_hover: "#efefef", + back_button_background: "#cc9393", + back_button_background_hover: "#cc9393", + back_button_link_color: "#efefef", + back_button_link_color_hover: "#efefef", + date_text_color: "#cfbfaf", + at_color: "#cfbfaf", + switch_theme_background: "#4a4949", + switch_theme_link_color: "#efefef", + switch_theme_active: "#efef8f", + switch_theme_border: "#5a5a5a", + change_theme_link_color: "#efefef", + change_theme_link_color_hover: "#efefef", + upload_text_color: "#efefef", + upload_form_border_color: "#4a4949", + upload_form_background: "#777777", + upload_button_background: "#cc9393", + upload_button_text_color: "#efefef", + drag_background: "#3333338f", + drag_border_color: "#efefef", + drag_text_color: "#efefef", + size_background_color: "#7f9f7f", + size_text_color: "#efefef", + error_color: "#d06565", }, ColorScheme::Monokai => Theme { - background: "#272822".to_string(), - text_color: "#F8F8F2".to_string(), - directory_link_color: "#F92672".to_string(), - file_link_color: "#A6E22E".to_string(), - symlink_link_color: "#FD971F".to_string(), - table_background: "#3B3A32".to_string(), - table_text_color: "#F8F8F0".to_string(), - table_header_background: "#75715E".to_string(), - table_header_text_color: "#F8F8F2".to_string(), - table_header_active_color: "#E6DB74".to_string(), - active_row_color: "#ae81fe3d".to_string(), - odd_row_background: "#3E3D32".to_string(), - even_row_background: "#49483E".to_string(), - root_link_color: "#66D9EF".to_string(), - download_button_background: "#AE81FF".to_string(), - download_button_background_hover: "#c6a6ff".to_string(), - download_button_link_color: "#F8F8F0".to_string(), - download_button_link_color_hover: "#F8F8F0".to_string(), - back_button_background: "#AE81FF".to_string(), - back_button_background_hover: "#AE81FF".to_string(), - back_button_link_color: "#F8F8F0".to_string(), - back_button_link_color_hover: "#F8F8F0".to_string(), - date_text_color: "#66D9EF".to_string(), - at_color: "#66D9EF".to_string(), - switch_theme_background: "#3B3A32".to_string(), - switch_theme_link_color: "#F8F8F2".to_string(), - switch_theme_active: "#A6E22E".to_string(), - switch_theme_border: "#49483E".to_string(), - change_theme_link_color: "#F8F8F2".to_string(), - change_theme_link_color_hover: "#F8F8F2".to_string(), - upload_text_color: "#F8F8F2".to_string(), - upload_form_border_color: "#3B3A32".to_string(), - upload_form_background: "#49483E".to_string(), - upload_button_background: "#AE81FF".to_string(), - upload_button_text_color: "#F8F8F0".to_string(), - drag_background: "#3333338f".to_string(), - drag_border_color: "#F8F8F2".to_string(), - drag_text_color: "#F8F8F2".to_string(), - size_background_color: "#75715E".to_string(), - size_text_color: "#F8F8F2".to_string(), - error_color: "#d02929".to_string(), + background: "#272822", + text_color: "#F8F8F2", + directory_link_color: "#F92672", + file_link_color: "#A6E22E", + symlink_link_color: "#FD971F", + table_background: "#3B3A32", + table_text_color: "#F8F8F0", + table_header_background: "#75715E", + table_header_text_color: "#F8F8F2", + table_header_active_color: "#E6DB74", + active_row_color: "#ae81fe3d", + odd_row_background: "#3E3D32", + even_row_background: "#49483E", + root_link_color: "#66D9EF", + download_button_background: "#AE81FF", + download_button_background_hover: "#c6a6ff", + download_button_link_color: "#F8F8F0", + download_button_link_color_hover: "#F8F8F0", + back_button_background: "#AE81FF", + back_button_background_hover: "#AE81FF", + back_button_link_color: "#F8F8F0", + back_button_link_color_hover: "#F8F8F0", + date_text_color: "#66D9EF", + at_color: "#66D9EF", + switch_theme_background: "#3B3A32", + switch_theme_link_color: "#F8F8F2", + switch_theme_active: "#A6E22E", + switch_theme_border: "#49483E", + change_theme_link_color: "#F8F8F2", + change_theme_link_color_hover: "#F8F8F2", + upload_text_color: "#F8F8F2", + upload_form_border_color: "#3B3A32", + upload_form_background: "#49483E", + upload_button_background: "#AE81FF", + upload_button_text_color: "#F8F8F0", + drag_background: "#3333338f", + drag_border_color: "#F8F8F2", + drag_text_color: "#F8F8F2", + size_background_color: "#75715E", + size_text_color: "#F8F8F2", + error_color: "#d02929", }, ColorScheme::Squirrel => Theme { - background: "#FFFFFF".to_string(), - text_color: "#323232".to_string(), - directory_link_color: "#d02474".to_string(), - file_link_color: "#0086B3".to_string(), - symlink_link_color: "#ED6A43".to_string(), - table_background: "#ffffff".to_string(), - table_text_color: "#323232".to_string(), - table_header_background: "#323232".to_string(), - table_header_text_color: "#F5F5F5".to_string(), - table_header_active_color: "#FFFFFF".to_string(), - active_row_color: "#f6f8fa".to_string(), - odd_row_background: "#fbfbfb".to_string(), - even_row_background: "#f2f2f2".to_string(), - root_link_color: "#323232".to_string(), - download_button_background: "#d02474".to_string(), - download_button_background_hover: "#f52d8a".to_string(), - download_button_link_color: "#FFFFFF".to_string(), - download_button_link_color_hover: "#FFFFFF".to_string(), - back_button_background: "#d02474".to_string(), - back_button_background_hover: "#d02474".to_string(), - back_button_link_color: "#FFFFFF".to_string(), - back_button_link_color_hover: "#FFFFFF".to_string(), - date_text_color: "#797979".to_string(), - at_color: "#797979".to_string(), - switch_theme_background: "#323232".to_string(), - switch_theme_link_color: "#F5F5F5".to_string(), - switch_theme_active: "#d02474".to_string(), - switch_theme_border: "#49483E".to_string(), - change_theme_link_color: "#F5F5F5".to_string(), - change_theme_link_color_hover: "#F5F5F5".to_string(), - upload_text_color: "#323232".to_string(), - upload_form_border_color: "#d2d2d2".to_string(), - upload_form_background: "#f2f2f2".to_string(), - upload_button_background: "#d02474".to_string(), - upload_button_text_color: "#FFFFFF".to_string(), - drag_background: "#3333338f".to_string(), - drag_border_color: "#ffffff".to_string(), - drag_text_color: "#ffffff".to_string(), - size_background_color: "#323232".to_string(), - size_text_color: "#FFFFFF".to_string(), - error_color: "#d02424".to_string(), + background: "#FFFFFF", + text_color: "#323232", + directory_link_color: "#d02474", + file_link_color: "#0086B3", + symlink_link_color: "#ED6A43", + table_background: "#ffffff", + table_text_color: "#323232", + table_header_background: "#323232", + table_header_text_color: "#F5F5F5", + table_header_active_color: "#FFFFFF", + active_row_color: "#f6f8fa", + odd_row_background: "#fbfbfb", + even_row_background: "#f2f2f2", + root_link_color: "#323232", + download_button_background: "#d02474", + download_button_background_hover: "#f52d8a", + download_button_link_color: "#FFFFFF", + download_button_link_color_hover: "#FFFFFF", + back_button_background: "#d02474", + back_button_background_hover: "#d02474", + back_button_link_color: "#FFFFFF", + back_button_link_color_hover: "#FFFFFF", + date_text_color: "#797979", + at_color: "#797979", + switch_theme_background: "#323232", + switch_theme_link_color: "#F5F5F5", + switch_theme_active: "#d02474", + switch_theme_border: "#49483E", + change_theme_link_color: "#F5F5F5", + change_theme_link_color_hover: "#F5F5F5", + upload_text_color: "#323232", + upload_form_border_color: "#d2d2d2", + upload_form_background: "#f2f2f2", + upload_button_background: "#d02474", + upload_button_text_color: "#FFFFFF", + drag_background: "#3333338f", + drag_border_color: "#ffffff", + drag_text_color: "#ffffff", + size_background_color: "#323232", + size_text_color: "#FFFFFF", + error_color: "#d02424", }, } } } - -/// Describes a theme -pub struct Theme { - pub background: String, - pub text_color: String, - pub directory_link_color: String, - pub file_link_color: String, - pub symlink_link_color: String, - pub table_background: String, - pub table_text_color: String, - pub table_header_background: String, - pub table_header_text_color: String, - pub table_header_active_color: String, - pub active_row_color: String, - pub odd_row_background: String, - pub even_row_background: String, - pub root_link_color: String, - pub download_button_background: String, - pub download_button_background_hover: String, - pub download_button_link_color: String, - pub download_button_link_color_hover: String, - pub back_button_background: String, - pub back_button_background_hover: String, - pub back_button_link_color: String, - pub back_button_link_color_hover: String, - pub date_text_color: String, - pub at_color: String, - pub switch_theme_background: String, - pub switch_theme_link_color: String, - pub switch_theme_active: String, - pub switch_theme_border: String, - pub change_theme_link_color: String, - pub change_theme_link_color_hover: String, - pub upload_text_color: String, - pub upload_form_border_color: String, - pub upload_form_background: String, - pub upload_button_background: String, - pub upload_button_text_color: String, - pub drag_background: String, - pub drag_border_color: String, - pub drag_text_color: String, - pub size_background_color: String, - pub size_text_color: String, - pub error_color: String, -} |