"use server"; import { revalidatePath } from "next/cache"; import path from "node:path"; import fs from "node:fs/promises"; import { rawDb } from "@/lib/db/client"; import { safeJoin } from "@/lib/safePath"; const LIBRARY_ROOT = path.join(process.cwd(), "library"); const THUMB_ROOT = path.join(process.cwd(), "data", "thumbs"); export async function deleteAttachedImage(attachedId: number) { const row = rawDb.prepare(` SELECT parent_image_id AS parentId, rel_path AS relPath, thumb_path AS thumbPath FROM images WHERE id = ? `).get(attachedId) as { parentId: number | null; relPath: string; thumbPath: string } | undefined; if (!row || row.parentId == null) return; rawDb.prepare(`DELETE FROM images WHERE id = ?`).run(attachedId); const fileAbs = safeJoin(LIBRARY_ROOT, row.relPath); const thumbAbs = safeJoin(THUMB_ROOT, row.thumbPath); if (fileAbs) await fs.rm(fileAbs, { force: true }).catch(() => {}); if (thumbAbs) await fs.rm(thumbAbs, { force: true }).catch(() => {}); const parentCode = rawDb.prepare(`SELECT code FROM images WHERE id = ?`).get(row.parentId) as { code: string | null } | undefined; revalidatePath(`/image/${row.parentId}`); if (parentCode?.code) revalidatePath(`/id/${parentCode.code}`); }