This commit is contained in:
Lynne Megido 2021-10-06 02:36:09 +10:00
parent 58d5ac7e75
commit 480acf515f
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 16 additions and 11 deletions

View file

@ -309,7 +309,6 @@ pub fn mime_extension_lookup(essence: String) -> Option<Vec<String>> {
}
}
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<Vec<String>> {
]
.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
})

View file

@ -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",

View file

@ -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<String> = Lazy::new(|| {
.into()
});
/// A [`Mime`] representing the "application/zip" mimetype.
pub static APPLICATION_ZIP: Lazy<Mime> = 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 {