From 35d785e7011c0304ae9dd5de1d7112f285c2056b Mon Sep 17 00:00:00 2001 From: faupol3 <45740133+faupol3@users.noreply.github.com> Date: Fri, 5 Nov 2021 19:04:10 -0300 Subject: [PATCH] Update isImage helper with file signatures --- src/helpers.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index b9b6398c..1fc3d108 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,7 +1,28 @@ export function isImage(file) { - if (file.type.split('/')[0] === 'image') { - return true; + var reader = new FileReader(); + reader.onloadend = (event) => { + var arr = (new Uint8Array(event.target.result)).subarray(0, 4); + var header = ""; + for (var i = 0; i < arr.length; i++) { + header += arr[i].toString(16); + } + + switch (header) { + case "47494638": // GIF + case "52494646": // WEBP + case "89504e47": // PNG + case "ffd8ffe0": // JPEG + case "ffd8ffe1": // JPEG + case "ffd8ffe2": // JPEG + case "ffd8ffe3": // JPEG + case "ffd8ffe8": // JPEG + return true; + default: + return false; + } + } + fileReader.readAsArrayBuffer(file); } export function convertBytesToMbsOrKbs(filesize) {