썸네일 미참조 삭제 허용·원본명 업로드·미디어 검색 정리(v0.0.91)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { mkdir, writeFile } from 'node:fs/promises'
|
||||
import { mkdir, stat, writeFile } from 'node:fs/promises'
|
||||
import { extname, join } from 'node:path'
|
||||
import { createError, readMultipartFormData } from 'h3'
|
||||
import { requireAdminSession } from '../../../utils/admin-auth'
|
||||
@@ -37,6 +36,37 @@ const getUploadExtension = (file) => {
|
||||
return extension
|
||||
}
|
||||
|
||||
/**
|
||||
* 디렉터리 안에서 비어 있는 저장 파일명을 고른다. 동일 이름이 있으면 `이름-2`, `이름-3` 식으로 넘버링한다.
|
||||
* @param {string} directoryPath - 저장 디렉터리 절대 경로
|
||||
* @param {string} stem - 확장자 제외 파일명
|
||||
* @param {string} extension - 확장자(점 포함, 예: `.png`)
|
||||
* @returns {Promise<{ fileName: string, filePath: string }>} 선택된 파일명과 절대 경로
|
||||
*/
|
||||
const pickUniqueDiskFileName = async (directoryPath, stem, extension) => {
|
||||
let suffix = 1
|
||||
|
||||
while (suffix < 10000) {
|
||||
const fileName = suffix === 1 ? `${stem}${extension}` : `${stem}-${suffix}${extension}`
|
||||
const filePath = join(directoryPath, fileName)
|
||||
|
||||
try {
|
||||
await stat(filePath)
|
||||
suffix += 1
|
||||
} catch {
|
||||
return {
|
||||
fileName,
|
||||
filePath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: '저장할 고유 파일명을 만들 수 없습니다.'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 관리자 이미지 업로드 API
|
||||
* @param {import('h3').H3Event} event - 요청 이벤트
|
||||
@@ -83,10 +113,9 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
const originalName = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'image'
|
||||
const originalStem = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'image'
|
||||
const extension = getUploadExtension(file)
|
||||
const fileName = `${originalName}-${randomUUID()}${extension}`
|
||||
const filePath = join(directoryPath, fileName)
|
||||
const { fileName, filePath } = await pickUniqueDiskFileName(directoryPath, originalStem, extension)
|
||||
|
||||
await writeFile(filePath, file.data)
|
||||
|
||||
@@ -96,7 +125,7 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
uploadedFiles.push({
|
||||
url: publicUrl,
|
||||
name: file.filename,
|
||||
name: fileName,
|
||||
size: file.data.length
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user