Files
sori.studio/server/routes/admin/api/uploads.post.js

247 lines
7.5 KiB
JavaScript

import { mkdir, stat, writeFile } from 'node:fs/promises'
import { extname, join } from 'node:path'
import { createError, readMultipartFormData } from 'h3'
import sharp from 'sharp'
import {
buildDefaultUploadSizeLimits,
formatUploadSizeLimit,
getMaxUploadBytesForKind,
getUploadKind,
getUploadKindLabel
} from '../../../../lib/upload-size-limit.js'
import { requireAdminSession } from '../../../utils/admin-auth'
import { getRuntimeEnvNumber } from '../../../utils/runtime-env.js'
import { upsertMediaMetadataCategory } from '../../../utils/media-library'
import {
POST_THUMBNAIL_HEIGHT,
POST_THUMBNAIL_QUALITY,
POST_THUMBNAIL_WIDTH,
getPostThumbnailFileName,
isPostThumbnailSource
} from '../../../utils/post-thumbnail-image.js'
const allowedUploadTypes = new Map([
['image/jpeg', '.jpg'],
['image/png', '.png'],
['image/webp', '.webp'],
['image/gif', '.gif'],
['video/mp4', '.mp4'],
['video/webm', '.webm'],
['video/quicktime', '.mov'],
['audio/mpeg', '.mp3'],
['audio/wav', '.wav'],
['audio/ogg', '.ogg'],
['audio/mp4', '.m4a'],
['application/pdf', '.pdf'],
['application/zip', '.zip'],
['text/plain', '.txt'],
['text/csv', '.csv'],
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.docx'],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.xlsx'],
['application/vnd.openxmlformats-officedocument.presentationml.presentation', '.pptx']
])
const allowedUploadExtensions = new Set([
'.jpg',
'.jpeg',
'.png',
'.webp',
'.gif',
'.mp4',
'.webm',
'.mov',
'.mp3',
'.wav',
'.ogg',
'.m4a',
'.pdf',
'.zip',
'.txt',
'.csv',
'.docx',
'.xlsx',
'.pptx'
])
/**
* 업로드 경로 조각을 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 (allowedUploadTypes.has(file.type)) {
return allowedUploadTypes.get(file.type)
}
return extension
}
/**
* 업로드 허용 파일인지 확인한다.
* @param {Object} file - multipart 파일 파트
* @returns {boolean} 허용 여부
*/
const isAllowedUploadFile = (file) => allowedUploadTypes.has(file.type)
|| allowedUploadExtensions.has(extname(file.filename || '').toLowerCase())
/**
* 디렉터리 안에서 비어 있는 저장 파일명을 고른다. 동일 이름이 있으면 `이름-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: '저장할 고유 파일명을 만들 수 없습니다.'
})
}
/**
* 게시물 목록용 카드 썸네일을 생성한다.
* @param {Object} options - 생성 옵션
* @param {Buffer} options.sourceBuffer - 원본 이미지 버퍼
* @param {string} options.directoryPath - 원본 이미지 저장 디렉터리
* @param {string} options.fileName - 원본 파일명
* @returns {Promise<string>} 생성된 썸네일 파일명
*/
const createPostCardThumbnail = async ({ sourceBuffer, directoryPath, fileName }) => {
const thumbnailDirectoryPath = join(directoryPath, 'thumbs')
const thumbnailFileName = getPostThumbnailFileName(fileName)
const thumbnailPath = join(thumbnailDirectoryPath, thumbnailFileName)
await mkdir(thumbnailDirectoryPath, { recursive: true })
const thumbnailBuffer = await sharp(sourceBuffer)
.rotate()
.resize({
width: POST_THUMBNAIL_WIDTH,
height: POST_THUMBNAIL_HEIGHT,
fit: 'cover',
position: 'centre',
withoutEnlargement: true
})
.webp({ quality: POST_THUMBNAIL_QUALITY })
.toBuffer()
await writeFile(thumbnailPath, thumbnailBuffer)
return thumbnailFileName
}
/**
* 관리자 미디어 업로드 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 uploadSizeLimits = buildDefaultUploadSizeLimits({
image: getRuntimeEnvNumber('MAX_FILE_SIZE', 'maxFileSize', 10485760),
video: getRuntimeEnvNumber('MAX_VIDEO_FILE_SIZE', 'maxVideoFileSize', 209715200),
audio: getRuntimeEnvNumber('MAX_AUDIO_FILE_SIZE', 'maxAudioFileSize', 52428800),
document: getRuntimeEnvNumber('MAX_DOCUMENT_FILE_SIZE', 'maxDocumentFileSize', 52428800)
})
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 (!isAllowedUploadFile(file)) {
throw createError({
statusCode: 400,
message: '지원하지 않는 파일 형식입니다.'
})
}
const uploadKind = getUploadKind(file.type, file.filename)
const maxFileSize = getMaxUploadBytesForKind(uploadKind, uploadSizeLimits)
if (file.data.length > maxFileSize) {
throw createError({
statusCode: 413,
message: `${getUploadKindLabel(uploadKind)} 업로드 가능 크기를 초과했습니다. (최대 ${formatUploadSizeLimit(maxFileSize)})`
})
}
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}`
let thumbnailUrl = ''
if (isPostThumbnailSource(file.type, fileName)) {
const thumbnailFileName = await createPostCardThumbnail({
sourceBuffer: file.data,
directoryPath,
fileName
})
thumbnailUrl = `${uploadBaseUrl}/posts/${year}/${month}/thumbs/${thumbnailFileName}`
}
await upsertMediaMetadataCategory(publicUrl, '미분류')
uploadedFiles.push({
url: publicUrl,
thumbnailUrl,
name: fileName,
size: file.data.length
})
}
return {
files: uploadedFiles
}
})