import { mkdir, writeFile, unlink } from 'node:fs/promises'; import { join } from 'node:path'; const DATA_DIR = process.env.TASKPLANER_DATA_DIR || './data'; export const UPLOAD_DIR = join(DATA_DIR, 'uploads'); export const ORIGINALS_DIR = join(DATA_DIR, 'uploads/originals'); export const THUMBNAILS_DIR = join(DATA_DIR, 'uploads/thumbnails'); /** * Ensure upload directories exist */ export async function ensureDirectories(): Promise { await mkdir(ORIGINALS_DIR, { recursive: true }); await mkdir(THUMBNAILS_DIR, { recursive: true }); } /** * Save original image to filesystem */ export async function saveOriginal(id: string, ext: string, buffer: Buffer): Promise { const path = getOriginalPath(id, ext); await writeFile(path, buffer); } /** * Save thumbnail to filesystem (always jpg) */ export async function saveThumbnail(id: string, buffer: Buffer): Promise { const path = getThumbnailPath(id); await writeFile(path, buffer); } /** * Get path to original image */ export function getOriginalPath(id: string, ext: string): string { return join(ORIGINALS_DIR, `${id}.${ext}`); } /** * Get path to thumbnail (always jpg) */ export function getThumbnailPath(id: string): string { return join(THUMBNAILS_DIR, `${id}.jpg`); } /** * Delete both original and thumbnail files * Does not throw if files don't exist */ export async function deleteImage(id: string, ext: string): Promise { try { await unlink(getOriginalPath(id, ext)); } catch { // File may not exist, ignore } try { await unlink(getThumbnailPath(id)); } catch { // File may not exist, ignore } }