FIX: Detect decode failures earlier in image optimization pipeline (#13595)

FIX: Detect decode failures earlier in image optimization pipeline (#13595)

  • FIX: Detect decode failures earlier in image optimization pipeline

Follow up to 9b51b9b but also detects the bug earlier and backs off.

What iOS 15 is doing is returning all zeroes to ctx.getImageData, so we don’t have to wait until resize to detect the problem.

  • Update app/assets/javascripts/discourse/app/lib/media-optimization-utils.js

Co-authored-by: Jarek Radosz jradosz@gmail.com

Co-authored-by: Jarek Radosz jradosz@gmail.com

diff --git a/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js b/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js
index 2d18cbf..ad581f0 100644
--- a/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js
+++ b/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js
@@ -66,6 +66,14 @@ function isTransparent(type, imageData) {
   return false;
 }
 
+function jpegDecodeFailure(type, imageData) {
+  if (!/(\.|\/)jpe?g$/i.test(type)) {
+    return false;
+  }
+
+  return imageData.data[3] === 0;
+}
+
 export async function fileToImageData(file) {
   const drawable = await fileToDrawable(file);
   const imageData = drawableToimageData(drawable);
@@ -74,5 +82,9 @@ export async function fileToImageData(file) {
     throw "Image has transparent pixels, won't convert to JPEG!";
   }
 
+  if (jpegDecodeFailure(file.type, imageData)) {
+    throw "JPEG image has transparent pixel, decode failed!";
+  }
+
   return imageData;
 }

GitHub sha: fae68455b70eecb2ad10785dba462018de4fb638

This commit appears in #13595 which was approved by CvX. It was merged by Falco.