Use author ID as filename instead of random suffix

This commit is contained in:
江山 2025-10-04 12:17:56 +00:00
parent 4c80072f23
commit 5036ec921f
2 changed files with 11 additions and 4 deletions

View file

@ -20,7 +20,7 @@
<ui-text-input v-model="imageUrl" :placeholder="$strings.LabelImageURLFromTheWeb" class="h-9 w-full" @input="onUrlInput" />
<label class="ml-2 sm:ml-3 w-24 h-9 flex items-center justify-center bg-gray-700 text-white rounded cursor-pointer hover:bg-gray-600">
<input type="file" accept="image/*" class="hidden" @change="onFileChange" />
Local
{{ $strings.ButtonUpload || 'Local' }}
</label>
<ui-btn color="bg-success" type="submit" :padding-x="4" :disabled="!imageUrl && !selectedFile" class="ml-2 sm:ml-3 w-24 h-9">{{ $strings.ButtonSubmit }}</ui-btn>
<ui-btn v-if="selectedFile || previewUrl" type="button" color="bg-error" class="ml-2 sm:ml-3 w-24 h-9" small @click="clearSelection">{{ $strings.ButtonCancel }}</ui-btn>

View file

@ -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
}