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' import { upsertMediaMetadataCategory } from '../../../utils/media-library' const allowedImageTypes = new Map([ ['image/jpeg', '.jpg'], ['image/png', '.png'], ['image/webp', '.webp'], ['image/gif', '.gif'] ]) /** * 업로드 경로 조각을 URL 안전 문자열로 정리 * @param {string} value - 원본 경로 조각 * @returns {string} 정리된 경로 조각 */ const sanitizePathPart = (value) => value .replace(/[^a-zA-Z0-9가-힣._-]/g, '-') .replace(/-+/g, '-') .replace(/^-|-$/g, '') /** * 파일 확장자 조회 * @param {Object} file - multipart 파일 파트 * @returns {string} 확장자 */ const getUploadExtension = (file) => { const extension = extname(file.filename || '').toLowerCase() if (allowedImageTypes.has(file.type)) { return allowedImageTypes.get(file.type) } 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 - 요청 이벤트 * @returns {Promise<{ files: Array<{ url: string, name: string, size: number }> }>} 업로드 결과 */ export default defineEventHandler(async (event) => { requireAdminSession(event) const config = useRuntimeConfig() const maxFileSize = Number(config.maxFileSize || 10485760) const formData = await readMultipartFormData(event) const files = (formData || []).filter((part) => part.name === 'files' && part.filename) if (!files.length) { throw createError({ statusCode: 400, message: '업로드할 이미지가 없습니다.' }) } const now = new Date() const year = String(now.getFullYear()) const month = String(now.getMonth() + 1).padStart(2, '0') const uploadBaseUrl = String(config.uploadDir || '/uploads').replace(/\/+$/g, '') || '/uploads' const publicBasePath = uploadBaseUrl.replace(/^\/+/, '') const directoryPath = join(process.cwd(), 'public', publicBasePath, 'posts', year, month) await mkdir(directoryPath, { recursive: true }) const uploadedFiles = [] for (const file of files) { if (!allowedImageTypes.has(file.type)) { throw createError({ statusCode: 400, message: '이미지 파일만 업로드할 수 있습니다.' }) } if (file.data.length > maxFileSize) { throw createError({ statusCode: 413, message: '업로드 가능한 파일 크기를 초과했습니다.' }) } const originalStem = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'image' const extension = getUploadExtension(file) const { fileName, filePath } = await pickUniqueDiskFileName(directoryPath, originalStem, extension) await writeFile(filePath, file.data) const publicUrl = `${uploadBaseUrl}/posts/${year}/${month}/${fileName}` await upsertMediaMetadataCategory(publicUrl, '미분류') uploadedFiles.push({ url: publicUrl, name: fileName, size: file.data.length }) } return { files: uploadedFiles } })