2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-14 16:33:57 +00:00
pict-rs/src/validate.rs

54 lines
1.9 KiB
Rust
Raw Normal View History

2021-09-14 01:22:42 +00:00
use crate::{config::Format, error::Error, ffmpeg::InputFormat, magick::ValidInputType};
use actix_web::web::Bytes;
use tokio::io::AsyncRead;
2021-09-14 01:22:42 +00:00
use tracing::instrument;
pub(crate) fn image_webp() -> mime::Mime {
"image/webp".parse().unwrap()
}
pub(crate) fn video_mp4() -> mime::Mime {
"video/mp4".parse().unwrap()
}
2021-09-14 01:22:42 +00:00
#[instrument(name = "Validate image", skip(bytes))]
pub(crate) async fn validate_image_bytes(
bytes: Bytes,
prescribed_format: Option<Format>,
2021-09-14 01:22:42 +00:00
) -> Result<(mime::Mime, Box<dyn AsyncRead + Unpin>), Error> {
let input_type = crate::magick::input_type_bytes(bytes.clone()).await?;
match (prescribed_format, input_type) {
(_, ValidInputType::Gif) => Ok((
video_mp4(),
2021-09-14 01:22:42 +00:00
Box::new(crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Gif)?)
as Box<dyn AsyncRead + Unpin>,
)),
(_, ValidInputType::Mp4) => Ok((
video_mp4(),
2021-09-14 01:22:42 +00:00
Box::new(crate::ffmpeg::to_mp4_bytes(bytes, InputFormat::Mp4)?)
as Box<dyn AsyncRead + Unpin>,
)),
(Some(Format::Jpeg) | None, ValidInputType::Jpeg) => Ok((
mime::IMAGE_JPEG,
2021-09-14 01:22:42 +00:00
Box::new(crate::exiftool::clear_metadata_bytes_read(bytes)?)
as Box<dyn AsyncRead + Unpin>,
)),
(Some(Format::Png) | None, ValidInputType::Png) => Ok((
mime::IMAGE_PNG,
2021-09-14 01:22:42 +00:00
Box::new(crate::exiftool::clear_metadata_bytes_read(bytes)?)
as Box<dyn AsyncRead + Unpin>,
)),
(Some(Format::Webp) | None, ValidInputType::Webp) => Ok((
image_webp(),
2021-09-14 01:22:42 +00:00
Box::new(crate::magick::clear_metadata_bytes_read(bytes)?)
as Box<dyn AsyncRead + Unpin>,
)),
(Some(format), _) => Ok((
format.to_mime(),
2021-09-14 01:22:42 +00:00
Box::new(crate::magick::convert_bytes_read(bytes, format)?)
as Box<dyn AsyncRead + Unpin>,
)),
}
}