2023-07-13 03:12:21 +00:00
|
|
|
use actix_web::web::Bytes;
|
|
|
|
|
|
|
|
use crate::{
|
2023-08-31 01:37:54 +00:00
|
|
|
formats::{AnimationFormat, ImageFormat},
|
2023-07-13 03:12:21 +00:00
|
|
|
magick::MagickError,
|
|
|
|
process::Process,
|
2023-09-24 16:54:16 +00:00
|
|
|
read::BoxRead,
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_file::TmpDir,
|
2023-07-13 03:12:21 +00:00
|
|
|
};
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
pub(super) async fn convert_image(
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_dir: &TmpDir,
|
2023-07-13 03:12:21 +00:00
|
|
|
input: ImageFormat,
|
|
|
|
output: ImageFormat,
|
2023-07-17 22:45:26 +00:00
|
|
|
quality: Option<u8>,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: u64,
|
2023-07-13 03:12:21 +00:00
|
|
|
bytes: Bytes,
|
2023-09-24 16:54:16 +00:00
|
|
|
) -> Result<BoxRead<'static>, MagickError> {
|
2023-07-17 22:45:26 +00:00
|
|
|
convert(
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_dir,
|
2023-07-17 22:45:26 +00:00
|
|
|
input.magick_format(),
|
|
|
|
output.magick_format(),
|
|
|
|
false,
|
|
|
|
quality,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout,
|
2023-07-17 22:45:26 +00:00
|
|
|
bytes,
|
|
|
|
)
|
|
|
|
.await
|
2023-07-13 03:12:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
pub(super) async fn convert_animation(
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_dir: &TmpDir,
|
2023-07-13 03:12:21 +00:00
|
|
|
input: AnimationFormat,
|
|
|
|
output: AnimationFormat,
|
2023-07-17 22:45:26 +00:00
|
|
|
quality: Option<u8>,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: u64,
|
2023-07-13 03:12:21 +00:00
|
|
|
bytes: Bytes,
|
2023-09-24 16:54:16 +00:00
|
|
|
) -> Result<BoxRead<'static>, MagickError> {
|
2023-07-17 22:45:26 +00:00
|
|
|
convert(
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_dir,
|
2023-07-17 22:45:26 +00:00
|
|
|
input.magick_format(),
|
|
|
|
output.magick_format(),
|
|
|
|
true,
|
|
|
|
quality,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout,
|
2023-07-17 22:45:26 +00:00
|
|
|
bytes,
|
|
|
|
)
|
|
|
|
.await
|
2023-07-13 03:12:21 +00:00
|
|
|
}
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
async fn convert(
|
2023-10-07 00:42:24 +00:00
|
|
|
tmp_dir: &TmpDir,
|
2023-07-14 00:21:57 +00:00
|
|
|
input: &'static str,
|
|
|
|
output: &'static str,
|
|
|
|
coalesce: bool,
|
2023-07-17 22:45:26 +00:00
|
|
|
quality: Option<u8>,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: u64,
|
2023-07-14 00:21:57 +00:00
|
|
|
bytes: Bytes,
|
2023-09-24 16:54:16 +00:00
|
|
|
) -> Result<BoxRead<'static>, MagickError> {
|
2023-10-07 00:42:24 +00:00
|
|
|
let input_file = tmp_dir.tmp_file(None);
|
2023-07-14 00:21:57 +00:00
|
|
|
let input_file_str = input_file.to_str().ok_or(MagickError::Path)?;
|
|
|
|
|
|
|
|
crate::store::file_store::safe_create_parent(&input_file)
|
|
|
|
.await
|
|
|
|
.map_err(MagickError::CreateDir)?;
|
|
|
|
|
|
|
|
let mut tmp_one = crate::file::File::create(&input_file)
|
|
|
|
.await
|
|
|
|
.map_err(MagickError::CreateFile)?;
|
|
|
|
tmp_one
|
|
|
|
.write_from_bytes(bytes)
|
|
|
|
.await
|
|
|
|
.map_err(MagickError::Write)?;
|
|
|
|
tmp_one.close().await.map_err(MagickError::CloseFile)?;
|
|
|
|
|
|
|
|
let input_arg = format!("{input}:{input_file_str}");
|
|
|
|
let output_arg = format!("{output}:-");
|
2023-07-17 23:13:41 +00:00
|
|
|
let quality = quality.map(|q| q.to_string());
|
2023-07-14 00:21:57 +00:00
|
|
|
|
2023-07-18 21:18:01 +00:00
|
|
|
let mut args = vec!["convert"];
|
2023-07-17 23:13:41 +00:00
|
|
|
|
|
|
|
if coalesce {
|
|
|
|
args.push("-coalesce");
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:18:01 +00:00
|
|
|
args.extend(["-strip", "-auto-orient", &input_arg]);
|
|
|
|
|
|
|
|
if let Some(quality) = &quality {
|
|
|
|
args.extend(["-quality", quality]);
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:13:41 +00:00
|
|
|
args.push(&output_arg);
|
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
let reader = Process::run("magick", &args, timeout)?.read();
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
let clean_reader = crate::tmp_file::cleanup_tmpfile(reader, input_file);
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
Ok(Box::pin(clean_reader))
|
2023-07-13 22:42:21 +00:00
|
|
|
}
|