From 5036ec921f02622827474394b446f529d18e302a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E5=B1=B1?= <2479583912@QQ.COM> Date: Sat, 4 Oct 2025 12:17:56 +0000 Subject: [PATCH] Use author ID as filename instead of random suffix --- client/components/modals/authors/EditModal.vue | 2 +- server/controllers/AuthorController.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/client/components/modals/authors/EditModal.vue b/client/components/modals/authors/EditModal.vue index 7ac5dc74c..3256629cb 100644 --- a/client/components/modals/authors/EditModal.vue +++ b/client/components/modals/authors/EditModal.vue @@ -20,7 +20,7 @@ {{ $strings.ButtonSubmit }} {{ $strings.ButtonCancel }} diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index aeb391b56..4cec9d79c 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -276,14 +276,21 @@ class AuthorController { // 1. File upload (express-fileupload) if (req.files && req.files.image) { const uploadedFile = req.files.image - const uploadsDir = Path.join(global.MetadataPath, 'uploads') + const uploadsDir = Path.join(global.MetadataPath, 'authors') if (!fs.existsSync(uploadsDir)) { fs.mkdirSync(uploadsDir, { recursive: true }) } - const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9) - const fileName = 'image-' + uniqueSuffix + Path.extname(uploadedFile.name) + + // Use author ID as filename instead of random suffix + const fileName = `${req.author.id}${Path.extname(uploadedFile.name)}` const filePath = Path.join(uploadsDir, fileName) + + // Remove existing file if it exists + if (fs.existsSync(filePath)) { + fs.unlinkSync(filePath) + } + await uploadedFile.mv(filePath) imagePath = filePath }