diff --git a/src/bin/ptth_file_server.rs b/src/bin/ptth_file_server.rs index 32a43b5..bbcfcf9 100644 --- a/src/bin/ptth_file_server.rs +++ b/src/bin/ptth_file_server.rs @@ -70,7 +70,7 @@ async fn handle_all (req: Request , state: Arc >) ptth_req.method, &ptth_req.uri, &ptth_req.headers, - state.hidden_path.as_ref ().map (|p| p.as_path ()) + state.hidden_path.as_deref () ).await; let mut resp = Response::builder () @@ -116,7 +116,7 @@ async fn main () -> Result <(), Box > { }, handlebars, server_info: crate::file_server::ServerInfo { - server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()).clone (), + server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()), }, hidden_path: Some (path), }); diff --git a/src/load_toml.rs b/src/load_toml.rs index 58f7159..921ce1f 100644 --- a/src/load_toml.rs +++ b/src/load_toml.rs @@ -30,7 +30,7 @@ pub fn load_public < > ( config_file_path: P ) -> T { - let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); + let f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); load_inner (f) } @@ -45,7 +45,7 @@ pub fn load < ) -> T { use std::os::unix::fs::PermissionsExt; - let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); + let f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); let mode = f.metadata ().unwrap ().permissions ().mode (); assert_eq! (mode, CONFIG_PERMISSIONS_MODE, "Config file has bad permissions mode, it should be octal 0600"); diff --git a/src/server/file_server.rs b/src/server/file_server.rs index 2e3a688..c5f6e9a 100644 --- a/src/server/file_server.rs +++ b/src/server/file_server.rs @@ -146,6 +146,34 @@ fn check_range (range_str: Option <&str>, file_len: u64) PartialContent (start..end) } +fn get_icon (file_name: &str) -> &'static str { + // Because my editor actually doesn't render these + + let video = "🎞️"; + let picture = "📷"; + let file = "📄"; + + if + file_name.ends_with (".mp4") || + file_name.ends_with (".avi") || + file_name.ends_with (".mkv") || + file_name.ends_with (".webm") + { + video + } + else if + file_name.ends_with (".jpg") || + file_name.ends_with (".jpeg") || + file_name.ends_with (".png") || + file_name.ends_with (".bmp") + { + picture + } + else { + file + } +} + async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry { let file_name = match entry.file_name ().into_string () { @@ -174,37 +202,13 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry let (trailing_slash, icon, size) = { let t = metadata.file_type (); + let icon_folder = "📁"; if t.is_dir () { - ("/", "📁", "".into ()) + ("/", icon_folder, "".into ()) } else { - let icon = if file_name.ends_with (".mp4") { - "🎞️" - } - else if file_name.ends_with (".avi") { - "🎞️" - } - else if file_name.ends_with (".mkv") { - "🎞️" - } - else if file_name.ends_with (".jpg") { - "📷" - } - else if file_name.ends_with (".jpeg") { - "📷" - } - else if file_name.ends_with (".png") { - "📷" - } - else if file_name.ends_with (".bmp") { - "📷" - } - else { - "📄" - }; - - ("", icon, pretty_print_bytes (metadata.len ()).into ()) + ("", get_icon (&file_name), pretty_print_bytes (metadata.len ()).into ()) } }; @@ -385,7 +389,7 @@ fn render_markdown (bytes: &[u8], out: &mut String) -> Result <(), MarkdownError use pulldown_cmark::{Parser, Options, html}; let markdown_input = match std::str::from_utf8 (bytes) { - Err (_) => return Err (MarkdownError::FileIsNotUtf8), + Err (_) => return Err (MarkdownError::NotUtf8), Ok (x) => x, }; @@ -430,9 +434,9 @@ struct ServeFileParams { #[derive (Debug, PartialEq)] enum MarkdownError { - FileIsTooBig, - FileIsNotMarkdown, - FileIsNotUtf8, + TooBig, + // NotMarkdown, + NotUtf8, } #[derive (Debug, PartialEq)] @@ -511,7 +515,7 @@ async fn internal_serve_all ( } } - let has_trailing_slash = path_s.is_empty () || path_s.ends_with ("/"); + let has_trailing_slash = path_s.is_empty () || path_s.ends_with ('/'); if let Ok (dir) = read_dir (&full_path).await { if ! has_trailing_slash { @@ -548,7 +552,7 @@ async fn internal_serve_all ( if uri.query () == Some ("as_markdown") { const MAX_BUF_SIZE: u32 = 1_000_000; if file_len > MAX_BUF_SIZE.try_into ().unwrap () { - MarkdownErr (MarkdownError::FileIsTooBig) + MarkdownErr (MarkdownError::TooBig) } else { let mut buffer = vec! [0u8; MAX_BUF_SIZE.try_into ().unwrap ()]; @@ -632,9 +636,9 @@ pub async fn serve_all ( range_requested, }) => serve_file (file.into_inner (), send_body, range, range_requested).await, MarkdownErr (e) => match e { - MarkdownError::FileIsTooBig => serve_error (StatusCode::InternalServerError, "File is too big to preview as Markdown"), - MarkdownError::FileIsNotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"), - MarkdownError::FileIsNotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"), + MarkdownError::TooBig => serve_error (StatusCode::InternalServerError, "File is too big to preview as Markdown"), + //MarkdownError::NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"), + MarkdownError::NotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"), }, MarkdownPreview (s) => serve_html (s), } @@ -695,6 +699,29 @@ mod tests { StatusCode, }; + #[test] + fn icons () { + let video = "🎞️"; + let picture = "📷"; + let file = "📄"; + + for (input, expected) in vec! [ + ("copying_is_not_theft.mp4", video), + ("copying_is_not_theft.avi", video), + ("copying_is_not_theft.mkv", video), + ("copying_is_not_theft.webm", video), + ("lolcats.jpg", picture), + ("lolcats.jpeg", picture), + ("lolcats.png", picture), + ("lolcats.bmp", picture), + ("ptth.log", file), + ("README.md", file), + ("todo.txt", file), + ].into_iter () { + assert_eq! (super::get_icon (input), expected); + } + } + #[test] fn parse_range_header () { for (input, expected) in vec! [ diff --git a/src/server/mod.rs b/src/server/mod.rs index 334e8c8..61b59d7 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -70,7 +70,7 @@ async fn handle_req_resp <'a> ( parts.method, &parts.uri, &parts.headers, - state.hidden_path.as_ref ().map (|p| p.as_path ()) + state.hidden_path.as_deref () ).await; let mut resp_req = state.client @@ -139,7 +139,7 @@ pub async fn run_server ( ) -> Result <(), Box > { - let asset_root = asset_root.unwrap_or_else (|| PathBuf::new ()); + let asset_root = asset_root.unwrap_or_else (PathBuf::new); use std::convert::TryInto;