Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // Import Node.js Dependencies import * as fs from "node:fs/promises"; import * as path from "node:path"; // Import Third-Party Dependencies import fetch from "node-fetch"; // CONSTANTS const mediaRegex = new RegExp(".(gif|jpe?g|tiff?|png|webp|bmp|ico|wav|mp3|mp4)$", "i"); export interface Media { path?: string, uri?: string, } export interface ResponsePayload { base64?: string } export async function toBase64(media: Media): Promise<ResponsePayload> { let base64: string = ""; if (media.uri) { const uri = new URL(media.uri!); const image = await fetch(uri); const imageBuffer = await image.buffer(); base64 = imageBuffer.toString('base64'); } else if (media.path && mediaRegex.test(media.path)) { let isFile: boolean; isFile = (await fs.stat(media.path)).isFile(); Iif (isFile!) { const imageBuffer = await fs.readFile(path.resolve(media.path)); base64 = imageBuffer.toString('base64'); } } else { throw new Error("Didn't get any valid media or uri."); } return { base64 }; } |