From e719ea1300c7cc2bb63964f0cb668a55bc893f3f Mon Sep 17 00:00:00 2001 From: seaHi Date: Fri, 11 Sep 2026 18:16:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AA=92=E4=BD=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=A0=E9=99=A4=20API=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/admin/api/upload/+server.ts | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/routes/admin/api/upload/+server.ts b/src/routes/admin/api/upload/+server.ts index 4ee8e0a..808bbdd 100644 --- a/src/routes/admin/api/upload/+server.ts +++ b/src/routes/admin/api/upload/+server.ts @@ -33,3 +33,43 @@ export const POST: RequestHandler = async ({ request, platform }) => { 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 }); + } +};