973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | // Ported from crates/malice-core/src/media.rs's attachment_content_type, so the |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | // service worker can sniff decrypted bytes without a round trip to the wasm host. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | function startsWith(bytes: Uint8Array, prefix: number[]): boolean { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | if (bytes.length < prefix.length) return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | for (let i = 0; i < prefix.length; i++) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | if (bytes[i] !== prefix[i]) return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | return true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | function ascii(text: string): number[] { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | return Array.from(text, (ch) => ch.charCodeAt(0)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | function bytesEqual(bytes: Uint8Array, offset: number, text: string): boolean { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | const wanted = ascii(text); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | if (bytes.length < offset + wanted.length) return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | for (let i = 0; i < wanted.length; i++) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | if (bytes[offset + i] !== wanted[i]) return false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | return true; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | export function imageContentType(bytes: Uint8Array): string | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return 'image/png'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | if (startsWith(bytes, [0xff, 0xd8, 0xff])) return 'image/jpeg'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | if (bytesEqual(bytes, 0, 'GIF87a') || bytesEqual(bytes, 0, 'GIF89a')) return 'image/gif'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | if (bytes.length >= 12 && bytesEqual(bytes, 0, 'RIFF') && bytesEqual(bytes, 8, 'WEBP')) return 'image/webp'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | if (bytes.length >= 12 && bytesEqual(bytes, 4, 'ftyp') && (bytesEqual(bytes, 8, 'avif') || bytesEqual(bytes, 8, 'avis'))) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | return 'image/avif'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | export function attachmentContentType(bytes: Uint8Array): string | null { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | const image = imageContentType(bytes); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | if (image) return image; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | if (startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3])) return 'video/webm'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | if (bytes.length >= 12 && bytesEqual(bytes, 0, 'RIFF') && bytesEqual(bytes, 8, 'WAVE')) return 'audio/wav'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | if (bytesEqual(bytes, 0, 'OggS')) return 'audio/ogg'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | if (bytesEqual(bytes, 0, 'ID3') || startsWith(bytes, [0xff, 0xfb])) return 'audio/mpeg'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | if ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | bytes.length >= 12 && |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | bytesEqual(bytes, 4, 'ftyp') && |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | ['isom', 'iso2', 'mp41', 'mp42', 'avc1'].some((brand) => bytesEqual(bytes, 8, brand)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | ) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | return 'video/mp4'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | return null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | } |