에디터 미디어 업로드 검증 로직 분리

This commit is contained in:
2026-07-24 17:55:33 +09:00
parent 6cc820d2a2
commit e3f98cb820
7 changed files with 143 additions and 61 deletions

View File

@@ -5,6 +5,12 @@ import {
isPostCardThumbnailUrl,
withGeneratedImageCacheVersion
} from './post-card-thumbnail-url.js'
import {
formatUploadSizeLimit,
getMaxUploadBytesForKind,
getUploadFilesTotalSize,
getUploadKind
} from './upload-size-limit.js'
/**
* 파일 크기를 표시용 문자열로 변환한다.
@@ -233,3 +239,65 @@ export const resolveSelectedMediaItems = (selectedUrls, selectedItemMap, mediaIt
.map((url) => itemMap[url] || items.find((item) => item?.url === url))
.filter(Boolean)
}
/**
* 업로드 API 오류 메시지를 사용자용 문장으로 변환한다.
* @param {unknown} error - fetch 오류
* @returns {string} 오류 메시지
*/
export const resolveUploadFetchErrorMessage = (error) => {
const responseMessage = error?.data?.message || error?.response?._data?.message
if (typeof responseMessage === 'string' && responseMessage.trim()) {
return responseMessage.trim()
}
if (error?.statusCode === 413 || error?.status === 413) {
return '업로드 가능한 파일 크기를 초과했습니다.'
}
if (error instanceof Error && error.message) {
return error.message
}
return '파일 업로드에 실패했습니다.'
}
/**
* 선택 파일이 업로드 한도를 넘지 않는지 검사한다.
* @param {Object} options - 업로드 한도 검사 옵션
* @param {FileList|Array<File>} options.files - 업로드 파일 목록
* @param {number} options.maxFileCount - 최대 파일 수
* @param {number} options.maxTotalSize - 최대 전체 용량
* @param {{ image: number, video: number, audio: number, document: number }} options.uploadSizeLimits - 종류별 업로드 한도
* @returns {string|null} 초과 시 오류 메시지, 아니면 null
*/
export const findUploadSizeLimitError = ({
files,
maxFileCount,
maxTotalSize,
uploadSizeLimits
}) => {
const fileList = Array.from(files || [])
if (fileList.length > maxFileCount) {
return `한 번에 업로드할 수 있는 파일은 최대 ${maxFileCount}개입니다.`
}
const uploadTotalSize = getUploadFilesTotalSize(fileList)
if (uploadTotalSize > maxTotalSize) {
return `한 번에 업로드 가능한 전체 용량을 초과했습니다. (최대 ${formatUploadSizeLimit(maxTotalSize)})`
}
for (const file of fileList) {
const uploadKind = getUploadKind(file.type, file.name)
const maxBytes = getMaxUploadBytesForKind(uploadKind, uploadSizeLimits)
if (file.size > maxBytes) {
return `파일 크기가 너무 큽니다. (최대 ${formatUploadSizeLimit(maxBytes)})`
}
}
return null
}