pict-rs/src/validate.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

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,
error::Error,
formats::{AnimationOutput, ImageOutput, InputFile, InternalFormat, PrescribedFormats},
2021-10-21 01:13:39 +00:00
};
use actix_web::web::Bytes;
use tokio::io::AsyncRead;
#[tracing::instrument(skip_all)]
pub(crate) async fn validate_bytes(
bytes: Bytes,
prescribed: &PrescribedFormats,
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
let discovery = crate::discover::discover_bytes(bytes.clone()).await?;
match &discovery.input {
InputFile::Image(input) => {
let ImageOutput {
format,
needs_transcode,
} = input.build_output(prescribed.image);
2023-02-04 23:32:36 +00:00
let read = if needs_transcode {
Either::left(Either::left(magick::convert_image(
input.format,
format,
bytes,
)?))
} else {
Either::left(Either::right(exiftool::clear_metadata_bytes_read(bytes)?))
};
Ok((InternalFormat::Image(format), read))
}
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 {
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
}
}
}