Files
BRETT/server/api/uploads/bulk-delete.post.ts
T
FliesentischKingandDevin de0cd6d34f Mass-select grid and bulk delete for moderators
- Select mode in the filter row (upload.delete permission): tiles
  toggle selection in the capture phase, a floating action bar
  offers select-all and delete with confirmation popover
- New POST /api/uploads/bulk-delete endpoint (validated, deduped,
  max 200 ids) removing rows, files and attachment data
- upload-deleted broadcast on bulk and single delete; other clients
  refresh the grid and drop matching notifications
- Also: skeleton tile grid as loading state, dropzone logic moved
  to the layout-level GlobalDropZone, obsolete ytimg preconnect
  removed

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-04 03:06:38 +02:00

50 lines
1.8 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) => {
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 })
return { ok: true, deleted: found.length }
})