forked from BRETT/BRETT
- Server-side video thumbnails: ffmpeg-static grabs a frame at ~1s, scales it to 480px and stores it under /uploads/thumbs/ at upload time (direct files, remote URLs and comment attachments) - YouTube thumbnails are downloaded once at upload time instead of being hotlinked from i.ytimg.com on every render; remote URL kept as fallback for legacy rows - thumbUrl now covers video/audio/youtube/twitch in grid meta, profile uploads and notifications - Generated thumbnail files are removed on single/bulk delete Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { unlink } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { eq } from 'drizzle-orm'
|
|
import { commentAttachments, comments, uploads } from '../../db/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await requirePermission(event, 'upload.delete')
|
|
const id = getRouterParam(event, 'id') ?? ''
|
|
if (!/^[A-Za-z0-9]{1,16}$/.test(id)) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
|
|
}
|
|
const db = useDb()
|
|
|
|
const [upload] = await db
|
|
.select({ fileName: uploads.fileName, previewImage: uploads.previewImage })
|
|
.from(uploads)
|
|
.where(eq(uploads.id, id))
|
|
.limit(1)
|
|
if (!upload) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Upload nicht gefunden' })
|
|
}
|
|
|
|
const attachmentFiles = await db
|
|
.select({ fileName: commentAttachments.fileName, cover: commentAttachments.cover })
|
|
.from(commentAttachments)
|
|
.innerJoin(comments, eq(commentAttachments.commentId, comments.id))
|
|
.where(eq(comments.uploadId, id))
|
|
|
|
await db.delete(uploads).where(eq(uploads.id, id))
|
|
if (upload.fileName) {
|
|
await unlink(join(uploadDir(), upload.fileName)).catch(() => {})
|
|
}
|
|
if (upload.previewImage?.startsWith('/uploads/')) {
|
|
await unlink(join(process.cwd(), 'public', upload.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: [id] })
|
|
return { ok: true }
|
|
})
|