* Read a local image file and return it as a base64 data URI. * Supports ~/ home directory expansion.
(filePath: string)
| 1823 | * Supports ~/ home directory expansion. |
| 1824 | */ |
| 1825 | function readImageFileAsDataUri(filePath: string): string { |
| 1826 | const resolved = filePath.startsWith("~/") ? join(homedir(), filePath.slice(2)) : filePath; |
| 1827 | |
| 1828 | if (!existsSync(resolved)) { |
| 1829 | throw new Error(`Image file not found: ${resolved}`); |
| 1830 | } |
| 1831 | |
| 1832 | const ext = resolved.split(".").pop()?.toLowerCase() ?? "png"; |
| 1833 | const mimeMap: Record<string, string> = { |
| 1834 | png: "image/png", |
| 1835 | jpg: "image/jpeg", |
| 1836 | jpeg: "image/jpeg", |
| 1837 | webp: "image/webp", |
| 1838 | }; |
| 1839 | const mime = mimeMap[ext] ?? "image/png"; |
| 1840 | const data = readFileSync(resolved); |
| 1841 | return `data:${mime};base64,${data.toString("base64")}`; |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * Upload a base64 data URI to catbox.moe and return a public URL. |
no outgoing calls
no test coverage detected