From 416ce148a49c9d846e9a4cf37a789e67d70a4e67 Mon Sep 17 00:00:00 2001 From: Fabrice Mouhartem Date: Wed, 16 Jul 2025 09:09:40 +0200 Subject: [PATCH] refactor: separate the create_image function --- src/main.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7f6e7a1..afab709 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,20 @@ use async_zip::base::read::seek::ZipFileReader; use async_zip::base::write::ZipFileWriter; use async_zip::{Compression, ZipEntryBuilder}; use infer; -use magick_rust; +use magick_rust::{self, MagickError}; use std::env; use std::ffi::OsStr; use std::path::Path; -use tokio::{ - fs::File, - io::BufReader, -}; -use tokio_util::compat::{TokioAsyncReadCompatExt }; +use tokio::{fs::File, io::BufReader}; +use tokio_util::compat::TokioAsyncReadCompatExt; + +fn compress_image(input: Vec, extension: &str) -> Result, MagickError> { + let wand = magick_rust::MagickWand::new(); + wand.read_image_blob(input)?; + wand.fit(1200, 1600); + wand.strip_image()?; + wand.write_image_blob(extension) +} #[tokio::main] async fn main() -> std::io::Result<()> { @@ -52,12 +57,8 @@ async fn main() -> std::io::Result<()> { .extension() .and_then(OsStr::to_str) .expect("String"); + let to_write = compress_image(buf, extension).expect("Failed to compress image"); println!("{}", extension); - let wand = magick_rust::MagickWand::new(); - wand.read_image_blob(buf).expect("Successful read"); - wand.fit(1200, 1600); - wand.strip_image().expect("Strip"); - let to_write = wand.write_image_blob(extension).expect("Write worked"); println!("data len: {}", to_write.len()); let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate); tmp_zip