Fix unknown file panic

This commit is contained in:
Matteo Paonessa 2021-10-23 21:38:56 +02:00
parent c360e1a18f
commit ca381a440d
3 changed files with 28 additions and 12 deletions

View File

@ -71,12 +71,12 @@ pub extern fn c_compress(input_path: *const c_char, output_path: *const c_char,
parameters.gif.quality = params.gif_quality; parameters.gif.quality = params.gif_quality;
parameters.webp.quality = params.webp_quality; parameters.webp.quality = params.webp_quality;
compress(CStr::from_ptr(input_path).to_str().unwrap().to_string(), match compress(CStr::from_ptr(input_path).to_str().unwrap().to_string(),
CStr::from_ptr(output_path).to_str().unwrap().to_string(), CStr::from_ptr(output_path).to_str().unwrap().to_string(),
parameters) parameters) {
.unwrap(); Ok(_) => true,
Err(_) => false
true }
} }
} }

View File

@ -8,13 +8,18 @@ pub enum SupportedFileTypes {
pub fn get_filetype(file_path: &str) -> SupportedFileTypes { pub fn get_filetype(file_path: &str) -> SupportedFileTypes {
match infer::get_from_path(file_path) { match infer::get_from_path(file_path) {
Ok(v) => match v.unwrap().mime_type() { Ok(v) => match v {
"image/jpeg" => SupportedFileTypes::Jpeg, None => {
"image/png" => SupportedFileTypes::Png, SupportedFileTypes::Unkn
"image/gif" => SupportedFileTypes::Gif, }
"image/webp" => SupportedFileTypes::WebP, Some(ft) => match ft.mime_type() {
_ => SupportedFileTypes::Unkn "image/jpeg" => SupportedFileTypes::Jpeg,
"image/png" => SupportedFileTypes::Png,
"image/gif" => SupportedFileTypes::Gif,
"image/webp" => SupportedFileTypes::WebP,
_ => SupportedFileTypes::Unkn
}
}, },
Err(e) => panic!("{}", e) Err(_) => SupportedFileTypes::Unkn
} }
} }

11
tests/integration.rs Normal file
View File

@ -0,0 +1,11 @@
use caesium;
#[test]
fn unknown_file_type() {
let output = "tests/samples/output/should_not_be_there";
let params = caesium::initialize_parameters();
let result = caesium::compress(String::from("tests/samples/output/.gitkeep"),
String::from(output),
params);
assert!(result.is_err())
}