A file called reaction.gif can still contain MP4 video. This matters in upload widgets: choosing an <img> solely from the filename can produce a broken preview, while accepting the file as a verified image can mislead later processing. A short header check can improve the error message before the application hands the bytes to a real decoder.
Read a small prefix, not the whole file
The following function checks the two GIF signatures and a common ISO Base Media File Format header. Its iso-bmff result deliberately does not claim that the file is a playable MP4. Other formats use the same container family.
async function inspectHeader(file) {
const bytes = new Uint8Array(await file.slice(0, 32).arrayBuffer());
const ascii = (start, length) =>
String.fromCharCode(...bytes.slice(start, start + length));
if (bytes.length >= 6 &&
['GIF87a', 'GIF89a'].includes(ascii(0, 6))) {
return { kind: 'gif', mimeHint: 'image/gif' };
}
if (bytes
Discussion
Be the first to comment
Add your perspective to get the discussion started.