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>
This commit is contained in:
Rafael dos Santos Silva 2021-06-30 19:03:34 -03:00 committed by GitHub
parent 9b51b9bf4e
commit fae68455b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -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;
}