Skip on already lower size

This commit is contained in:
Matteo Paonessa 2023-07-07 20:50:49 +02:00
parent 7d2d387c39
commit 7a02953cfa
2 changed files with 8 additions and 3 deletions

View File

@ -226,7 +226,8 @@ pub fn compress_to_size(
) -> Result<(), Box<dyn Error>>
{
let file_type = get_filetype(&input_path);
let in_file = fs::read(input_path)?;
let in_file = fs::read(input_path.clone())?;
let original_size = in_file.len();
let tolerance_percentage = 2;
let tolerance = max_output_size * tolerance_percentage / 100;
let mut quality = 80;
@ -235,6 +236,11 @@ pub fn compress_to_size(
let max_tries: u32 = 10;
let mut tries: u32 = 0;
if original_size <= max_output_size {
fs::copy(input_path, output_path)?;
return Ok(());
}
let compressed_file = loop {
if tries >= max_tries {
return Err("Max tries reached".into());

View File

@ -39,7 +39,7 @@ fn compress_to_10_mb() {
max_output_size
).unwrap();
assert_eq!(100, pars.jpeg.quality);
assert_eq!(80, pars.jpeg.quality);
assert!(std::path::Path::new(output).exists());
assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
let kind = infer::get_from_path(output).unwrap().unwrap();
@ -71,4 +71,3 @@ fn compress_to_range() {
remove_compressed_test_file(output);
}
}