2021-08-31 02:19:47 +00:00
|
|
|
use crate::{config::Format, error::UploadError, ffmpeg::InputFormat, magick::ValidInputType};
|
2021-08-28 22:15:14 +00:00
|
|
|
|
|
|
|
pub(crate) fn image_webp() -> mime::Mime {
|
|
|
|
"image/webp".parse().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn video_mp4() -> mime::Mime {
|
|
|
|
"video/mp4".parse().unwrap()
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:19:47 +00:00
|
|
|
pub(crate) async fn validate_image_stream<S>(
|
|
|
|
stream: S,
|
2021-08-28 22:15:14 +00:00
|
|
|
prescribed_format: Option<Format>,
|
2021-08-31 02:19:47 +00:00
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
mime::Mime,
|
|
|
|
futures::stream::LocalBoxStream<'static, Result<actix_web::web::Bytes, UploadError>>,
|
|
|
|
),
|
|
|
|
UploadError,
|
|
|
|
>
|
|
|
|
where
|
|
|
|
S: futures::stream::Stream<Item = Result<actix_web::web::Bytes, UploadError>> + Unpin + 'static,
|
|
|
|
{
|
|
|
|
let (base_stream, copied_stream) = crate::stream::try_duplicate(stream, 1024);
|
|
|
|
|
|
|
|
let input_type =
|
|
|
|
crate::magick::input_type_stream::<_, UploadError, UploadError>(Box::pin(base_stream))
|
|
|
|
.await?;
|
2021-08-28 22:15:14 +00:00
|
|
|
|
|
|
|
match (prescribed_format, input_type) {
|
2021-08-31 02:19:47 +00:00
|
|
|
(_, ValidInputType::Gif) => Ok((
|
|
|
|
video_mp4(),
|
|
|
|
crate::ffmpeg::to_mp4_stream(copied_stream, InputFormat::Gif)?,
|
|
|
|
)),
|
|
|
|
(_, ValidInputType::Mp4) => Ok((
|
|
|
|
video_mp4(),
|
|
|
|
crate::ffmpeg::to_mp4_stream(copied_stream, InputFormat::Mp4)?,
|
|
|
|
)),
|
|
|
|
(Some(Format::Jpeg) | None, ValidInputType::Jpeg) => Ok((
|
|
|
|
mime::IMAGE_JPEG,
|
2021-08-31 16:02:30 +00:00
|
|
|
crate::exiftool::clear_metadata_stream(copied_stream)?,
|
2021-08-31 02:19:47 +00:00
|
|
|
)),
|
|
|
|
(Some(Format::Png) | None, ValidInputType::Png) => Ok((
|
|
|
|
mime::IMAGE_PNG,
|
2021-08-31 16:02:30 +00:00
|
|
|
crate::exiftool::clear_metadata_stream(copied_stream)?,
|
2021-08-31 02:19:47 +00:00
|
|
|
)),
|
|
|
|
(Some(Format::Webp) | None, ValidInputType::Webp) => Ok((
|
|
|
|
image_webp(),
|
|
|
|
crate::magick::clear_metadata_stream(copied_stream)?,
|
|
|
|
)),
|
|
|
|
(Some(format), _) => Ok((
|
|
|
|
format.to_mime(),
|
|
|
|
crate::magick::convert_stream(copied_stream, format)?,
|
|
|
|
)),
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
}
|