2021-10-21 02:36:18 +00:00
|
|
|
use crate::error::{Error, UploadError};
|
|
|
|
use std::path::PathBuf;
|
2020-06-07 17:51:45 +00:00
|
|
|
|
|
|
|
pub(crate) trait Processor {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str;
|
2020-06-07 19:12:19 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(k: &str, v: &str) -> Option<Self>
|
2020-06-07 19:12:19 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
|
2020-06-07 17:51:45 +00:00
|
|
|
fn path(&self, path: PathBuf) -> PathBuf;
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, args: Vec<String>) -> Vec<String>;
|
2020-06-07 17:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Identity;
|
2021-10-23 04:48:56 +00:00
|
|
|
pub(crate) struct Thumbnail(usize);
|
2022-09-26 23:14:25 +00:00
|
|
|
pub(crate) struct Resize {
|
|
|
|
filter: Option<ResizeFilter>,
|
|
|
|
kind: ResizeKind,
|
|
|
|
}
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub(crate) enum ResizeFilter {
|
|
|
|
Lanczos,
|
|
|
|
LanczosSharp,
|
|
|
|
Lanczos2,
|
|
|
|
Lanczos2Sharp,
|
|
|
|
Mitchell,
|
|
|
|
RobidouxSharp,
|
|
|
|
}
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub(crate) enum ResizeKind {
|
|
|
|
Bounds(usize),
|
|
|
|
Area(usize),
|
|
|
|
}
|
2021-10-23 04:48:56 +00:00
|
|
|
pub(crate) struct Crop(usize, usize);
|
|
|
|
pub(crate) struct Blur(f64);
|
|
|
|
|
2022-09-26 23:14:25 +00:00
|
|
|
impl ResizeFilter {
|
|
|
|
fn from_str(s: &str) -> Option<Self> {
|
|
|
|
match s.to_lowercase().as_str() {
|
|
|
|
"lanczos" => Some(Self::Lanczos),
|
|
|
|
"lanczossharp" => Some(Self::LanczosSharp),
|
|
|
|
"lanczos2" => Some(Self::Lanczos2),
|
|
|
|
"lanczos2sharp" => Some(Self::Lanczos2Sharp),
|
|
|
|
"mitchell" => Some(Self::Mitchell),
|
|
|
|
"robidouxsharp" => Some(Self::RobidouxSharp),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_magick_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Lanczos => "Lanczos",
|
|
|
|
Self::LanczosSharp => "LanczosSharp",
|
|
|
|
Self::Lanczos2 => "Lanczos2",
|
|
|
|
Self::Lanczos2Sharp => "Lanczos2Sharp",
|
|
|
|
Self::Mitchell => "Mitchell",
|
|
|
|
Self::RobidouxSharp => "RobidouxSharp",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ResizeFilter {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Lanczos2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResizeKind {
|
|
|
|
fn from_str(s: &str) -> Option<Self> {
|
|
|
|
let kind = if s.starts_with('a') {
|
|
|
|
let size = s.trim_start_matches('a').parse().ok()?;
|
|
|
|
ResizeKind::Area(size)
|
|
|
|
} else {
|
|
|
|
let size = s.parse().ok()?;
|
|
|
|
ResizeKind::Bounds(size)
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_magick_string(self) -> String {
|
|
|
|
match self {
|
2023-01-29 17:57:59 +00:00
|
|
|
Self::Area(size) => format!("{size}@>"),
|
|
|
|
Self::Bounds(size) => format!("{size}x{size}>"),
|
2022-09-26 23:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:17:18 +00:00
|
|
|
#[tracing::instrument(level = "debug")]
|
2022-03-28 20:34:36 +00:00
|
|
|
pub(crate) fn build_chain(
|
|
|
|
args: &[(String, String)],
|
|
|
|
ext: &str,
|
|
|
|
) -> Result<(PathBuf, Vec<String>), Error> {
|
2022-03-29 01:47:46 +00:00
|
|
|
fn parse<P: Processor>(key: &str, value: &str) -> Result<Option<P>, Error> {
|
2021-10-23 04:48:56 +00:00
|
|
|
if key == P::NAME {
|
|
|
|
return Ok(Some(P::parse(key, value).ok_or(UploadError::ParsePath)?));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! parse {
|
|
|
|
($inner:expr, $x:ident, $k:expr, $v:expr) => {{
|
|
|
|
if let Some(processor) = parse::<$x>($k, $v)? {
|
|
|
|
return Ok((processor.path($inner.0), processor.command($inner.1)));
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:34:36 +00:00
|
|
|
let (mut path, args) =
|
2021-10-28 04:06:03 +00:00
|
|
|
args.iter()
|
2021-10-23 04:48:56 +00:00
|
|
|
.fold(Ok((PathBuf::default(), vec![])), |inner, (name, value)| {
|
|
|
|
if let Ok(inner) = inner {
|
|
|
|
parse!(inner, Identity, name, value);
|
|
|
|
parse!(inner, Thumbnail, name, value);
|
|
|
|
parse!(inner, Resize, name, value);
|
|
|
|
parse!(inner, Crop, name, value);
|
|
|
|
parse!(inner, Blur, name, value);
|
|
|
|
|
|
|
|
Err(Error::from(UploadError::ParsePath))
|
|
|
|
} else {
|
|
|
|
inner
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2022-03-28 20:34:36 +00:00
|
|
|
path.push(ext);
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
Ok((path, args))
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
2020-06-07 17:51:45 +00:00
|
|
|
|
|
|
|
impl Processor for Identity {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str = "identity";
|
2020-06-07 19:12:19 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(_: &str, _: &str) -> Option<Self>
|
2020-06-07 19:12:19 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2021-10-21 01:13:39 +00:00
|
|
|
Some(Identity)
|
2020-06-07 19:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 18:03:36 +00:00
|
|
|
fn path(&self, path: PathBuf) -> PathBuf {
|
2020-06-07 17:51:45 +00:00
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, args: Vec<String>) -> Vec<String> {
|
|
|
|
args
|
2020-06-07 17:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Processor for Thumbnail {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str = "thumbnail";
|
2020-06-07 19:12:19 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(_: &str, v: &str) -> Option<Self>
|
2020-06-07 19:12:19 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2020-06-24 16:58:46 +00:00
|
|
|
let size = v.parse().ok()?;
|
2021-10-21 01:13:39 +00:00
|
|
|
Some(Thumbnail(size))
|
2020-06-07 19:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:51:45 +00:00
|
|
|
fn path(&self, mut path: PathBuf) -> PathBuf {
|
2021-10-21 01:13:39 +00:00
|
|
|
path.push(Self::NAME);
|
2020-06-07 17:51:45 +00:00
|
|
|
path.push(self.0.to_string());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, mut args: Vec<String>) -> Vec<String> {
|
|
|
|
args.extend(["-sample".to_string(), format!("{}x{}>", self.0, self.0)]);
|
2020-06-17 02:05:06 +00:00
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
args
|
2020-06-07 17:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 05:05:04 +00:00
|
|
|
impl Processor for Resize {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str = "resize";
|
2020-12-10 05:05:04 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(_: &str, v: &str) -> Option<Self>
|
2020-12-10 05:05:04 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-09-26 23:14:25 +00:00
|
|
|
if let Some((first, second)) = v.split_once('.') {
|
|
|
|
let filter = ResizeFilter::from_str(first);
|
|
|
|
|
|
|
|
let kind = ResizeKind::from_str(second)?;
|
|
|
|
|
|
|
|
Some(Resize { filter, kind })
|
|
|
|
} else {
|
|
|
|
let size = v.parse().ok()?;
|
|
|
|
Some(Resize {
|
|
|
|
filter: None,
|
|
|
|
kind: ResizeKind::Bounds(size),
|
|
|
|
})
|
|
|
|
}
|
2020-12-10 05:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self, mut path: PathBuf) -> PathBuf {
|
2021-10-21 01:13:39 +00:00
|
|
|
path.push(Self::NAME);
|
2022-09-26 23:14:25 +00:00
|
|
|
match self {
|
|
|
|
Resize {
|
|
|
|
filter: None,
|
|
|
|
kind: ResizeKind::Bounds(size),
|
|
|
|
} => {
|
|
|
|
path.push(size.to_string());
|
|
|
|
}
|
|
|
|
Resize {
|
|
|
|
filter: None,
|
|
|
|
kind: ResizeKind::Area(size),
|
|
|
|
} => {
|
2023-01-29 17:57:59 +00:00
|
|
|
let node = format!(".a{size}");
|
2022-09-26 23:14:25 +00:00
|
|
|
path.push(node);
|
|
|
|
}
|
|
|
|
Resize {
|
|
|
|
filter: Some(filter),
|
|
|
|
kind: ResizeKind::Bounds(size),
|
|
|
|
} => {
|
2023-01-29 17:57:59 +00:00
|
|
|
let node = format!("{}.{size}", filter.to_magick_str());
|
2022-09-26 23:14:25 +00:00
|
|
|
path.push(node);
|
|
|
|
}
|
|
|
|
Resize {
|
|
|
|
filter: Some(filter),
|
|
|
|
kind: ResizeKind::Area(size),
|
|
|
|
} => {
|
2023-01-29 17:57:59 +00:00
|
|
|
let node = format!("{}.a{size}", filter.to_magick_str());
|
2022-09-26 23:14:25 +00:00
|
|
|
path.push(node);
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 05:05:04 +00:00
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, mut args: Vec<String>) -> Vec<String> {
|
|
|
|
args.extend([
|
|
|
|
"-filter".to_string(),
|
2022-09-26 23:14:25 +00:00
|
|
|
self.filter.unwrap_or_default().to_magick_str().to_string(),
|
2021-08-28 22:15:14 +00:00
|
|
|
"-resize".to_string(),
|
2022-09-26 23:14:25 +00:00
|
|
|
self.kind.to_magick_string(),
|
2021-08-28 22:15:14 +00:00
|
|
|
]);
|
2020-12-10 05:05:04 +00:00
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
args
|
2020-12-10 05:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:20:06 +00:00
|
|
|
impl Processor for Crop {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str = "crop";
|
2020-12-17 00:20:06 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(_: &str, v: &str) -> Option<Self> {
|
2020-12-17 00:20:06 +00:00
|
|
|
let mut iter = v.split('x');
|
|
|
|
let first = iter.next()?;
|
|
|
|
let second = iter.next()?;
|
|
|
|
|
|
|
|
let width = first.parse::<usize>().ok()?;
|
|
|
|
let height = second.parse::<usize>().ok()?;
|
|
|
|
|
|
|
|
if width == 0 || height == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if width > 20 || height > 20 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
Some(Crop(width, height))
|
2020-12-17 00:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self, mut path: PathBuf) -> PathBuf {
|
2021-10-21 01:13:39 +00:00
|
|
|
path.push(Self::NAME);
|
2020-12-17 00:20:06 +00:00
|
|
|
path.push(format!("{}x{}", self.0, self.1));
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, mut args: Vec<String>) -> Vec<String> {
|
|
|
|
args.extend([
|
|
|
|
"-gravity".to_string(),
|
|
|
|
"center".to_string(),
|
|
|
|
"-crop".to_string(),
|
|
|
|
format!("{}:{}+0+0", self.0, self.1),
|
|
|
|
]);
|
2020-12-17 00:20:06 +00:00
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
args
|
2020-12-17 00:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:51:45 +00:00
|
|
|
impl Processor for Blur {
|
2021-10-21 01:13:39 +00:00
|
|
|
const NAME: &'static str = "blur";
|
2020-06-07 19:12:19 +00:00
|
|
|
|
2021-10-21 01:13:39 +00:00
|
|
|
fn parse(_: &str, v: &str) -> Option<Self> {
|
2020-06-24 16:58:46 +00:00
|
|
|
let sigma = v.parse().ok()?;
|
2021-10-21 01:13:39 +00:00
|
|
|
Some(Blur(sigma))
|
2020-06-07 19:12:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:51:45 +00:00
|
|
|
fn path(&self, mut path: PathBuf) -> PathBuf {
|
2021-10-21 01:13:39 +00:00
|
|
|
path.push(Self::NAME);
|
2020-06-07 17:51:45 +00:00
|
|
|
path.push(self.0.to_string());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
fn command(&self, mut args: Vec<String>) -> Vec<String> {
|
|
|
|
args.extend(["-gaussian-blur".to_string(), self.0.to_string()]);
|
2020-06-24 16:58:46 +00:00
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
args
|
2020-06-07 17:51:45 +00:00
|
|
|
}
|
|
|
|
}
|