forked from BRETT/BRETT
New audit_log table records actor, action, target and a JSON snapshot of the details, so entries stay readable after the target is deleted; the actor is set to NULL when the user is removed. logAudit() is called from upload delete (single and bulk), comment delete, tag removal, role create/delete, role permission changes, role assign/revoke and MOTD updates. Logging failures only warn and never fail the action itself. The admin page gets an audit card behind the new audit.view permission with action filter, click-to-filter by user, readable summaries per action and server-side pagination. Action catalog lives in shared/utils/audit.ts, labels under admin.auditActions (de/en). Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { unlink } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { eq, inArray } from 'drizzle-orm'
|
|
import { commentAttachments, comments, uploads } from '../../db/schema'
|
|
|
|
const ID_RE = /^[A-Za-z0-9]{1,16}$/
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const actor = await requirePermission(event, 'upload.delete')
|
|
const body = await readBody<{ ids?: unknown }>(event)
|
|
const raw = Array.isArray(body?.ids) ? body.ids : []
|
|
const ids = [...new Set(raw.filter((x): x is string => typeof x === 'string' && ID_RE.test(x)))].slice(0, 200)
|
|
if (!ids.length) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Keine Uploads ausgewählt' })
|
|
}
|
|
|
|
const db = useDb()
|
|
const rows = await db
|
|
.select({ id: uploads.id, fileName: uploads.fileName, previewImage: uploads.previewImage })
|
|
.from(uploads)
|
|
.where(inArray(uploads.id, ids))
|
|
if (!rows.length) return { ok: true, deleted: 0 }
|
|
|
|
const found = rows.map(r => r.id)
|
|
const attachmentFiles = await db
|
|
.select({ fileName: commentAttachments.fileName, cover: commentAttachments.cover })
|
|
.from(commentAttachments)
|
|
.innerJoin(comments, eq(commentAttachments.commentId, comments.id))
|
|
.where(inArray(comments.uploadId, found))
|
|
|
|
await db.delete(uploads).where(inArray(uploads.id, found))
|
|
|
|
for (const r of rows) {
|
|
if (r.fileName) {
|
|
await unlink(join(uploadDir(), r.fileName)).catch(() => {})
|
|
}
|
|
if (r.previewImage?.startsWith('/uploads/')) {
|
|
await unlink(join(process.cwd(), 'public', r.previewImage)).catch(() => {})
|
|
}
|
|
}
|
|
for (const f of attachmentFiles) {
|
|
await unlink(join(uploadDir(), 'attachments', f.fileName)).catch(() => {})
|
|
if (f.cover) {
|
|
await unlink(join(process.cwd(), 'public', f.cover)).catch(() => {})
|
|
}
|
|
}
|
|
broadcastToAll({ type: 'upload-deleted', ids: found })
|
|
await logAudit(actor.id, 'bulk_delete_uploads', null, { ids: found, count: found.length })
|
|
return { ok: true, deleted: found.length }
|
|
})
|