26 lines
1.4 KiB
TypeScript
26 lines
1.4 KiB
TypeScript
import { z } from 'zod'
|
|
import { serializeMedia, type MediaAssetRow } from '../../../utils/media'
|
|
import { useDatabase } from '../../../utils/db'
|
|
|
|
const updateSchema = z.object({
|
|
altText: z.string().trim().max(160),
|
|
source: z.string().trim().max(200),
|
|
copyright: z.string().trim().max(200),
|
|
authorizationStatus: z.enum(['pending', 'authorized', 'restricted']),
|
|
})
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireAdmin(event)
|
|
const id = Number(getRouterParam(event, 'id'))
|
|
if (!Number.isSafeInteger(id) || id < 1) throw createError({ statusCode: 400, statusMessage: '无效的素材编号' })
|
|
const input = updateSchema.parse(await readBody(event))
|
|
const db = useDatabase()
|
|
const timestamp = new Date().toISOString()
|
|
const result = db.prepare(`UPDATE media_assets SET alt_text = ?, source_text = ?, copyright_text = ?, authorization_status = ?, updated_at = ? WHERE id = ? AND status = 'active'`)
|
|
.run(input.altText, input.source, input.copyright, input.authorizationStatus, timestamp, id)
|
|
if (!result.changes) throw createError({ statusCode: 404, statusMessage: '素材不存在' })
|
|
const row = db.prepare('SELECT * FROM media_assets WHERE id = ?').get(id) as MediaAssetRow
|
|
writeAuditLog(event, { operatorId: user.id, action: 'media.update', entityType: 'media', entityKey: row.asset_key })
|
|
return { data: serializeMedia(row) }
|
|
})
|