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.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(),
parameters)
.unwrap();
true
parameters) {
Ok(_) => true,
Err(_) => false
}
}
}

View File

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