2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00

Validate with magickwand

This commit is contained in:
asonix 2020-06-16 16:34:37 -05:00
parent df85faff8b
commit 0fef7abc67
2 changed files with 70 additions and 14 deletions

View file

@ -91,6 +91,24 @@ pub(crate) enum Format {
Webp, Webp,
} }
impl Format {
pub(crate) fn to_mime(&self) -> mime::Mime {
match self {
Format::Jpeg => mime::IMAGE_JPEG,
Format::Png => mime::IMAGE_PNG,
Format::Webp => "image/webp".parse().unwrap(),
}
}
pub(crate) fn to_magick_format(&self) -> &'static str {
match self {
Format::Jpeg => "JPEG",
Format::Png => "PNG",
Format::Webp => "WEBP",
}
}
}
impl std::str::FromStr for Format { impl std::str::FromStr for Format {
type Err = FormatError; type Err = FormatError;

View file

@ -14,6 +14,10 @@ pub(crate) trait Op {
fn op<F, T>(&self, f: F) -> Result<T, UploadError> fn op<F, T>(&self, f: F) -> Result<T, UploadError>
where where
F: Fn(&Self) -> Result<T, &'static str>; F: Fn(&Self) -> Result<T, &'static str>;
fn op_mut<F, T>(&mut self, f: F) -> Result<T, UploadError>
where
F: Fn(&mut Self) -> Result<T, &'static str>;
} }
impl Op for MagickWand { impl Op for MagickWand {
@ -26,11 +30,30 @@ impl Op for MagickWand {
Err(e) => { Err(e) => {
if let Ok(e) = self.get_exception() { if let Ok(e) = self.get_exception() {
error!("WandError: {}", e.0); error!("WandError: {}", e.0);
} Err(UploadError::Wand(e.0.to_owned()))
} else {
Err(UploadError::Wand(e.to_owned())) Err(UploadError::Wand(e.to_owned()))
} }
} }
} }
}
fn op_mut<F, T>(&mut self, f: F) -> Result<T, UploadError>
where
F: Fn(&mut Self) -> Result<T, &'static str>,
{
match f(self) {
Ok(t) => Ok(t),
Err(e) => {
if let Ok(e) = self.get_exception() {
error!("WandError: {}", e.0);
Err(UploadError::Wand(e.0.to_owned()))
} else {
Err(UploadError::Wand(e.to_owned()))
}
}
}
}
} }
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@ -80,7 +103,6 @@ pub(crate) async fn validate_image(
debug!("format: {}", wand.op(|w| w.get_format())?); debug!("format: {}", wand.op(|w| w.get_format())?);
} }
let meta = Metadata::new_from_path(&tmpfile)?;
meta.clear(); meta.clear();
meta.save_to_file(&tmpfile)?; meta.save_to_file(&tmpfile)?;
@ -95,7 +117,6 @@ pub(crate) async fn validate_image(
debug!("format: {}", wand.op(|w| w.get_format())?); debug!("format: {}", wand.op(|w| w.get_format())?);
} }
let meta = Metadata::new_from_path(&tmpfile)?;
meta.clear(); meta.clear();
meta.save_to_file(&tmpfile)?; meta.save_to_file(&tmpfile)?;
@ -104,33 +125,50 @@ pub(crate) async fn validate_image(
(Some(Format::Webp), MediaType::Other(webp)) | (None, MediaType::Other(webp)) (Some(Format::Webp), MediaType::Other(webp)) | (None, MediaType::Other(webp))
if webp == "image/webp" => if webp == "image/webp" =>
{ {
let newfile = tmp_file();
let newfile_str = ptos(&newfile)?;
// clean metadata by writing new webp, since exiv2 doesn't support webp yet
{ {
let wand = MagickWand::new(); let wand = MagickWand::new();
debug!("reading: {}", tmpfile_str); debug!("reading: {}", tmpfile_str);
wand.op(|w| w.read_image(&tmpfile_str))?; wand.op(|w| w.read_image(&tmpfile_str))?;
debug!("format: {}", wand.op(|w| w.get_format())?); debug!("format: {}", wand.op(|w| w.get_format())?);
debug!("image_format: {}", wand.op(|w| w.get_image_format())?);
debug!("type: {}", wand.op(|w| Ok(w.get_type()))?); debug!("type: {}", wand.op(|w| Ok(w.get_type()))?);
debug!("image_type: {}", wand.op(|w| Ok(w.get_image_type()))?); debug!("image_type: {}", wand.op(|w| Ok(w.get_image_type()))?);
wand.op(|w| w.write_image(&newfile_str))?;
} }
// let meta = Metadata::new_from_path(&tmpfile)?; std::fs::rename(&newfile, &tmpfile)?;
// meta.clear();
// meta.save_to_file(&tmpfile)?;
image_webp() image_webp()
} }
(Some(Format::Jpeg), _) => { (Some(format), _) => {
let newfile = tmp_file(); let newfile = tmp_file();
convert(&tmpfile, &newfile, ImageFormat::Jpeg)?; let newfile_str = ptos(&newfile)?;
{
let mut wand = MagickWand::new();
mime::IMAGE_JPEG debug!("reading: {}", tmpfile_str);
wand.op(|w| w.read_image(&tmpfile_str))?;
debug!("format: {}", wand.op(|w| w.get_format())?);
debug!("image_format: {}", wand.op(|w| w.get_image_format())?);
debug!("type: {}", wand.op(|w| Ok(w.get_type()))?);
debug!("image_type: {}", wand.op(|w| Ok(w.get_image_type()))?);
wand.op_mut(|w| w.set_image_format(format.to_magick_format()))?;
debug!("writing: {}", newfile_str);
wand.op(|w| w.write_image(&newfile_str))?;
} }
(Some(Format::Png), _) => {
let newfile = tmp_file();
convert(&tmpfile, &newfile, ImageFormat::Png)?;
mime::IMAGE_PNG std::fs::rename(&newfile, &tmpfile)?;
format.to_mime()
} }
(_, media_type) => { (_, media_type) => {
warn!("Unsupported media type, {}", media_type); warn!("Unsupported media type, {}", media_type);