2023-07-13 03:12:21 +00:00
|
|
|
mod exiftool;
|
|
|
|
mod ffmpeg;
|
|
|
|
mod magick;
|
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
use crate::{
|
2022-03-29 16:04:56 +00:00
|
|
|
either::Either,
|
2023-07-13 03:12:21 +00:00
|
|
|
error::Error,
|
|
|
|
formats::{AnimationOutput, ImageOutput, InputFile, InternalFormat, PrescribedFormats},
|
2021-10-21 01:13:39 +00:00
|
|
|
};
|
2021-09-04 00:53:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
|
|
|
use tokio::io::AsyncRead;
|
2021-08-28 22:15:14 +00:00
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-10-01 00:38:11 +00:00
|
|
|
pub(crate) async fn validate_bytes(
|
2021-09-04 00:53:53 +00:00
|
|
|
bytes: Bytes,
|
2023-07-13 03:12:21 +00:00
|
|
|
prescribed: &PrescribedFormats,
|
|
|
|
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
|
|
|
|
let discovery = crate::discover::discover_bytes(bytes.clone()).await?;
|
2021-09-26 16:02:19 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
match &discovery.input {
|
|
|
|
InputFile::Image(input) => {
|
|
|
|
let ImageOutput {
|
|
|
|
format,
|
|
|
|
needs_transcode,
|
|
|
|
} = input.build_output(prescribed.image);
|
2023-02-04 23:32:36 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
let read = if needs_transcode {
|
|
|
|
Either::left(Either::left(magick::convert_image(
|
|
|
|
input.format,
|
|
|
|
format,
|
|
|
|
bytes,
|
|
|
|
)?))
|
2023-06-19 19:25:22 +00:00
|
|
|
} else {
|
2023-07-13 03:12:21 +00:00
|
|
|
Either::left(Either::right(exiftool::clear_metadata_bytes_read(bytes)?))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((InternalFormat::Image(format), read))
|
2022-09-25 23:16:37 +00:00
|
|
|
}
|
2023-07-13 03:12:21 +00:00
|
|
|
InputFile::Animation(input) => {
|
|
|
|
let AnimationOutput {
|
|
|
|
format,
|
|
|
|
needs_transcode,
|
|
|
|
} = input.build_output(prescribed.animation);
|
|
|
|
|
|
|
|
let read = if needs_transcode {
|
|
|
|
Either::right(Either::left(magick::convert_animation(
|
|
|
|
input.format,
|
|
|
|
format,
|
|
|
|
bytes,
|
|
|
|
)?))
|
2022-10-15 16:13:24 +00:00
|
|
|
} else {
|
2023-07-13 03:12:21 +00:00
|
|
|
Either::right(Either::right(Either::left(
|
|
|
|
exiftool::clear_metadata_bytes_read(bytes)?,
|
|
|
|
)))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((InternalFormat::Animation(format), read))
|
|
|
|
}
|
|
|
|
InputFile::Video(input) => {
|
|
|
|
let output = input.build_output(prescribed.video, prescribed.allow_audio);
|
|
|
|
let read = Either::right(Either::right(Either::right(
|
|
|
|
ffmpeg::transcode_bytes(*input, output, bytes).await?,
|
|
|
|
)));
|
|
|
|
|
|
|
|
Ok((InternalFormat::Video(output.internal_format()), read))
|
2022-10-15 16:13:24 +00:00
|
|
|
}
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
}
|