0

This is what I currently found from here https://stackoverflow.com/a/58613321/3774282

  // tslint:disable number-literal-format
  // tslint:disable no-magic-numbers
  const imageMimes: Mime[] = [
    {
      mime: 'image/png',
      pattern: [0x89, 0x50, 0x4e, 0x47]
    },
    {
      mime: 'image/jpeg',
      pattern: [0xff, 0xd8, 0xff]
    },
    /*{
      mime: 'image/gif',
      pattern: [0x47, 0x49, 0x46, 0x38]
    },*/
    {
      mime: 'image/webp',
      pattern: [0x52, 0x49, 0x46, 0x46, undefined, undefined, undefined, undefined, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50],
    }
    // You can expand this list @see https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
  ];
  // tslint:enable no-magic-numbers
  // tslint:enable number-literal-format

And this is how I'm checking.

function isMime(bytes: Uint8Array, mime: Mime): boolean {
  return mime.pattern.every((p, i) => !p || bytes[i] === p);
}

function validateImageMimeType(file: File, callback: (b: boolean) => void) {
  const numBytesNeeded = Math.max(...imageMimes.map(m => m.pattern.length));
  const blob = file.slice(0, numBytesNeeded); // Read the needed bytes of the file

  const fileReader = new FileReader();

  fileReader.onloadend = e => {
    if (!e || !fileReader.result) return;

    const bytes = new Uint8Array(fileReader.result as ArrayBuffer);

    const valid = imageMimes.some(mime => isMime(bytes, mime));

    callback(valid);
  };

  fileReader.readAsArrayBuffer(blob);
}

I need to check all the possible image mime types here.

Can anyone give me the list of all images mime type and their patterns? Or Recommend me any open-source library which has this list.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.