(blob: Blob)
| 60 | } |
| 61 | |
| 62 | export async function getExtensionFromBytes(blob: Blob): Promise<string | undefined> { |
| 63 | const buffer = await blob.slice(0, 12).arrayBuffer(); |
| 64 | const bytes = new Uint8Array(buffer); |
| 65 | |
| 66 | if (bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38) { |
| 67 | return "gif"; |
| 68 | } |
| 69 | |
| 70 | if (bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4E && bytes[3] === 0x47) { |
| 71 | const fullBuffer = await blob.slice(0, 4096).arrayBuffer(); |
| 72 | const fullBytes = new Uint8Array(fullBuffer); |
| 73 | for (let i = 0; i < fullBytes.length - 4; i++) { |
| 74 | if (fullBytes[i] === 0x61 && fullBytes[i + 1] === 0x63 && |
| 75 | fullBytes[i + 2] === 0x54 && fullBytes[i + 3] === 0x4C) { |
| 76 | return "apng"; |
| 77 | } |
| 78 | } |
| 79 | return "png"; |
| 80 | } |
| 81 | |
| 82 | if (bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 && |
| 83 | bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) { |
| 84 | return "webp"; |
| 85 | } |
| 86 | |
| 87 | if (bytes[0] === 0xFF && bytes[1] === 0xD8 && bytes[2] === 0xFF) { |
| 88 | return "jpg"; |
| 89 | } |
| 90 | |
| 91 | return undefined; |
| 92 | } |
| 93 | |
| 94 | export function getUrlExtension(url: string): string | undefined { |
| 95 | if (!url.startsWith("https:") && !url.startsWith("http:")) { |
no outgoing calls
no test coverage detected