썸네일 미참조 삭제 허용·원본명 업로드·미디어 검색 정리(v0.0.91)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-12 10:52:57 +09:00
parent 21024602b0
commit 16bb9370fa
10 changed files with 150 additions and 55 deletions

View File

@@ -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 { join } from 'node:path'
import { createError, readMultipartFormData } from 'h3'
import sharp from 'sharp'
@@ -48,6 +47,36 @@ const clampNumber = (value, minimum, maximum) => {
return Math.round(value)
}
/**
* 아바타 저장 디렉터리에서 비어 있는 `.webp` 파일명을 고른다. 동일 stem이 있으면 `-2`, `-3` 넘버링한다.
* @param {string} directoryPath - 저장 디렉터리 절대 경로
* @param {string} stem - 확장자 제외 파일명
* @returns {Promise<{ fileName: string, filePath: string }>} 파일명과 절대 경로
*/
const pickUniqueWebpFileName = async (directoryPath, stem) => {
let suffix = 1
while (suffix < 10000) {
const fileName = suffix === 1 ? `${stem}.webp` : `${stem}-${suffix}.webp`
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 - 요청 이벤트
@@ -104,9 +133,8 @@ export default defineEventHandler(async (event) => {
await mkdir(directoryPath, { recursive: true })
const originalName = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'avatar'
const fileName = `${originalName}-${randomUUID()}.webp`
const filePath = join(directoryPath, fileName)
const originalStem = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'avatar'
const { fileName, filePath } = await pickUniqueWebpFileName(directoryPath, originalStem)
const avatarUrl = `${uploadBaseUrl}/members/avatars/${year}/${month}/${fileName}`
const metadata = await sharp(file.data).metadata()