Compare commits
2 Commits
72b8174422
...
main
Author | SHA1 | Date | |
---|---|---|---|
02ad79a93c
|
|||
416ce148a4
|
44
src/main.rs
44
src/main.rs
@@ -3,15 +3,20 @@ use async_zip::base::read::seek::ZipFileReader;
|
|||||||
use async_zip::base::write::ZipFileWriter;
|
use async_zip::base::write::ZipFileWriter;
|
||||||
use async_zip::{Compression, ZipEntryBuilder};
|
use async_zip::{Compression, ZipEntryBuilder};
|
||||||
use infer;
|
use infer;
|
||||||
use magick_rust;
|
use magick_rust::{self, MagickError};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::{
|
use tokio::{fs::File, io::BufReader};
|
||||||
fs::File,
|
use tokio_util::compat::TokioAsyncReadCompatExt;
|
||||||
io::BufReader,
|
|
||||||
};
|
fn compress_image(input: Vec<u8>, extension: &str) -> Result<Vec<u8>, MagickError> {
|
||||||
use tokio_util::compat::{TokioAsyncReadCompatExt };
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
@@ -47,30 +52,23 @@ 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)
|
||||||
.expect("String");
|
.expect("String");
|
||||||
|
let to_write = compress_image(buf, extension).expect("Failed to compress image");
|
||||||
println!("{}", extension);
|
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());
|
println!("data len: {}", to_write.len());
|
||||||
let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate);
|
to_write
|
||||||
tmp_zip
|
|
||||||
.write_entry_whole(builder, &to_write)
|
|
||||||
.await
|
|
||||||
.expect("Failed to write a compressed image.");
|
|
||||||
} else {
|
} else {
|
||||||
let builder = ZipEntryBuilder::new(filename.clone(), Compression::Deflate);
|
buf
|
||||||
tmp_zip
|
};
|
||||||
.write_entry_whole(builder, &buf)
|
tmp_zip
|
||||||
.await
|
.write_entry_whole(builder, &to_write)
|
||||||
.expect("Failed to write a normal file??");
|
.await
|
||||||
}
|
.expect("Failed to write a compressed image.");
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp_zip.close().await.expect("Failed to close the file");
|
tmp_zip.close().await.expect("Failed to close the file");
|
||||||
|
Reference in New Issue
Block a user