yona#37 파일 업로드 클라이언트, 서버단 사전 검증으로 적용

This commit is contained in:
Rinjae
2025-12-01 11:03:58 +09:00
parent 6501ace1af
commit 609984ae94
12 changed files with 231 additions and 57 deletions
+80
View File
@@ -64,6 +64,86 @@ $(function(){
});
});
/**
* 파일 검증 유틸리티
* PORTAL_CONFIG.file 설정값 사용
*/
const FileValidator = {
/**
* 파일 크기 검증
* @param {File} file - 검증할 파일
* @returns {boolean} 유효 여부
*/
validateFileSize: function(file) {
const maxSizeBytes = window.PORTAL_CONFIG?.file?.maxSizeBytes || 8388608;
if (file.size > maxSizeBytes) {
const maxSize = window.PORTAL_CONFIG?.file?.maxSize || '8MB';
customPopups.showAlert('파일 크기는 ' + maxSize + '를 초과할 수 없습니다.\n선택한 파일: ' + this.formatFileSize(file.size));
return false;
}
return true;
},
/**
* 파일 확장자 검증
* @param {File} file - 검증할 파일
* @returns {boolean} 유효 여부
*/
validateFileExtension: function(file) {
const allowedExtensions = (window.PORTAL_CONFIG?.file?.allowedExtensions || '').split(',');
const fileExt = file.name.split('.').pop().toLowerCase();
if (!allowedExtensions.includes(fileExt)) {
customPopups.showAlert('허용되지 않는 파일 형식입니다.\n허용된 형식: ' + allowedExtensions.join(', '));
return false;
}
return true;
},
/**
* 파일 전체 검증 (크기 + 확장자)
* @param {File} file - 검증할 파일
* @returns {boolean} 유효 여부
*/
validateFile: function(file) {
return this.validateFileExtension(file) && this.validateFileSize(file);
},
/**
* 여러 파일 검증
* @param {FileList} files - 검증할 파일 목록
* @returns {boolean} 모두 유효 여부
*/
validateFiles: function(files) {
for (let i = 0; i < files.length; i++) {
if (!this.validateFile(files[i])) {
return false;
}
}
return true;
},
/**
* 바이트를 읽기 쉬운 형식으로 변환
* @param {number} bytes
* @returns {string}
*/
formatFileSize: function(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
/**
* 최대 허용 용량 반환 (표시용)
* @returns {string}
*/
getMaxSizeDisplay: function() {
return window.PORTAL_CONFIG?.file?.maxSize || '8MB';
}
};
class FormValidator {
constructor(selector, validateFunction, message, extraValidation) {
this.form = document.querySelector(selector);