2023-11-10 00:20:59 +00:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
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},
|
2024-02-04 00:19:14 +00:00
|
|
|
magick::{MagickError, MAGICK_CONFIGURE_PATH, MAGICK_TEMPORARY_PATH},
|
2023-12-23 02:52:58 +00:00
|
|
|
process::{Process, ProcessRead},
|
2024-02-04 00:18:13 +00:00
|
|
|
state::State,
|
2023-07-13 03:12:21 +00:00
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
pub(super) async fn convert_image<S>(
|
|
|
|
state: &State<S>,
|
2023-07-13 03:12:21 +00:00
|
|
|
input: ImageFormat,
|
|
|
|
output: ImageFormat,
|
2023-07-17 22:45:26 +00:00
|
|
|
quality: Option<u8>,
|
2023-07-13 03:12:21 +00:00
|
|
|
bytes: Bytes,
|
2023-12-23 02:52:58 +00:00
|
|
|
) -> Result<ProcessRead, MagickError> {
|
2023-07-17 22:45:26 +00:00
|
|
|
convert(
|
2024-02-03 19:31:54 +00:00
|
|
|
state,
|
2023-07-17 22:45:26 +00:00
|
|
|
input.magick_format(),
|
|
|
|
output.magick_format(),
|
|
|
|
false,
|
|
|
|
quality,
|
|
|
|
bytes,
|
|
|
|
)
|
|
|
|
.await
|
2023-07-13 03:12:21 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
pub(super) async fn convert_animation<S>(
|
|
|
|
state: &State<S>,
|
2023-07-13 03:12:21 +00:00
|
|
|
input: AnimationFormat,
|
|
|
|
output: AnimationFormat,
|
2023-07-17 22:45:26 +00:00
|
|
|
quality: Option<u8>,
|
2023-07-13 03:12:21 +00:00
|
|
|
bytes: Bytes,
|
2023-12-23 02:52:58 +00:00
|
|
|
) -> Result<ProcessRead, MagickError> {
|
2023-07-17 22:45:26 +00:00
|
|
|
convert(
|
2024-02-03 19:31:54 +00:00
|
|
|
state,
|
2023-07-17 22:45:26 +00:00
|
|
|
input.magick_format(),
|
|
|
|
output.magick_format(),
|
|
|
|
true,
|
|
|
|
quality,
|
2024-02-04 00:18:13 +00:00
|
|
|
bytes,
|
2023-07-17 22:45:26 +00:00
|
|
|
)
|
|
|
|
.await
|
2023-07-13 03:12:21 +00:00
|
|
|
}
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
async fn convert<S>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
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-07-14 00:21:57 +00:00
|
|
|
bytes: Bytes,
|
2023-12-23 02:52:58 +00:00
|
|
|
) -> Result<ProcessRead, MagickError> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let temporary_path = state
|
|
|
|
.tmp_dir
|
2023-11-10 00:20:59 +00:00
|
|
|
.tmp_folder()
|
|
|
|
.await
|
|
|
|
.map_err(MagickError::CreateTemporaryDirectory)?;
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let input_file = state.tmp_dir.tmp_file(None);
|
2023-07-14 00:21:57 +00:00
|
|
|
|
|
|
|
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)?;
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
let input_arg = [input.as_ref(), input_file.as_os_str()].join(":".as_ref());
|
2023-07-14 00:21:57 +00:00
|
|
|
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-11-10 00:20:59 +00:00
|
|
|
let mut args: Vec<&OsStr> = vec!["convert".as_ref()];
|
2023-07-17 23:13:41 +00:00
|
|
|
|
|
|
|
if coalesce {
|
2023-11-10 00:20:59 +00:00
|
|
|
args.push("-coalesce".as_ref());
|
2023-07-17 23:13:41 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
args.extend(["-strip".as_ref(), "-auto-orient".as_ref(), &input_arg] as [&OsStr; 3]);
|
2023-07-18 21:18:01 +00:00
|
|
|
|
|
|
|
if let Some(quality) = &quality {
|
2023-11-10 00:20:59 +00:00
|
|
|
args.extend(["-quality".as_ref(), quality.as_ref()] as [&OsStr; 2]);
|
2023-07-18 21:18:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 00:20:59 +00:00
|
|
|
args.push(output_arg.as_ref());
|
|
|
|
|
2024-02-01 03:32:01 +00:00
|
|
|
let envs = [
|
|
|
|
(MAGICK_TEMPORARY_PATH, temporary_path.as_os_str()),
|
2024-02-03 19:31:54 +00:00
|
|
|
(MAGICK_CONFIGURE_PATH, state.policy_dir.as_os_str()),
|
2024-02-01 03:32:01 +00:00
|
|
|
];
|
2023-07-17 23:13:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let reader = Process::run("magick", &args, &envs, state.config.media.process_timeout)?.read();
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2023-12-23 02:52:58 +00:00
|
|
|
let clean_reader = reader.add_extras(input_file).add_extras(temporary_path);
|
2023-07-13 22:42:21 +00:00
|
|
|
|
2023-12-23 02:52:58 +00:00
|
|
|
Ok(clean_reader)
|
2023-07-13 22:42:21 +00:00
|
|
|
}
|