2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Add quality value to process method, move -coalesce earlier in pipelines

This commit is contained in:
asonix 2023-07-18 16:18:01 -05:00
parent 558605381d
commit eeac900d7e
6 changed files with 63 additions and 21 deletions

View file

@ -23,6 +23,7 @@ pub(crate) async fn generate<R: FullRepo, S: Store + 'static>(
thumbnail_args: Vec<String>, thumbnail_args: Vec<String>,
input_format: Option<InternalVideoFormat>, input_format: Option<InternalVideoFormat>,
thumbnail_format: Option<ThumbnailFormat>, thumbnail_format: Option<ThumbnailFormat>,
media: &'static crate::config::Media,
hash: R::Bytes, hash: R::Bytes,
) -> Result<(Details, Bytes), Error> { ) -> Result<(Details, Bytes), Error> {
let process_fut = process( let process_fut = process(
@ -34,6 +35,7 @@ pub(crate) async fn generate<R: FullRepo, S: Store + 'static>(
thumbnail_args, thumbnail_args,
input_format, input_format,
thumbnail_format, thumbnail_format,
media,
hash.clone(), hash.clone(),
); );
@ -54,6 +56,7 @@ async fn process<R: FullRepo, S: Store + 'static>(
thumbnail_args: Vec<String>, thumbnail_args: Vec<String>,
input_format: Option<InternalVideoFormat>, input_format: Option<InternalVideoFormat>,
thumbnail_format: Option<ThumbnailFormat>, thumbnail_format: Option<ThumbnailFormat>,
media: &'static crate::config::Media,
hash: R::Bytes, hash: R::Bytes,
) -> Result<(Details, Bytes), Error> { ) -> Result<(Details, Bytes), Error> {
let permit = crate::PROCESS_SEMAPHORE.acquire().await; let permit = crate::PROCESS_SEMAPHORE.acquire().await;
@ -107,12 +110,18 @@ async fn process<R: FullRepo, S: Store + 'static>(
return Err(UploadError::InvalidProcessExtension.into()); 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( let mut processed_reader = crate::magick::process_image_store_read(
store, store,
&identifier, &identifier,
thumbnail_args, thumbnail_args,
input_format, input_format,
format, format,
quality,
) )
.await?; .await?;

View file

@ -46,7 +46,7 @@ pub(crate) async fn ingest<R, S>(
store: &S, store: &S,
stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + 'static, stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
declared_alias: Option<Alias>, declared_alias: Option<Alias>,
media: &crate::config::Media, media: &'static crate::config::Media,
) -> Result<Session<R, S>, Error> ) -> Result<Session<R, S>, Error>
where where
R: FullRepo + 'static, R: FullRepo + 'static,
@ -70,11 +70,19 @@ where
let (_, magick_args) = let (_, magick_args) =
crate::processor::build_chain(operations, format.file_extension())?; 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( let processed_reader = crate::magick::process_image_async_read(
validated_reader, validated_reader,
magick_args, magick_args,
format, format,
format, format,
quality,
) )
.await?; .await?;

View file

@ -728,6 +728,7 @@ async fn process<R: FullRepo, S: Store + 'static>(
thumbnail_args, thumbnail_args,
original_details.video_format(), original_details.video_format(),
None, None,
&CONFIG.media,
hash, hash,
) )
.await?; .await?;

View file

