Add initial image CLI runtime
This commit is contained in:
37
src/image.ts
Normal file
37
src/image.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
const IMAGES_DIR = "images";
|
||||
|
||||
function mediaTypeToExtension(mediaType: string): string {
|
||||
switch (mediaType) {
|
||||
case "image/png":
|
||||
return "png";
|
||||
case "image/jpeg":
|
||||
return "jpg";
|
||||
case "image/webp":
|
||||
return "webp";
|
||||
case "image/gif":
|
||||
return "gif";
|
||||
default:
|
||||
return "bin";
|
||||
}
|
||||
}
|
||||
|
||||
function createTimestampFileName(mediaType: string): string {
|
||||
const extension = mediaTypeToExtension(mediaType);
|
||||
const timestamp = new Date().toISOString().replace(/[.:]/g, "-");
|
||||
return `${timestamp}.${extension}`;
|
||||
}
|
||||
|
||||
export async function saveImage(
|
||||
bytes: Uint8Array,
|
||||
mediaType: string,
|
||||
): Promise<string> {
|
||||
await mkdir(IMAGES_DIR, { recursive: true });
|
||||
|
||||
const filePath = join(IMAGES_DIR, createTimestampFileName(mediaType));
|
||||
await writeFile(filePath, bytes);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
Reference in New Issue
Block a user