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 | 1x 1x 1x 1x 1x 4x 4x 3x 2x 1x 1x 1x 1x 1x 1x 1x 2x | // 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 async function toBase64(media: Media): Promise<string> {
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();
if (isFile!) {
const imageBuffer = await fs.readFile(path.resolve(media.path));
base64 = imageBuffer.toString('base64');
}
}
else E{
throw new Error("Didn't get any valid media or uri.");
}
return base64;
}
|