2023-07-13 03:12:21 +00:00
|
|
|
mod exiftool;
|
|
|
|
mod ffmpeg;
|
|
|
|
mod magick;
|
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
use crate::{
|
2023-07-13 22:42:21 +00:00
|
|
|
discover::Discovery,
|
2022-03-29 16:04:56 +00:00
|
|
|
either::Either,
|
2023-07-13 03:12:21 +00:00
|
|
|
error::Error,
|
2023-07-13 22:42:21 +00:00
|
|
|
formats::{
|
|
|
|
AnimationFormat, AnimationOutput, ImageInput, ImageOutput, InputFile, InternalFormat,
|
|
|
|
OutputVideoFormat, Validations, VideoFormat,
|
|
|
|
},
|
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
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum ValidationError {
|
|
|
|
#[error("Too wide")]
|
|
|
|
Width,
|
|
|
|
|
|
|
|
#[error("Too tall")]
|
|
|
|
Height,
|
|
|
|
|
|
|
|
#[error("Too many pixels")]
|
|
|
|
Area,
|
|
|
|
|
|
|
|
#[error("Too many frames")]
|
|
|
|
Frames,
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
#[error("Uploaded file is empty")]
|
|
|
|
Empty,
|
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
#[error("Filesize too large")]
|
|
|
|
Filesize,
|
|
|
|
|
|
|
|
#[error("Video is disabled")]
|
|
|
|
VideoDisabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
const MEGABYTES: usize = 1024 * 1024;
|
|
|
|
|
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 22:42:21 +00:00
|
|
|
validations: Validations<'_>,
|
2023-07-13 03:12:21 +00:00
|
|
|
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
|
2023-07-14 00:21:57 +00:00
|
|
|
if bytes.is_empty() {
|
|
|
|
return Err(ValidationError::Empty.into());
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
let Discovery {
|
|
|
|
input,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames,
|
|
|
|
} = crate::discover::discover_bytes(bytes.clone()).await?;
|
2021-09-26 16:02:19 +00:00
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
match &input {
|
2023-07-13 03:12:21 +00:00
|
|
|
InputFile::Image(input) => {
|
2023-07-13 22:42:21 +00:00
|
|
|
let (format, read) =
|
|
|
|
process_image(bytes, *input, width, height, validations.image).await?;
|
2023-07-13 03:12:21 +00:00
|
|
|
|
2023-07-13 22:42:21 +00:00
|
|
|
Ok((format, Either::left(read)))
|
2022-09-25 23:16:37 +00:00
|
|
|
}
|
2023-07-13 03:12:21 +00:00
|
|
|
InputFile::Animation(input) => {
|
2023-07-13 22:42:21 +00:00
|
|
|
let (format, read) = process_animation(
|
|
|
|
bytes,
|
|
|
|
*input,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames.unwrap_or(1),
|
|
|
|
&validations,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok((format, Either::right(Either::left(read))))
|
|
|
|
}
|
|
|
|
InputFile::Video(input) => {
|
|
|
|
let (format, read) = process_video(
|
|
|
|
bytes,
|
|
|
|
*input,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
frames.unwrap_or(1),
|
|
|
|
validations.video,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok((format, Either::right(Either::right(read))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(bytes, validations))]
|
|
|
|
async fn process_image(
|
|
|
|
bytes: Bytes,
|
|
|
|
input: ImageInput,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
validations: &crate::config::Image,
|
|
|
|
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
|
|
|
|
if width > validations.max_width {
|
|
|
|
return Err(ValidationError::Width.into());
|
|
|
|
}
|
|
|
|
if height > validations.max_height {
|
|
|
|
return Err(ValidationError::Height.into());
|
|
|
|
}
|
|
|
|
if u32::from(width) * u32::from(height) > validations.max_area {
|
|
|
|
return Err(ValidationError::Area.into());
|
|
|
|
}
|
|
|
|
if bytes.len() > validations.max_file_size * MEGABYTES {
|
|
|
|
return Err(ValidationError::Filesize.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let ImageOutput {
|
|
|
|
format,
|
|
|
|
needs_transcode,
|
|
|
|
} = input.build_output(validations.format);
|
|
|
|
|
|
|
|
let read = if needs_transcode {
|
2023-07-17 22:45:26 +00:00
|
|
|
let quality = validations.quality_for(format);
|
|
|
|
|
|
|
|
Either::left(magick::convert_image(input.format, format, quality, bytes).await?)
|
2023-07-13 22:42:21 +00:00
|
|
|
} else {
|
|
|
|
Either::right(exiftool::clear_metadata_bytes_read(bytes)?)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((InternalFormat::Image(format), read))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_animation(
|
|
|
|
size: usize,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: u32,
|
|
|
|
validations: &crate::config::Animation,
|
|
|
|
) -> Result<(), ValidationError> {
|
|
|
|
if width > validations.max_width {
|
|
|
|
return Err(ValidationError::Width);
|
|
|
|
}
|
|
|
|
if height > validations.max_height {
|
|
|
|
return Err(ValidationError::Height);
|
|
|
|
}
|
|
|
|
if u32::from(width) * u32::from(height) > validations.max_area {
|
|
|
|
return Err(ValidationError::Area);
|
|
|
|
}
|
|
|
|
if frames > validations.max_frame_count {
|
|
|
|
return Err(ValidationError::Frames);
|
|
|
|
}
|
|
|
|
if size > validations.max_file_size * MEGABYTES {
|
|
|
|
return Err(ValidationError::Filesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(bytes, validations))]
|
|
|
|
async fn process_animation(
|
|
|
|
bytes: Bytes,
|
|
|
|
input: AnimationFormat,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: u32,
|
|
|
|
validations: &Validations<'_>,
|
|
|
|
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
|
|
|
|
match validate_animation(bytes.len(), width, height, frames, validations.animation) {
|
|
|
|
Ok(()) => {
|
2023-07-13 03:12:21 +00:00
|
|
|
let AnimationOutput {
|
|
|
|
format,
|
|
|
|
needs_transcode,
|
2023-07-13 22:42:21 +00:00
|
|
|
} = input.build_output(validations.animation.format);
|
2023-07-13 03:12:21 +00:00
|
|
|
|
|
|
|
let read = if needs_transcode {
|
2023-07-17 22:45:26 +00:00
|
|
|
let quality = validations.animation.quality_for(format);
|
|
|
|
|
|
|
|
Either::left(magick::convert_animation(input, format, quality, bytes).await?)
|
2022-10-15 16:13:24 +00:00
|
|
|
} else {
|
2023-07-13 22:42:21 +00:00
|
|
|
Either::right(Either::left(exiftool::clear_metadata_bytes_read(bytes)?))
|
2023-07-13 03:12:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((InternalFormat::Animation(format), read))
|
|
|
|
}
|
2023-07-17 22:45:26 +00:00
|
|
|
Err(_) => match validate_video(bytes.len(), width, height, frames, validations.video) {
|
|
|
|
Ok(()) => {
|
|
|
|
let output = OutputVideoFormat::from_parts(
|
|
|
|
validations.video.video_codec,
|
|
|
|
validations.video.audio_codec,
|
|
|
|
validations.video.allow_audio,
|
|
|
|
);
|
|
|
|
|
|
|
|
let read = Either::right(Either::right(
|
|
|
|
magick::convert_video(input, output, bytes).await?,
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok((InternalFormat::Video(output.internal_format()), read))
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
},
|
2023-07-13 22:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_video(
|
|
|
|
size: usize,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: u32,
|
|
|
|
validations: &crate::config::Video,
|
|
|
|
) -> Result<(), ValidationError> {
|
|
|
|
if !validations.enable {
|
|
|
|
return Err(ValidationError::VideoDisabled);
|
|
|
|
}
|
|
|
|
if width > validations.max_width {
|
|
|
|
return Err(ValidationError::Width);
|
|
|
|
}
|
|
|
|
if height > validations.max_height {
|
|
|
|
return Err(ValidationError::Height);
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
2023-07-13 22:42:21 +00:00
|
|
|
if u32::from(width) * u32::from(height) > validations.max_area {
|
|
|
|
return Err(ValidationError::Area);
|
|
|
|
}
|
|
|
|
if frames > validations.max_frame_count {
|
|
|
|
return Err(ValidationError::Frames);
|
|
|
|
}
|
|
|
|
if size > validations.max_file_size * MEGABYTES {
|
|
|
|
return Err(ValidationError::Filesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(bytes, validations))]
|
|
|
|
async fn process_video(
|
|
|
|
bytes: Bytes,
|
|
|
|
input: VideoFormat,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
frames: u32,
|
|
|
|
validations: &crate::config::Video,
|
|
|
|
) -> Result<(InternalFormat, impl AsyncRead + Unpin), Error> {
|
|
|
|
validate_video(bytes.len(), width, height, frames, validations)?;
|
|
|
|
|
|
|
|
let output = input.build_output(
|
|
|
|
validations.video_codec,
|
|
|
|
validations.audio_codec,
|
|
|
|
validations.allow_audio,
|
|
|
|
);
|
|
|
|
|
2023-07-17 22:45:26 +00:00
|
|
|
let crf = validations.crf_for(width, height);
|
|
|
|
|
|
|
|
let read = ffmpeg::transcode_bytes(input, output, crf, bytes).await?;
|
2023-07-13 22:42:21 +00:00
|
|
|
|
|
|
|
Ok((InternalFormat::Video(output.internal_format()), read))
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|