2023-07-13 03:12:21 +00:00
|
|
|
use actix_web::web::Bytes;
|
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
|
|
|
|
use crate::{
|
2023-07-13 22:42:21 +00:00
|
|
|
formats::{AnimationFormat, ImageFormat, OutputVideoFormat},
|
2023-07-13 03:12:21 +00:00
|
|
|
magick::MagickError,
|
|
|
|
process::Process,
|
|
|
|
};
|
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
pub(super) async fn convert_image(
|
2023-07-13 03:12:21 +00:00
|
|
|
input: ImageFormat,
|
|
|
|
output: ImageFormat,
|
|
|
|
bytes: Bytes,
|
|
|
|
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
2023-07-14 00:21:57 +00:00
|
|
|
convert(input.magick_format(), output.magick_format(), false, 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-07-13 03:12:21 +00:00
|
|
|
input: AnimationFormat,
|
|
|
|
output: AnimationFormat,
|
|
|
|
bytes: Bytes,
|
|
|
|
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
2023-07-14 00:21:57 +00:00
|
|
|
convert(input.magick_format(), output.magick_format(), true, 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
|
|
|
pub(super) async fn convert_video(
|
2023-07-13 22:42:21 +00:00
|
|
|
input: AnimationFormat,
|
|
|
|
output: OutputVideoFormat,
|
|
|
|
bytes: Bytes,
|
|
|
|
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
2023-07-14 00:21:57 +00:00
|
|
|
convert(input.magick_format(), output.magick_format(), true, bytes).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn convert(
|
|
|
|
input: &'static str,
|
|
|
|
output: &'static str,
|
|
|
|
coalesce: bool,
|
|
|
|
bytes: Bytes,
|
|
|
|
) -> Result<impl AsyncRead + Unpin, MagickError> {
|
|
|
|
let input_file = crate::tmp_file::tmp_file(None);
|
|
|
|
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}:-");
|
|
|
|
|
|
|
|
let process = if coalesce {
|
|
|
|
Process::run(
|
|
|
|
"magick",
|
|
|
|
&[
|
|
|
|
"convert",
|
|
|
|
"-strip",
|
|
|
|
"-auto-orient",
|
|
|
|
&input_arg,
|
|
|
|
"-coalesce",
|
|
|
|
&output_arg,
|
|
|
|
],
|
2023-07-17 18:30:08 +00:00
|
|
|
)?
|
2023-07-14 00:21:57 +00:00
|
|
|
} else {
|
|
|
|
Process::run(
|
|
|
|
"magick",
|
|
|
|
&["convert", "-strip", "-auto-orient", &input_arg, &output_arg],
|
2023-07-17 18:30:08 +00:00
|
|
|
)?
|
2023-07-14 00:21:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let reader = process.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
|
|
|
}
|