mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-11-20 11:21:14 +00:00
Add quality value to process method, move -coalesce earlier in pipelines
This commit is contained in:
parent
558605381d
commit
eeac900d7e
6 changed files with 63 additions and 21 deletions
|
@ -23,6 +23,7 @@ pub(crate) async fn generate<R: FullRepo, S: Store + 'static>(
|
|||
thumbnail_args: Vec<String>,
|
||||
input_format: Option<InternalVideoFormat>,
|
||||
thumbnail_format: Option<ThumbnailFormat>,
|
||||
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<R: FullRepo, S: Store + 'static>(
|
|||
thumbnail_args,
|
||||
input_format,
|
||||
thumbnail_format,
|
||||
media,
|
||||
hash.clone(),
|
||||
);
|
||||
|
||||
|
@ -54,6 +56,7 @@ async fn process<R: FullRepo, S: Store + 'static>(
|
|||
thumbnail_args: Vec<String>,
|
||||
input_format: Option<InternalVideoFormat>,
|
||||
thumbnail_format: Option<ThumbnailFormat>,
|
||||
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<R: FullRepo, S: Store + 'static>(
|
|||
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?;
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ pub(crate) async fn ingest<R, S>(
|
|||
store: &S,
|
||||
stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + 'static,
|
||||
declared_alias: Option<Alias>,
|
||||
media: &crate::config::Media,
|
||||
media: &'static crate::config::Media,
|
||||
) -> Result<Session<R, S>, 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?;
|
||||
|
||||
|
|
|
@ -728,6 +728,7 @@ async fn process<R: FullRepo, S: Store + 'static>(
|
|||
thumbnail_args,
|
||||
original_details.video_format(),
|
||||
None,
|
||||
&CONFIG.media,
|
||||
hash,
|
||||
)
|
||||
.await?;
|
||||
|
|
|
@ -67,6 +67,7 @@ async fn process_image<F, Fut>(
|
|||
process_args: Vec<String>,
|
||||
input_format: ProcessableFormat,
|
||||
format: ProcessableFormat,
|
||||
quality: Option<u8>,
|
||||
write_file: F,
|
||||
) -> Result<impl AsyncRead + Unpin, MagickError>
|
||||
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<S: Store + 'static>(
|
|||
args: Vec<String>,
|
||||
input_format: ProcessableFormat,
|
||||
format: ProcessableFormat,
|
||||
quality: Option<u8>,
|
||||
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
||||
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<A: AsyncRead + Unpin + 'static>(
|
|||
args: Vec<String>,
|
||||
input_format: ProcessableFormat,
|
||||
format: ProcessableFormat,
|
||||
quality: Option<u8>,
|
||||
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ where
|
|||
Serde::into_inner(source),
|
||||
process_path,
|
||||
process_args,
|
||||
&CONFIG.media,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
|
@ -133,6 +134,7 @@ async fn generate<R: FullRepo, S: Store + 'static>(
|
|||
source: Alias,
|
||||
process_path: PathBuf,
|
||||
process_args: Vec<String>,
|
||||
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<R: FullRepo, S: Store + 'static>(
|
|||
process_args,
|
||||
original_details.video_format(),
|
||||
None,
|
||||
meida,
|
||||
hash,
|
||||
)
|
||||
.await?;
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue