refactor: deduplicate code

This commit is contained in:
2025-07-16 09:19:32 +02:00
parent 416ce148a4
commit 02ad79a93c

View File

@@ -52,7 +52,8 @@ async fn main() -> std::io::Result<()> {
.read_to_end_checked(&mut buf) .read_to_end_checked(&mut buf)
.await .await
.expect("Failed to read the zip individual file."); .expect("Failed to read the zip individual file.");
if infer::is_image(&buf[..n]) { let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate);
let to_write = if infer::is_image(&buf[..n]) {
let extension = Path::new(filename.as_str().expect("Failed to read filename.")) let extension = Path::new(filename.as_str().expect("Failed to read filename."))
.extension() .extension()
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
@@ -60,18 +61,14 @@ async fn main() -> std::io::Result<()> {
let to_write = compress_image(buf, extension).expect("Failed to compress image"); let to_write = compress_image(buf, extension).expect("Failed to compress image");
println!("{}", extension); println!("{}", extension);
println!("data len: {}", to_write.len()); println!("data len: {}", to_write.len());
let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate); to_write
} else {
buf
};
tmp_zip tmp_zip
.write_entry_whole(builder, &to_write) .write_entry_whole(builder, &to_write)
.await .await
.expect("Failed to write a compressed image."); .expect("Failed to write a compressed image.");
} else {
let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate);
tmp_zip
.write_entry_whole(builder, &buf)
.await
.expect("Failed to write a normal file??");
}
} }
tmp_zip.close().await.expect("Failed to close the file"); tmp_zip.close().await.expect("Failed to close the file");