[as] feat: 파일 업로드 확장자 제한 정책 적용
This commit is contained in:
@@ -32,6 +32,28 @@ interface UseTusReturn {
|
||||
uploads: Record<string, Upload>;
|
||||
}
|
||||
|
||||
const ALLOWED_EXTENSIONS: readonly string[] = [
|
||||
// 문서
|
||||
".pdf",
|
||||
".txt",
|
||||
".hwp",
|
||||
".hwpx",
|
||||
".doc",
|
||||
".docx",
|
||||
".xls",
|
||||
".xlsx",
|
||||
".ppt",
|
||||
".pptx",
|
||||
// 이미지
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".png",
|
||||
".gif",
|
||||
".bmp",
|
||||
// 압축
|
||||
".zip",
|
||||
];
|
||||
|
||||
function getDefaultFileKey(file: File) {
|
||||
return `${file.name}::${file.lastModified}::${file.size}`;
|
||||
}
|
||||
@@ -44,12 +66,16 @@ function useTus(options: UseTusOptions = {}): UseTusReturn {
|
||||
|
||||
const validateFileType = useCallback(
|
||||
(file: File): boolean => {
|
||||
if (!options.accept) return true;
|
||||
|
||||
const allowedExtensions = options.accept.split(",");
|
||||
const fileExtension = file.name.substring(file.name.lastIndexOf(".")).toLowerCase();
|
||||
const acceptExtensions = options.accept
|
||||
?.split(",")
|
||||
.map((extension) => extension.trim().toLowerCase());
|
||||
|
||||
if (!allowedExtensions.includes(fileExtension)) {
|
||||
const isAllowed =
|
||||
ALLOWED_EXTENSIONS.includes(fileExtension) &&
|
||||
(!acceptExtensions || acceptExtensions.includes(fileExtension));
|
||||
|
||||
if (!isAllowed) {
|
||||
alert(`허용되지 않은 파일 형식입니다. (${fileExtension})`);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user