From 842af264e2db0956724c57951026b95c268cdf74 Mon Sep 17 00:00:00 2001 From: seaHi Date: Wed, 29 Jul 2026 21:48:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=91=BD=E5=90=8D=E8=A7=84=E8=8C=83=E5=B9=B6?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=AD=98=E5=82=A8=E9=94=AE=E5=90=8D=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/admin/files/+page.server.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/routes/admin/files/+page.server.ts b/src/routes/admin/files/+page.server.ts index 0024327..d51c888 100644 --- a/src/routes/admin/files/+page.server.ts +++ b/src/routes/admin/files/+page.server.ts @@ -64,12 +64,15 @@ export const actions: Actions = { return fail(400, { error: '没有提供有效的文件' }); } - // Generate unique key with year-month grouping (e.g. uploads/2026-07/timestamp-random.ext) + // Generate timestamp-based filename and robust physical key const ext = file.name.split('.').pop() || ''; const randomStr = Math.random().toString(36).substring(2, 8); const now = new Date(); const yearMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`; - const key = `uploads/${yearMonth}/${Date.now()}-${randomStr}.${ext}`; + const yyyymmdd = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`; + const hhmmss = `${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`; + const newFilename = `${yyyymmdd}_${hhmmss}.${ext}`; + const key = `uploads/${yearMonth}/${yyyymmdd}_${hhmmss}_${randomStr}.${ext}`; try { // Upload to R2 @@ -81,7 +84,7 @@ export const actions: Actions = { await ensureAttachmentsTable(db); // Defensive checking await db.prepare( 'INSERT INTO attachments (filename, key, mime_type, size) VALUES (?, ?, ?, ?)' - ).bind(file.name, key, file.type, file.size).run(); + ).bind(newFilename, key, file.type, file.size).run(); return { success: true }; } catch (err: any) {