From eeac900d7e47e6a27cd301382cb3d67e3b48f59b Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 18 Jul 2023 16:18:01 -0500 Subject: [PATCH] Add quality value to process method, move -coalesce earlier in pipelines --- src/generate.rs | 9 ++++++++ src/ingest.rs | 10 ++++++++- src/lib.rs | 1 + src/magick.rs | 49 +++++++++++++++++++++++++++++------------- src/queue/process.rs | 3 +++ src/validate/magick.rs | 12 ++++++----- 6 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/generate.rs b/src/generate.rs index 0f2d51b..4980911 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -23,6 +23,7 @@ pub(crate) async fn generate( thumbnail_args: Vec, input_format: Option, thumbnail_format: Option, + media: &'static crate::config::Media, hash: R::Bytes, ) -> Result<(Details, Bytes), Error> { let process_fut = process( @@ -34,6 +35,7 @@ pub(crate) async fn generate( thumbnail_args, input_format, thumbnail_format, + media, hash.clone(), ); @@ -54,6 +56,7 @@ async fn process( thumbnail_args: Vec, input_format: Option, thumbnail_format: Option, + media: &'static crate::config::Media, hash: R::Bytes, ) -> Result<(Details, Bytes), Error> { let permit = crate::PROCESS_SEMAPHORE.acquire().await; @@ -107,12 +110,18 @@ async fn process( return Err(UploadError::InvalidProcessExtension.into()); }; + let quality = match format { + crate::formats::ProcessableFormat::Image(format) => media.image.quality_for(format), + crate::formats::ProcessableFormat::Animation(format) => media.animation.quality_for(format), + }; + let mut processed_reader = crate::magick::process_image_store_read( store, &identifier, thumbnail_args, input_format, format, + quality, ) .await?; diff --git a/src/ingest.rs b/src/ingest.rs index 714cd86..32253b1 100644 --- a/src/ingest.rs +++ b/src/ingest.rs @@ -46,7 +46,7 @@ pub(crate) async fn ingest( store: &S, stream: impl Stream> + Unpin + 'static, declared_alias: Option, - media: &crate::config::Media, + media: &'static crate::config::Media, ) -> Result, Error> where R: FullRepo + 'static, @@ -70,11 +70,19 @@ where let (_, magick_args) = crate::processor::build_chain(operations, format.file_extension())?; + let quality = match format { + crate::formats::ProcessableFormat::Image(format) => media.image.quality_for(format), + crate::formats::ProcessableFormat::Animation(format) => { + media.animation.quality_for(format) + } + }; + let processed_reader = crate::magick::process_image_async_read( validated_reader, magick_args, format, format, + quality, ) .await?; diff --git a/src/lib.rs b/src/lib.rs index 6b3a560..dacc8a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -728,6 +728,7 @@ async fn process( thumbnail_args, original_details.video_format(), None, + &CONFIG.media, hash, ) .await?; diff --git a/src/magick.rs b/src/magick.rs index 1931b9a..bc2aaa3 100644 --- a/src/magick.rs +++ b/src/magick.rs @@ -67,6 +67,7 @@ async fn process_image( process_args: Vec, input_format: ProcessableFormat, format: ProcessableFormat, + quality: Option, write_file: F, ) -> Result where @@ -87,6 +88,7 @@ where let input_arg = format!("{}:{input_file_str}", input_format.magick_format()); let output_arg = format!("{}:-", format.magick_format()); + let quality = quality.map(|q| q.to_string()); let len = if format.coalesce() { process_args.len() + 4 @@ -97,10 +99,13 @@ where let mut args: Vec<&str> = Vec::with_capacity(len); args.push("convert"); args.push(&input_arg); - args.extend(process_args.iter().map(|s| s.as_str())); if format.coalesce() { args.push("-coalesce"); } + args.extend(process_args.iter().map(|s| s.as_str())); + if let Some(quality) = &quality { + args.extend(["-quality", quality]); + } args.push(&output_arg); let reader = Process::run("magick", &args)?.read(); @@ -116,19 +121,26 @@ pub(crate) async fn process_image_store_read( args: Vec, input_format: ProcessableFormat, format: ProcessableFormat, + quality: Option, ) -> Result { let stream = store .to_stream(identifier, None, None) .await .map_err(MagickError::Store)?; - process_image(args, input_format, format, |mut tmp_file| async move { - tmp_file - .write_from_stream(stream) - .await - .map_err(MagickError::Write)?; - Ok(tmp_file) - }) + process_image( + args, + input_format, + format, + quality, + |mut tmp_file| async move { + tmp_file + .write_from_stream(stream) + .await + .map_err(MagickError::Write)?; + Ok(tmp_file) + }, + ) .await } @@ -137,13 +149,20 @@ pub(crate) async fn process_image_async_read( args: Vec, input_format: ProcessableFormat, format: ProcessableFormat, + quality: Option, ) -> Result { - process_image(args, input_format, format, |mut tmp_file| async move { - tmp_file - .write_from_async_read(async_read) - .await - .map_err(MagickError::Write)?; - Ok(tmp_file) - }) + process_image( + args, + input_format, + format, + quality, + |mut tmp_file| async move { + tmp_file + .write_from_async_read(async_read) + .await + .map_err(MagickError::Write)?; + Ok(tmp_file) + }, + ) .await } diff --git a/src/queue/process.rs b/src/queue/process.rs index 2e22f1d..60c729c 100644 --- a/src/queue/process.rs +++ b/src/queue/process.rs @@ -51,6 +51,7 @@ where Serde::into_inner(source), process_path, process_args, + &CONFIG.media, ) .await? } @@ -133,6 +134,7 @@ async fn generate( source: Alias, process_path: PathBuf, process_args: Vec, + meida: &'static crate::config::Media, ) -> Result<(), Error> { let Some(hash) = repo.hash(&source).await? else { // Nothing to do @@ -159,6 +161,7 @@ async fn generate( process_args, original_details.video_format(), None, + meida, hash, ) .await?; diff --git a/src/validate/magick.rs b/src/validate/magick.rs index 83e4015..89c7bad 100644 --- a/src/validate/magick.rs +++ b/src/validate/magick.rs @@ -81,16 +81,18 @@ async fn convert( let output_arg = format!("{output}:-"); let quality = quality.map(|q| q.to_string()); - let mut args = vec!["convert", "-strip", "-auto-orient", &input_arg]; - - if let Some(quality) = &quality { - args.extend(["-quality", quality]); - } + let mut args = vec!["convert"]; if coalesce { args.push("-coalesce"); } + args.extend(["-strip", "-auto-orient", &input_arg]); + + if let Some(quality) = &quality { + args.extend(["-quality", quality]); + } + args.push(&output_arg); let reader = Process::run("magick", &args)?.read();