From fd965bb1a581f80f163dd374b22eb99b3aad6560 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 24 Sep 2023 13:20:33 -0500 Subject: [PATCH] Allow processing still images into animation formats --- src/error.rs | 5 ----- src/formats.rs | 26 ++++++++++++-------------- src/generate.rs | 8 ++------ src/magick.rs | 4 ++-- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/error.rs b/src/error.rs index 9d31dde..0855d56 100644 --- a/src/error.rs +++ b/src/error.rs @@ -107,9 +107,6 @@ pub(crate) enum UploadError { #[error("pict-rs is in read-only mode")] ReadOnly, - #[error("Requested file extension cannot be served by source file")] - InvalidProcessExtension, - #[error("Provided process path is invalid")] ParsePath, @@ -173,7 +170,6 @@ impl UploadError { } Self::Download(_) => ErrorCode::DOWNLOAD_FILE_ERROR, Self::ReadOnly => ErrorCode::READ_ONLY, - Self::InvalidProcessExtension => ErrorCode::INVALID_FILE_EXTENSION, Self::ParsePath => ErrorCode::INVALID_PROCESS_PATH, Self::Semaphore => ErrorCode::PROCESS_SEMAPHORE_CLOSED, Self::Canceled => ErrorCode::PANIC, @@ -235,7 +231,6 @@ impl ResponseError for Error { | UploadError::Repo(crate::repo::RepoError::AlreadyClaimed) | UploadError::Validation(_) | UploadError::UnsupportedProcessExtension - | UploadError::InvalidProcessExtension | UploadError::ReadOnly | UploadError::FailedExternalValidation, ) => StatusCode::BAD_REQUEST, diff --git a/src/formats.rs b/src/formats.rs index e82fbec..076a600 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -166,33 +166,31 @@ impl ProcessableFormat { } } - pub(crate) const fn process_to(self, output: InputProcessableFormat) -> Option { + pub(crate) const fn process_to(self, output: InputProcessableFormat) -> Self { match (self, output) { - (Self::Image(_), InputProcessableFormat::Avif) => Some(Self::Image(ImageFormat::Avif)), + (Self::Image(_), InputProcessableFormat::Avif) => Self::Image(ImageFormat::Avif), (Self::Image(_) | Self::Animation(_), InputProcessableFormat::Jpeg) => { - Some(Self::Image(ImageFormat::Jpeg)) + Self::Image(ImageFormat::Jpeg) } (Self::Image(_) | Self::Animation(_), InputProcessableFormat::Jxl) => { - Some(Self::Image(ImageFormat::Jxl)) + Self::Image(ImageFormat::Jxl) } (Self::Image(_) | Self::Animation(_), InputProcessableFormat::Png) => { - Some(Self::Image(ImageFormat::Png)) + Self::Image(ImageFormat::Png) } - (Self::Image(_), InputProcessableFormat::Webp) => Some(Self::Image(ImageFormat::Webp)), - (Self::Animation(_), InputProcessableFormat::Apng) => { - Some(Self::Animation(AnimationFormat::Apng)) + (Self::Image(_), InputProcessableFormat::Webp) => Self::Image(ImageFormat::Webp), + (Self::Animation(_) | Self::Image(_), InputProcessableFormat::Apng) => { + Self::Animation(AnimationFormat::Apng) } (Self::Animation(_), InputProcessableFormat::Avif) => { - Some(Self::Animation(AnimationFormat::Avif)) + Self::Animation(AnimationFormat::Avif) } - (Self::Animation(_), InputProcessableFormat::Gif) => { - Some(Self::Animation(AnimationFormat::Gif)) + (Self::Animation(_) | Self::Image(_), InputProcessableFormat::Gif) => { + Self::Animation(AnimationFormat::Gif) } (Self::Animation(_), InputProcessableFormat::Webp) => { - Some(Self::Animation(AnimationFormat::Webp)) + Self::Animation(AnimationFormat::Webp) } - (Self::Image(_), InputProcessableFormat::Apng) => None, - (Self::Image(_), InputProcessableFormat::Gif) => None, } } diff --git a/src/generate.rs b/src/generate.rs index d3303cc..318ee20 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -111,9 +111,7 @@ async fn process( .processable_format() .expect("Already verified format is processable"); - let format = input_format - .process_to(output_format) - .ok_or(UploadError::InvalidProcessExtension)?; + let format = input_format.process_to(output_format); let quality = match format { ProcessableFormat::Image(format) => media.image.quality_for(format), @@ -178,9 +176,7 @@ where { let should_thumbnail = if let Some(input_format) = original_details.internal_format().processable_format() { - let output_format = input_format - .process_to(output_format) - .ok_or(UploadError::InvalidProcessExtension)?; + let output_format = input_format.process_to(output_format); input_format.should_thumbnail(output_format) } else { diff --git a/src/magick.rs b/src/magick.rs index 41a6b91..61113a2 100644 --- a/src/magick.rs +++ b/src/magick.rs @@ -118,14 +118,14 @@ where let quality = quality.map(|q| q.to_string()); let len = 3 - + if format.coalesce() { 1 } else { 0 } + + if input_format.coalesce() { 1 } else { 0 } + if quality.is_some() { 1 } else { 0 } + process_args.len(); let mut args: Vec<&str> = Vec::with_capacity(len); args.push("convert"); args.push(&input_arg); - if format.coalesce() { + if input_format.coalesce() { args.push("-coalesce"); } args.extend(process_args.iter().map(|s| s.as_str()));