From 41a993d0b1c0c0f4f897a140eb21a9f66ed6402f Mon Sep 17 00:00:00 2001 From: Syfaro Date: Fri, 9 Oct 2020 19:10:44 -0400 Subject: [PATCH] Handle corrupted images. --- src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 79add7d..9aca4ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,16 +122,22 @@ async fn process_submission( .bytes() .await?; - let hasher = img_hash::HasherConfig::with_bytes_type::<[u8; 8]>() - .hash_alg(img_hash::HashAlg::Gradient) - .hash_size(8, 8) - .preproc_dct() - .to_hasher(); - let image = image::load_from_memory(&data)?; - let hash = hasher.hash_image(&image); - let mut bytes: [u8; 8] = [0; 8]; - bytes.copy_from_slice(hash.as_bytes()); - let num = i64::from_be_bytes(bytes); + let num = if let Ok(image) = image::load_from_memory(&data) { + let hasher = img_hash::HasherConfig::with_bytes_type::<[u8; 8]>() + .hash_alg(img_hash::HashAlg::Gradient) + .hash_size(8, 8) + .preproc_dct() + .to_hasher(); + let hash = hasher.hash_image(&image); + let mut bytes: [u8; 8] = [0; 8]; + bytes.copy_from_slice(hash.as_bytes()); + let num = i64::from_be_bytes(bytes); + Some(num) + } else { + println!("Unable to decode image on submission {}", sub.id); + + None + }; let mut hasher = Sha256::new(); hasher.update(&data);