2021-09-04 00:53:53 +00:00
|
|
|
use crate::{config::Format, stream::Process};
|
|
|
|
use actix_web::web::Bytes;
|
|
|
|
use tokio::{
|
2021-09-09 19:16:12 +00:00
|
|
|
io::{AsyncRead, AsyncReadExt},
|
2021-09-04 00:53:53 +00:00
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum MagickError {
|
|
|
|
#[error("{0}")]
|
|
|
|
IO(#[from] std::io::Error),
|
|
|
|
|
|
|
|
#[error("Invalid format")]
|
|
|
|
Format,
|
2021-09-09 19:16:12 +00:00
|
|
|
|
|
|
|
#[error("Image too large")]
|
|
|
|
Dimensions,
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 01:37:53 +00:00
|
|
|
pub(crate) enum ValidInputType {
|
|
|
|
Mp4,
|
|
|
|
Gif,
|
2021-08-28 22:15:14 +00:00
|
|
|
Png,
|
2021-08-29 01:37:53 +00:00
|
|
|
Jpeg,
|
2021-08-28 22:15:14 +00:00
|
|
|
Webp,
|
|
|
|
}
|
|
|
|
|
2021-08-29 03:05:49 +00:00
|
|
|
pub(crate) struct Details {
|
|
|
|
pub(crate) mime_type: mime::Mime,
|
|
|
|
pub(crate) width: usize,
|
|
|
|
pub(crate) height: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-04 00:53:53 +00:00
|
|
|
pub(crate) fn clear_metadata_bytes_read(input: Bytes) -> std::io::Result<impl AsyncRead + Unpin> {
|
|
|
|
let process = Process::spawn(Command::new("magick").args(["convert", "-", "-strip", "-"]))?;
|
|
|
|
|
|
|
|
Ok(process.bytes_read(input).unwrap())
|
|
|
|
}
|
|
|
|
|
2021-09-04 17:42:40 +00:00
|
|
|
pub(crate) async fn details_bytes(input: Bytes) -> Result<Details, MagickError> {
|
2021-09-04 00:53:53 +00:00
|
|
|
let process = Process::spawn(Command::new("magick").args([
|
|
|
|
"identify",
|
|
|
|
"-ping",
|
|
|
|
"-format",
|
|
|
|
"%w %h | %m\n",
|
|
|
|
"-",
|
|
|
|
]))?;
|
|
|
|
|
2021-09-04 17:42:40 +00:00
|
|
|
let mut reader = process.bytes_read(input).unwrap();
|
2021-09-04 00:53:53 +00:00
|
|
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
reader.read_to_end(&mut bytes).await?;
|
|
|
|
let s = String::from_utf8_lossy(&bytes);
|
|
|
|
|
2021-09-04 17:42:40 +00:00
|
|
|
parse_details(s)
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
2021-08-29 19:16:55 +00:00
|
|
|
|
2021-09-04 00:53:53 +00:00
|
|
|
pub(crate) fn convert_bytes_read(
|
|
|
|
input: Bytes,
|
|
|
|
format: Format,
|
|
|
|
) -> std::io::Result<impl AsyncRead + Unpin> {
|
|
|
|
let process = Process::spawn(Command::new("magick").args([
|
|
|
|
"convert",
|
|
|
|
"-",
|
|
|
|
format!("{}:-", format.to_magick_format()).as_str(),
|
|
|
|
]))?;
|
|
|
|
|
|
|
|
Ok(process.bytes_read(input).unwrap())
|
|
|
|
}
|
|
|
|
|
2021-08-29 03:05:49 +00:00
|
|
|
pub(crate) async fn details<P>(file: P) -> Result<Details, MagickError>
|
|
|
|
where
|
|
|
|
P: AsRef<std::path::Path>,
|
|
|
|
{
|
2021-09-04 00:53:53 +00:00
|
|
|
let output = Command::new("magick")
|
2021-08-29 03:05:49 +00:00
|
|
|
.args([&"identify", &"-ping", &"-format", &"%w %h | %m\n"])
|
|
|
|
.arg(&file.as_ref())
|
|
|
|
.output()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let s = String::from_utf8_lossy(&output.stdout);
|
|
|
|
|
2021-08-31 02:19:47 +00:00
|
|
|
parse_details(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_details(s: std::borrow::Cow<'_, str>) -> Result<Details, MagickError> {
|
2021-08-29 03:05:49 +00:00
|
|
|
let mut lines = s.lines();
|
2021-09-12 15:42:44 +00:00
|
|
|
let first = lines.next().ok_or(MagickError::Format)?;
|
2021-08-29 03:05:49 +00:00
|
|
|
|
|
|
|
let mut segments = first.split('|');
|
|
|
|
|
2021-09-12 15:42:44 +00:00
|
|
|
let dimensions = segments.next().ok_or(MagickError::Format)?.trim();
|
2021-08-29 03:05:49 +00:00
|
|
|
tracing::debug!("dimensions: {}", dimensions);
|
|
|
|
let mut dims = dimensions.split(' ');
|
2021-09-12 15:42:44 +00:00
|
|
|
let width = dims.next().ok_or(MagickError::Format)?.trim().parse()?;
|
|
|
|
let height = dims.next().ok_or(MagickError::Format)?.trim().parse()?;
|
|
|
|
|
|
|
|
let format = segments.next().ok_or(MagickError::Format)?.trim();
|
2021-08-29 03:05:49 +00:00
|
|
|
tracing::debug!("format: {}", format);
|
|
|
|
|
|
|
|
if !lines.all(|item| item.ends_with(format)) {
|
|
|
|
return Err(MagickError::Format);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mime_type = match format {
|
|
|
|
"MP4" => crate::validate::video_mp4(),
|
|
|
|
"GIF" => mime::IMAGE_GIF,
|
|
|
|
"PNG" => mime::IMAGE_PNG,
|
|
|
|
"JPEG" => mime::IMAGE_JPEG,
|
|
|
|
"WEBP" => crate::validate::image_webp(),
|
|
|
|
_ => return Err(MagickError::Format),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Details {
|
|
|
|
mime_type,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:16:12 +00:00
|
|
|
pub(crate) async fn input_type_bytes(input: Bytes) -> Result<ValidInputType, MagickError> {
|
|
|
|
details_bytes(input).await?.validate_input()
|
2021-08-28 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 17:42:40 +00:00
|
|
|
pub(crate) fn process_image_write_read(
|
|
|
|
input: impl AsyncRead + Unpin + 'static,
|
2021-08-28 22:15:14 +00:00
|
|
|
args: Vec<String>,
|
2021-09-04 00:53:53 +00:00
|
|
|
format: Format,
|
2021-09-04 17:42:40 +00:00
|
|
|
) -> std::io::Result<impl AsyncRead + Unpin> {
|
2021-09-04 00:53:53 +00:00
|
|
|
let process = Process::spawn(
|
|
|
|
Command::new("magick")
|
2021-08-31 02:19:47 +00:00
|
|
|
.args([&"convert", &"-"])
|
|
|
|
.args(args)
|
|
|
|
.arg(format!("{}:-", format.to_magick_format())),
|
|
|
|
)?;
|
|
|
|
|
2021-09-04 17:42:40 +00:00
|
|
|
Ok(process.write_read(input).unwrap())
|
2021-08-26 02:46:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 19:16:12 +00:00
|
|
|
impl Details {
|
|
|
|
fn validate_input(&self) -> Result<ValidInputType, MagickError> {
|
|
|
|
if self.width > crate::CONFIG.max_width() || self.height > crate::CONFIG.max_height() {
|
|
|
|
return Err(MagickError::Dimensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
let input_type = match (self.mime_type.type_(), self.mime_type.subtype()) {
|
|
|
|
(mime::VIDEO, mime::MP4 | mime::MPEG) => ValidInputType::Mp4,
|
|
|
|
(mime::IMAGE, mime::GIF) => ValidInputType::Gif,
|
|
|
|
(mime::IMAGE, mime::PNG) => ValidInputType::Png,
|
|
|
|
(mime::IMAGE, mime::JPEG) => ValidInputType::Jpeg,
|
|
|
|
(mime::IMAGE, subtype) if subtype.as_str() == "webp" => ValidInputType::Webp,
|
|
|
|
_ => return Err(MagickError::Format),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(input_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 03:05:49 +00:00
|
|
|
impl From<std::num::ParseIntError> for MagickError {
|
|
|
|
fn from(_: std::num::ParseIntError) -> MagickError {
|
|
|
|
MagickError::Format
|
|
|
|
}
|
|
|
|
}
|