refactor: deduplicate code
This commit is contained in:
15
src/main.rs
15
src/main.rs
@@ -52,7 +52,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.read_to_end_checked(&mut buf)
|
||||
.await
|
||||
.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."))
|
||||
.extension()
|
||||
.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");
|
||||
println!("{}", extension);
|
||||
println!("data len: {}", to_write.len());
|
||||
let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate);
|
||||
to_write
|
||||
} else {
|
||||
buf
|
||||
};
|
||||
tmp_zip
|
||||
.write_entry_whole(builder, &to_write)
|
||||
.await
|
||||
.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");
|
||||
|
Reference in New Issue
Block a user