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();
// -- VALIDATE IMAGE --
web::block(move || {
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
@ -208,11 +211,12 @@ impl UploadManager {
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![]);
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 --