Files
2026-05-26 22:46:00 +02:00

28 lines
1.2 KiB
TypeScript

"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}`);
}