2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00

Put image crate actions behind web::block

This commit is contained in:
asonix 2020-06-07 11:34:44 -05:00
parent fc1ae4be49
commit d71b86a81e

View file

@ -191,10 +191,13 @@ impl UploadManager {
let bytes = bytes.freeze(); let bytes = bytes.freeze();
// -- VALIDATE IMAGE -- // -- VALIDATE IMAGE --
let format = image::guess_format(&bytes).map_err(UploadError::InvalidImage)?; web::block(move || {
let img = image::load_from_memory(&bytes).map_err(UploadError::InvalidImage)?; let format = image::guess_format(&bytes).map_err(UploadError::InvalidImage)?;
let img = image::load_from_memory(&bytes).map_err(UploadError::InvalidImage)?;
(img, format) Ok((img, format)) as Result<(image::DynamicImage, image::ImageFormat), UploadError>
})
.await?
}; };
let (format, content_type) = self let (format, content_type) = self
@ -208,11 +211,12 @@ impl UploadManager {
return Err(UploadError::ContentType(content_type)); return Err(UploadError::ContentType(content_type));
} }
let bytes: bytes::Bytes = { let bytes: bytes::Bytes = web::block(move || {
let mut bytes = std::io::Cursor::new(vec![]); let mut bytes = std::io::Cursor::new(vec![]);
img.write_to(&mut bytes, format)?; img.write_to(&mut bytes, format)?;
bytes::Bytes::from(bytes.into_inner()) Ok(bytes::Bytes::from(bytes.into_inner())) as Result<bytes::Bytes, UploadError>
}; })
.await?;
// -- DUPLICATE CHECKS -- // -- DUPLICATE CHECKS --