Improve bounds check, don't save variant unless it's been changed

This commit is contained in:
asonix 2020-06-14 23:10:30 -05:00
parent 100a97a3f7
commit 84c10edd0e
2 changed files with 28 additions and 13 deletions

View File

@ -226,7 +226,13 @@ async fn serve(
}; };
debug!("Processing image"); debug!("Processing image");
let img = self::processor::process_image(chain, img).await?; let (img, changed) = self::processor::process_image(chain, img).await?;
if !changed {
let stream = actix_fs::read_to_stream(original_path).await?;
return Ok(srv_response(stream, ext));
}
// perform thumbnail operation in a blocking thread // perform thumbnail operation in a blocking thread
debug!("Exporting image"); debug!("Exporting image");

View File

@ -18,7 +18,7 @@ pub(crate) trait Processor {
Self: Sized; Self: Sized;
fn path(&self, path: PathBuf) -> PathBuf; fn path(&self, path: PathBuf) -> PathBuf;
fn process(&self, img: DynamicImage) -> Result<DynamicImage, UploadError>; fn process(&self, img: DynamicImage) -> Result<(DynamicImage, bool), UploadError>;
fn is_whitelisted(whitelist: Option<&HashSet<String>>) -> bool fn is_whitelisted(whitelist: Option<&HashSet<String>>) -> bool
where where
@ -59,8 +59,8 @@ impl Processor for Identity {
path path
} }
fn process(&self, img: DynamicImage) -> Result<DynamicImage, UploadError> { fn process(&self, img: DynamicImage) -> Result<(DynamicImage, bool), UploadError> {
Ok(img) Ok((img, false))
} }
} }
@ -95,12 +95,12 @@ impl Processor for Thumbnail {
path path
} }
fn process(&self, img: DynamicImage) -> Result<DynamicImage, UploadError> { fn process(&self, img: DynamicImage) -> Result<(DynamicImage, bool), UploadError> {
debug!("Thumbnail"); debug!("Thumbnail");
if img.in_bounds(self.0, self.0) { if img.width() > self.0 || img.height() > self.0 {
Ok(img.thumbnail(self.0, self.0)) Ok((img.thumbnail(self.0, self.0), true))
} else { } else {
Ok(img) Ok((img, false))
} }
} }
} }
@ -130,9 +130,13 @@ impl Processor for Blur {
path path
} }
fn process(&self, img: DynamicImage) -> Result<DynamicImage, UploadError> { fn process(&self, img: DynamicImage) -> Result<(DynamicImage, bool), UploadError> {
debug!("Blur"); debug!("Blur");
Ok(img.blur(self.0)) if self.0 > 0.0 {
Ok((img.blur(self.0), true))
} else {
Ok((img, false))
}
} }
} }
@ -188,11 +192,13 @@ pub(crate) fn build_path(base: PathBuf, chain: &ProcessChain, filename: String)
pub(crate) async fn process_image( pub(crate) async fn process_image(
chain: ProcessChain, chain: ProcessChain,
mut img: DynamicImage, mut img: DynamicImage,
) -> Result<DynamicImage, UploadError> { ) -> Result<(DynamicImage, bool), UploadError> {
let mut changed = false;
for processor in chain.inner.into_iter() { for processor in chain.inner.into_iter() {
debug!("Step"); debug!("Step");
let span = Span::current(); let span = Span::current();
img = web::block(move || { let tup = web::block(move || {
let entered = span.enter(); let entered = span.enter();
let res = processor.process(img); let res = processor.process(img);
drop(entered); drop(entered);
@ -200,7 +206,10 @@ pub(crate) async fn process_image(
}) })
.await?; .await?;
debug!("Step complete"); debug!("Step complete");
img = tup.0;
changed |= tup.1;
} }
Ok(img) Ok((img, changed))
} }