회원 아바타 자산을 전용 경로로 분리해 작가용 미디어 목록과 섞이지 않게 하고, 설정 화면에서 파일 업로드로 바로 반영할 수 있게 했다. Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
3.5 KiB
JavaScript
122 lines
3.5 KiB
JavaScript
import { randomUUID } from 'node:crypto'
|
|
import { mkdir, writeFile } from 'node:fs/promises'
|
|
import { extname, join } from 'node:path'
|
|
import { createError, readMultipartFormData } from 'h3'
|
|
import { getPostgresClient } from '../../repositories/postgres-client'
|
|
import { updateMemberProfile, getUserById } from '../../repositories/member-repository'
|
|
import { requireMemberSession } from '../../utils/member-auth'
|
|
|
|
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
|
|
}
|
|
|
|
/**
|
|
* 회원 썸네일 업로드 API
|
|
* @param {import('h3').H3Event} event - 요청 이벤트
|
|
* @returns {Promise<{ avatarUrl: string }>} 업로드 결과
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const session = requireMemberSession(event)
|
|
const currentUser = await getUserById(session.userId)
|
|
if (!currentUser) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
message: '회원 정보를 찾을 수 없습니다.'
|
|
})
|
|
}
|
|
|
|
const config = useRuntimeConfig()
|
|
const maxFileSize = Number(config.maxFileSize || 10485760)
|
|
const formData = await readMultipartFormData(event)
|
|
const file = (formData || []).find((part) => part.name === 'file' && part.filename)
|
|
|
|
if (!file) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '업로드할 이미지가 없습니다.'
|
|
})
|
|
}
|
|
|
|
if (!allowedImageTypes.has(file.type)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '이미지 파일만 업로드할 수 있습니다.'
|
|
})
|
|
}
|
|
|
|
if (file.data.length > maxFileSize) {
|
|
throw createError({
|
|
statusCode: 413,
|
|
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, 'members', 'avatars', year, month)
|
|
|
|
await mkdir(directoryPath, { recursive: true })
|
|
|
|
const originalName = sanitizePathPart(file.filename.replace(/\.[^.]+$/g, '')) || 'avatar'
|
|
const extension = getUploadExtension(file)
|
|
const fileName = `${originalName}-${randomUUID()}${extension}`
|
|
const filePath = join(directoryPath, fileName)
|
|
const avatarUrl = `${uploadBaseUrl}/members/avatars/${year}/${month}/${fileName}`
|
|
|
|
await writeFile(filePath, file.data)
|
|
|
|
await updateMemberProfile({
|
|
userId: session.userId,
|
|
username: currentUser.username,
|
|
avatarUrl
|
|
})
|
|
|
|
const sql = getPostgresClient()
|
|
if (sql) {
|
|
await sql`
|
|
INSERT INTO media_metadata (url, category)
|
|
VALUES (${avatarUrl}, ${'회원/썸네일'})
|
|
ON CONFLICT (url) DO UPDATE
|
|
SET
|
|
category = EXCLUDED.category,
|
|
updated_at = now()
|
|
`
|
|
}
|
|
|
|
return {
|
|
avatarUrl
|
|
}
|
|
})
|
|
|