Skip to content

Commit

Permalink
Attack of the clones
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Nov 1, 2024
1 parent 0e732f1 commit 75eaab7
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions rust/src/api/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@ pub struct CompressResult {

pub fn process_img(parameters: Parameters) -> Result<CompressResult, String> {
let img = read_image(&parameters.path)?;
let original_image_type = guess_image_type(parameters.path.clone())?;
let original_image_type = guess_image_type(&parameters.path)?;
let new_image_type = parameters.convert_extension.unwrap_or(original_image_type);
let out_path = get_out_path(&parameters, new_image_type);

let csparams = create_csparameters(&parameters, img.width(), img.height());

let should_convert = new_image_type != original_image_type;

let path = parameters.path.clone();
let result = if should_convert {
convert_image(path, out_path.clone(), csparams)?
convert_image(&parameters.path, &out_path, csparams)?
} else {
compress_image(path, out_path.clone(), csparams)?
compress_image(&parameters.path, &out_path, csparams)?
};

Ok(CompressResult {
path: parameters.path,
out_path: out_path.clone(),
out_path,
result,
})
}
Expand All @@ -66,7 +65,7 @@ fn read_image(path: &str) -> Result<DynamicImage, String> {
}
}

fn guess_image_type(path: String) -> Result<ImageType, String> {
fn guess_image_type(path: &str) -> Result<ImageType, String> {
let kind =
infer::get_from_path(path).map_err(|e| format!("Error determining file type: {}", e))?;
match kind {
Expand All @@ -86,9 +85,8 @@ fn guess_image_type(path: String) -> Result<ImageType, String> {
}

fn get_out_path(parameters: &Parameters, image_type: ImageType) -> String {
let out_path = parameters.path.clone();
let extension = parameters.convert_extension.unwrap_or(image_type);
let path = Path::new(&out_path);
let path = Path::new(&parameters.path);
let original_extension = path.extension().unwrap_or_default();
format!(
"{}{}.{}",
Expand Down Expand Up @@ -140,26 +138,18 @@ fn create_csparameters(parameters: &Parameters, width: u32, height: u32) -> CSPa
cspars
}

fn compress_image(
path: String,
out_path: String,
mut params: CSParameters,
) -> Result<String, String> {
let result = caesium::compress(path, out_path, &mut params);
fn compress_image(path: &str, out_path: &str, mut params: CSParameters) -> Result<String, String> {
let result = caesium::compress(path.to_string(), out_path.to_string(), &mut params);
match result {
Ok(_) => Ok("Success".to_string()),
Err(err) => Err(format!("Error: {}", err)),
}
}

fn convert_image(
path: String,
out_path: String,
mut params: CSParameters,
) -> Result<String, String> {
fn convert_image(path: &str, out_path: &str, mut params: CSParameters) -> Result<String, String> {
let result = caesium::convert(
path,
out_path,
path.to_string(),
out_path.to_string(),
&mut params,
caesium::SupportedFileTypes::WebP,
);
Expand Down Expand Up @@ -201,7 +191,7 @@ mod tests {

#[test]
fn test_guess_image_type() {
let result = guess_image_type("test/test.png".to_string());
let result = guess_image_type("test/test.png");
println!("{:?}", result);
assert_eq!(result.unwrap(), ImageType::PNG);
}
Expand Down

0 comments on commit 75eaab7

Please sign in to comment.