From 480acf515f018b62c08d69cec94f07d333fdc839 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Wed, 6 Oct 2021 02:36:09 +1000 Subject: [PATCH] cleanup --- src/files.rs | 9 ++++++--- src/tests/mod.rs | 12 ++++-------- src/utils.rs | 6 ++++++ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/files.rs b/src/files.rs index d658163..9c2b550 100644 --- a/src/files.rs +++ b/src/files.rs @@ -309,7 +309,6 @@ pub fn mime_extension_lookup(essence: String) -> Option> { } } - let essence = essence; let mut exts = mime_guess::get_mime_extensions_str(essence.as_str()); if exts.is_none() { // no matches :c @@ -363,9 +362,13 @@ pub fn mime_extension_lookup(essence: String) -> Option> { ] .concat() } else if essence == "application/x-ms-dos-executable" { - // both .dll and .exe files are given the same mime type... but you definitely don't want to rename one to the + // .dll, .exe, and .scr files are given the same mime type... but you definitely don't want to rename one to the // other! - [vec![String::from("dll"), String::from("exe")], possible_exts].concat() + [ + vec![String::from("dll"), String::from("exe"), String::from("scr")], + possible_exts, + ] + .concat() } else { possible_exts }) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 7760e58..89e55fd 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -11,6 +11,7 @@ use fif::files::{mime_extension_lookup, scan_directory, scan_from_walkdir, BUF_S use fif::findings::Findings; use fif::formats::{Format, PowerShell, Shell}; use fif::mime_db::MimeDb; +use fif::utils::APPLICATION_ZIP; use fif::{String, MIMEDB}; use itertools::Itertools; use maplit::{btreeset, hashmap}; @@ -24,11 +25,6 @@ const PNG_BYTES: &[u8] = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; const PDF_BYTES: &[u8] = b"%PDF-"; const ZIP_BYTES: &[u8] = b"PK\x03\x04"; -fn application_zip() -> Mime { - use std::str::FromStr; - Mime::from_str("application/zip").unwrap() -} - #[test] /// Ensure that `extension_from_path` successfully returns the extension from a set of paths. fn get_ext() { @@ -52,7 +48,7 @@ fn detect_type() { assert_eq!(MIMEDB.get_type(JPEG_BYTES), Some(IMAGE_JPEG)); assert_eq!(MIMEDB.get_type(PNG_BYTES), Some(IMAGE_PNG)); assert_eq!(MIMEDB.get_type(PDF_BYTES), Some(APPLICATION_PDF)); - assert_eq!(MIMEDB.get_type(ZIP_BYTES), Some(application_zip())); + assert_eq!(MIMEDB.get_type(ZIP_BYTES), Some(APPLICATION_ZIP.clone())); } #[test] @@ -68,7 +64,7 @@ fn recommend_ext() { assert!(mime_extension_lookup(APPLICATION_PDF.essence_str().into()) .unwrap() .contains(&String::from("pdf"))); - assert!(mime_extension_lookup(application_zip().essence_str().into()) + assert!(mime_extension_lookup(APPLICATION_ZIP.essence_str().into()) .unwrap() .contains(&String::from("zip"))); } @@ -178,7 +174,7 @@ fn simple_directory() { "jpg" | "jpeg" => IMAGE_JPEG, "png" => IMAGE_PNG, "pdf" => APPLICATION_PDF, - "zip" => application_zip(), + "zip" => APPLICATION_ZIP.clone(), _ => APPLICATION_OCTET_STREAM, // general "fallback" type }, "Incorrect MIME type detected - got {:?} for a {:?} file", diff --git a/src/utils.rs b/src/utils.rs index fc867e3..929647f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,10 @@ //! Various minor utilities. +use std::str::FromStr; + use cfg_if::cfg_if; +use mime::Mime; use once_cell::sync::Lazy; use crate::String; @@ -36,6 +39,9 @@ pub static CLAP_LONG_VERSION: Lazy = Lazy::new(|| { .into() }); +/// A [`Mime`] representing the "application/zip" mimetype. +pub static APPLICATION_ZIP: Lazy = Lazy::new(|| Mime::from_str("application/zip").unwrap()); + /// Returns the name of the target operating system with proper casing, like "Windows" or "macOS". #[allow(clippy::option_map_unit_fn)] pub fn os_name() -> String {