|
| 1 | +import { z } from 'zod'; |
| 2 | +import { uploadFile } from '@tool/utils/uploadFile'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Detect image MIME type from base64 binary data by checking file signatures |
| 6 | + * Supports JPEG, PNG, GIF, BMP, and WebP formats |
| 7 | + */ |
| 8 | +function detectImageMimeType(base64Data: string) { |
| 9 | + try { |
| 10 | + // Remove data URL prefix if exists and decode base64 |
| 11 | + const base64Content = base64Data.replace(/^data:[^;]+;base64,/, ''); |
| 12 | + const binaryString = atob(base64Content); |
| 13 | + const bytes = new Uint8Array(binaryString.length); |
| 14 | + for (let i = 0; i < binaryString.length; i++) { |
| 15 | + bytes[i] = binaryString.charCodeAt(i); |
| 16 | + } |
| 17 | + |
| 18 | + // Check for common image file signatures |
| 19 | + // JPEG: FF D8 FF |
| 20 | + if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) { |
| 21 | + return 'image/jpeg'; |
| 22 | + } |
| 23 | + |
| 24 | + // PNG: 89 50 4E 47 0D 0A 1A 0A |
| 25 | + if ( |
| 26 | + bytes.length >= 8 && |
| 27 | + bytes[0] === 0x89 && |
| 28 | + bytes[1] === 0x50 && |
| 29 | + bytes[2] === 0x4e && |
| 30 | + bytes[3] === 0x47 && |
| 31 | + bytes[4] === 0x0d && |
| 32 | + bytes[5] === 0x0a && |
| 33 | + bytes[6] === 0x1a && |
| 34 | + bytes[7] === 0x0a |
| 35 | + ) { |
| 36 | + return 'image/png'; |
| 37 | + } |
| 38 | + |
| 39 | + // GIF: 47 49 46 38 (GIF8) |
| 40 | + if ( |
| 41 | + bytes.length >= 4 && |
| 42 | + bytes[0] === 0x47 && |
| 43 | + bytes[1] === 0x49 && |
| 44 | + bytes[2] === 0x46 && |
| 45 | + bytes[3] === 0x38 |
| 46 | + ) { |
| 47 | + return 'image/gif'; |
| 48 | + } |
| 49 | + |
| 50 | + // BMP: 42 4D |
| 51 | + if (bytes.length >= 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) { |
| 52 | + return 'image/bmp'; |
| 53 | + } |
| 54 | + |
| 55 | + // WebP: RIFF + WEBP |
| 56 | + if ( |
| 57 | + bytes.length >= 12 && |
| 58 | + bytes[0] === 0x52 && |
| 59 | + bytes[1] === 0x49 && |
| 60 | + bytes[2] === 0x46 && |
| 61 | + bytes[3] === 0x46 && |
| 62 | + bytes[8] === 0x57 && |
| 63 | + bytes[9] === 0x45 && |
| 64 | + bytes[10] === 0x42 && |
| 65 | + bytes[11] === 0x50 |
| 66 | + ) { |
| 67 | + return 'image/webp'; |
| 68 | + } |
| 69 | + |
| 70 | + // Default to PNG if no signature matches |
| 71 | + return null; |
| 72 | + } catch { |
| 73 | + // If any error occurs during detection, default to PNG |
| 74 | + return null; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export const InputType = z.object({ |
| 79 | + base64: z.string().nonempty() |
| 80 | +}); |
| 81 | + |
| 82 | +export const OutputType = z.object({ |
| 83 | + url: z.string(), |
| 84 | + type: z.string(), |
| 85 | + size: z.number() |
| 86 | +}); |
| 87 | + |
| 88 | +/** |
| 89 | + * Convert base64 image data to a file and return its URL, type, and size |
| 90 | + * Supports both data URL format (with MIME type) and raw base64 (auto-detected) |
| 91 | + */ |
| 92 | +export async function tool({ |
| 93 | + base64 |
| 94 | +}: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> { |
| 95 | + // First try to get MIME type from data URL |
| 96 | + const mime = (() => { |
| 97 | + const match = base64.match(/^data:([^;]+);base64,/); |
| 98 | + if (match?.[1]) { |
| 99 | + return match[1]; |
| 100 | + } |
| 101 | + const detectedType = detectImageMimeType(base64); |
| 102 | + if (!detectedType) { |
| 103 | + throw new Error('Image Type unknown'); |
| 104 | + } |
| 105 | + return detectedType; |
| 106 | + })(); |
| 107 | + |
| 108 | + const ext = (() => { |
| 109 | + const m = mime.split('/')[1]; |
| 110 | + return m && m.length > 0 ? m : 'png'; |
| 111 | + })(); |
| 112 | + |
| 113 | + // Generate filename with appropriate extension |
| 114 | + const filename = `image.${ext}`; |
| 115 | + |
| 116 | + const meta = await uploadFile({ base64, defaultFilename: filename }); |
| 117 | + |
| 118 | + return { |
| 119 | + url: meta.accessUrl, |
| 120 | + type: meta.contentType, |
| 121 | + size: meta.size |
| 122 | + }; |
| 123 | +} |
0 commit comments