|
| 1 | +const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a] |
| 2 | +const JPEG_SIGNATURE = [0xff, 0xd8, 0xff] |
| 3 | +const GIF87A_SIGNATURE = "GIF87a" |
| 4 | +const GIF89A_SIGNATURE = "GIF89a" |
| 5 | +const RIFF_SIGNATURE = "RIFF" |
| 6 | +const WEBP_SIGNATURE = "WEBP" |
| 7 | +const BMP_SIGNATURE = "BM" |
| 8 | +const ICO_SIGNATURE = [0x00, 0x00, 0x01, 0x00] |
| 9 | +const TIFF_LE_SIGNATURE = [0x49, 0x49, 0x2a, 0x00] |
| 10 | +const TIFF_BE_SIGNATURE = [0x4d, 0x4d, 0x00, 0x2a] |
| 11 | +const ISO_BMFF_FILE_TYPE_BOX = "ftyp" |
| 12 | +const AVIF_BRANDS = new Set(["avif", "avis"]) |
| 13 | + |
| 14 | +function hasBytes(bytes: Uint8Array, signature: readonly number[], offset = 0): boolean { |
| 15 | + if (bytes.length < offset + signature.length) { |
| 16 | + return false |
| 17 | + } |
| 18 | + |
| 19 | + return signature.every((value, index) => bytes[offset + index] === value) |
| 20 | +} |
| 21 | + |
| 22 | +function hasAscii(bytes: Uint8Array, signature: string, offset = 0): boolean { |
| 23 | + if (bytes.length < offset + signature.length) { |
| 24 | + return false |
| 25 | + } |
| 26 | + |
| 27 | + for (let i = 0; i < signature.length; i++) { |
| 28 | + if (bytes[offset + i] !== signature.charCodeAt(i)) { |
| 29 | + return false |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return true |
| 34 | +} |
| 35 | + |
| 36 | +function asciiSlice(bytes: Uint8Array, start: number, end: number): string { |
| 37 | + if (bytes.length < end) { |
| 38 | + return "" |
| 39 | + } |
| 40 | + |
| 41 | + return String.fromCharCode(...bytes.slice(start, end)) |
| 42 | +} |
| 43 | + |
| 44 | +function looksLikeSvg(bytes: Uint8Array): boolean { |
| 45 | + const sample = new TextDecoder("utf-8", { fatal: false }).decode(bytes.slice(0, 512)).trimStart() |
| 46 | + return ( |
| 47 | + sample.startsWith("<svg") || |
| 48 | + sample.startsWith("<!DOCTYPE svg") || |
| 49 | + (sample.startsWith("<?xml") && sample.includes("<svg")) |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +export function detectImageMimeType(bytes: Uint8Array): string | undefined { |
| 54 | + if (hasBytes(bytes, PNG_SIGNATURE)) { |
| 55 | + return "image/png" |
| 56 | + } |
| 57 | + |
| 58 | + if (hasBytes(bytes, JPEG_SIGNATURE)) { |
| 59 | + return "image/jpeg" |
| 60 | + } |
| 61 | + |
| 62 | + if (hasAscii(bytes, GIF87A_SIGNATURE) || hasAscii(bytes, GIF89A_SIGNATURE)) { |
| 63 | + return "image/gif" |
| 64 | + } |
| 65 | + |
| 66 | + if (hasAscii(bytes, RIFF_SIGNATURE) && hasAscii(bytes, WEBP_SIGNATURE, 8)) { |
| 67 | + return "image/webp" |
| 68 | + } |
| 69 | + |
| 70 | + if (hasAscii(bytes, BMP_SIGNATURE)) { |
| 71 | + return "image/bmp" |
| 72 | + } |
| 73 | + |
| 74 | + if (hasBytes(bytes, ICO_SIGNATURE)) { |
| 75 | + return "image/x-icon" |
| 76 | + } |
| 77 | + |
| 78 | + if (hasBytes(bytes, TIFF_LE_SIGNATURE) || hasBytes(bytes, TIFF_BE_SIGNATURE)) { |
| 79 | + return "image/tiff" |
| 80 | + } |
| 81 | + |
| 82 | + if (hasAscii(bytes, ISO_BMFF_FILE_TYPE_BOX, 4) && AVIF_BRANDS.has(asciiSlice(bytes, 8, 12))) { |
| 83 | + return "image/avif" |
| 84 | + } |
| 85 | + |
| 86 | + if (looksLikeSvg(bytes)) { |
| 87 | + return "image/svg+xml" |
| 88 | + } |
| 89 | + |
| 90 | + return undefined |
| 91 | +} |
| 92 | + |
| 93 | +export function detectBase64ImageMimeType(data: string): string | undefined { |
| 94 | + try { |
| 95 | + return detectImageMimeType(Buffer.from(data, "base64")) |
| 96 | + } catch { |
| 97 | + return undefined |
| 98 | + } |
| 99 | +} |
0 commit comments