(sample: Uint8Array)
| 76 | } |
| 77 | |
| 78 | function mediaTypeFromMagic(sample: Uint8Array): string | null { |
| 79 | if (hasPrefix(sample, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png" |
| 80 | if (hasPrefix(sample, [0xff, 0xd8, 0xff])) return "image/jpeg" |
| 81 | if (asciiAt(sample, 0, 6) === "GIF87a" || asciiAt(sample, 0, 6) === "GIF89a") return "image/gif" |
| 82 | if (asciiAt(sample, 0, 4) === "RIFF" && asciiAt(sample, 8, 4) === "WEBP") return "image/webp" |
| 83 | if (asciiAt(sample, 0, 2) === "BM") return "image/bmp" |
| 84 | if (hasPrefix(sample, [0x00, 0x00, 0x01, 0x00])) return "image/x-icon" |
| 85 | |
| 86 | if (asciiAt(sample, 4, 4) === "ftyp") { |
| 87 | const brand = asciiAt(sample, 8, 12) |
| 88 | if (brand.includes("avif") || brand.includes("avis")) return "image/avif" |
| 89 | } |
| 90 | |
| 91 | return null |
| 92 | } |
| 93 | |
| 94 | function mediaTypeFromExtension(filePath: string): string | null { |
| 95 | return IMAGE_EXTENSION_MEDIA_TYPES.get(path.extname(filePath).toLowerCase()) ?? null |
no test coverage detected