@ -67,6 +67,7 @@ async fn process_image<F, Fut>(
process_args: Vec<String>, process_args: Vec<String>,
input_format: ProcessableFormat, input_format: ProcessableFormat,
format: ProcessableFormat, format: ProcessableFormat,
quality: Option<u8>,
write_file: F, write_file: F,
) -> Result<impl AsyncRead + Unpin, MagickError> ) -> Result<impl AsyncRead + Unpin, MagickError>
where where
@ -87,6 +88,7 @@ where
let input_arg = format!("{}:{input_file_str}", input_format.magick_format()); let input_arg = format!("{}:{input_file_str}", input_format.magick_format());
let output_arg = format!("{}:-", format.magick_format()); let output_arg = format!("{}:-", format.magick_format());
let quality = quality.map(|q| q.to_string());
let len = if format.coalesce() { let len = if format.coalesce() {
process_args.len() + 4 process_args.len() + 4
@ -97,10 +99,13 @@ where
let mut args: Vec<&str> = Vec::with_capacity(len); let mut args: Vec<&str> = Vec::with_capacity(len);
args.push("convert"); args.push("convert");
args.push(&input_arg); args.push(&input_arg);
args.extend(process_args.iter().map(|s| s.as_str()));
if format.coalesce() { if format.coalesce() {
args.push("-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); args.push(&output_arg);
let reader = Process::run("magick", &args)?.read(); let reader = Process::run("magick", &args)?.read();
@ -116,19 +121,26 @@ pub(crate) async fn process_image_store_read<S: Store + 'static>(
args: Vec<String>, args: Vec<String>,
input_format: ProcessableFormat, input_format: ProcessableFormat,
format: ProcessableFormat, format: ProcessableFormat,
quality: Option<u8>,
) -> Result<impl AsyncRead + Unpin, MagickError> { ) -> Result<impl AsyncRead + Unpin, MagickError> {
let stream = store let stream = store
.to_stream(identifier, None, None) .to_stream(identifier, None, None)
.await .await
.map_err(MagickError::Store)?; .map_err(MagickError::Store)?;
process_image(args, input_format, format, |mut tmp_file| async move { process_image(
tmp_file args,
.write_from_stream(stream) input_format,
.await format,
.map_err(MagickError::Write)?; quality,
Ok(tmp_file) |mut tmp_file| async move {
}) tmp_file
.write_from_stream(stream)
.await
.map_err(MagickError::Write)?;
Ok(tmp_file)
},
)
.await .await
} }
@ -137,13 +149,20 @@ pub(crate) async fn process_image_async_read<A: AsyncRead + Unpin + 'static>(
args: Vec<String>, args: Vec<String>,
input_format: ProcessableFormat, input_format: ProcessableFormat,
format: ProcessableFormat, format: ProcessableFormat,
quality: Option<u8>,
) -> Result<impl AsyncRead + Unpin, MagickError> { ) -> Result<impl AsyncRead + Unpin, MagickError> {
process_image(args, input_format, format, |mut tmp_file| async move { process_image(
tmp_file args,
.write_from_async_read(async_read) input_format,
.await format,
.map_err(MagickError::Write)?; quality,
Ok(tmp_file) |mut tmp_file| async move {
}) tmp_file
.write_from_async_read(async_read)
.await
.map_err(MagickError::Write)?;
Ok(tmp_file)
},
)
.await .await
} }

View file

@ -51,6 +51,7 @@ where
Serde::into_inner(source), Serde::into_inner(source),
process_path, process_path,
process_args, process_args,
&CONFIG.media,
) )
.await? .await?
} }
@ -133,6 +134,7 @@ async fn generate<R: FullRepo, S: Store + 'static>(
source: Alias, source: Alias,
process_path: PathBuf, process_path: PathBuf,
process_args: Vec<String>, process_args: Vec<String>,
meida: &'static crate::config::Media,
) -> Result<(), Error> { ) -> Result<(), Error> {
let Some(hash) = repo.hash(&source).await? else { let Some(hash) = repo.hash(&source).await? else {
// Nothing to do // Nothing to do
@ -159,6 +161,7 @@ async fn generate<R: FullRepo, S: Store + 'static>(
process_args, process_args,
original_details.video_format(), original_details.video_format(),
None, None,
meida,
hash, hash,
) )
.await?; .await?;

View file

@ -81,16 +81,18 @@ async fn convert(
let output_arg = format!("{output}:-"); let output_arg = format!("{output}:-");
let quality = quality.map(|q| q.to_string()); let quality = quality.map(|q| q.to_string());
let mut args = vec!["convert", "-strip", "-auto-orient", &input_arg]; let mut args = vec!["convert"];
if let Some(quality) = &quality {
args.extend(["-quality", quality]);
}
if coalesce { if coalesce {
args.push("-coalesce"); args.push("-coalesce");
} }
args.extend(["-strip", "-auto-orient", &input_arg]);
if let Some(quality) = &quality {
args.extend(["-quality", quality]);
}
args.push(&output_arg); args.push(&output_arg);
let reader = Process::run("magick", &args)?.read(); let reader = Process::run("magick", &args)?.read();