feat: 添加媒体文件删除 API 接口支持

This commit is contained in:
seahi committed 2026-09-11 18:16:11 +08:00
1 parent 2f41bda41e
commit e719ea1300
1 file changed
+40
+40
View File
@@ -33,3 +33,43 @@ export const POST: RequestHandler = async ({ request, platform }) => {
return json({ error: '文件上传失败,请稍后重试' }, { status: 500 }); return json({ error: '文件上传失败,请稍后重试' }, { status: 500 });
} }
}; };
export const DELETE: RequestHandler = async ({ request, platform }) => {
const db = platform?.env.DB;
const r2 = platform?.env.R2;
if (!db || !r2) return json({ error: '数据库或 R2 存储未连接' }, { status: 500 });
try {
const body = (await request.json()) as { url?: string; key?: string };
let key = body.key;
if (!key && body.url) {
try {
const urlObj = new URL(body.url);
key = urlObj.pathname.replace(/^\//, '');
} catch {
// Invalid URL
}
}
if (!key) {
return json({ error: '必须提供 key 或 url' }, { status: 400 });
}
const attachment = await db
.prepare('SELECT id FROM attachments WHERE key = ?')
.bind(key)
.first();
if (attachment) {
await db.prepare('DELETE FROM attachments WHERE key = ?').bind(key).run();
}
await r2.delete(key);
return json({ success: true });
} catch (cause) {
console.error('Delete failed:', cause);
return json({ error: '文件删除失败' }, { status: 500 });
}
};