import { mkdir, writeFile } from 'fs/promises';
import { randomBytes } from 'crypto';
import { join } from 'path';
import { tmpdir } from 'os';
export const UPLOAD_DIR = join(tmpdir(), 'cursor-connect-uploads');
const MIME_EXT = {
    'image/jpeg': 'jpg',
    'image/jpg': 'jpg',
    'image/png': 'png',
    'image/gif': 'gif',
    'image/webp': 'webp',
    'image/heic': 'heic',
    'image/heif': 'heif',
};
export function extensionForMime(mimeType) {
    return MIME_EXT[mimeType.toLowerCase()] ?? 'jpg';
}
/** Saves decoded image; returns absolute path for CDP / Cursor. */
export async function saveUploadedImage(buffer, mimeType) {
    await mkdir(UPLOAD_DIR, { recursive: true });
    const ext = extensionForMime(mimeType);
    const name = `${Date.now()}-${randomBytes(6).toString('hex')}.${ext}`;
    const path = join(UPLOAD_DIR, name);
    await writeFile(path, buffer);
    return path;
}
export function isPathUnderUploadDir(filePath) {
    const normalized = join(filePath);
    return normalized.startsWith(UPLOAD_DIR + '/') || normalized === UPLOAD_DIR;
}
