Skip to content

Commit 5459e46

Browse files
committed
feat: not allowed file types
1 parent fa8738e commit 5459e46

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

apps/frontend/src/components/media/new.uploader.tsx

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,74 @@ export function useUppyUploader(props: {
7676
},
7777
});
7878

79-
// Custom file size validation based on file type
79+
// check for valid file types it can be something like this image/*,video/mp4.
80+
// If it's an image, I need to replace image/* with image/png, image/jpeg, image/jpeg, image/gif (separately)
81+
uppy2.addPreProcessor((fileIDs) => {
82+
return new Promise<void>((resolve, reject) => {
83+
const files = uppy2.getFiles();
84+
const allowedTypes = allowedFileTypes
85+
.split(',')
86+
.map((type) => type.trim());
87+
88+
// Expand generic types to specific ones
89+
const expandedTypes = allowedTypes.flatMap((type) => {
90+
if (type === 'image/*') {
91+
return [
92+
'image/png',
93+
'image/jpeg',
94+
'image/jpg',
95+
'image/gif',
96+
'image/webp',
97+
'image/svg+xml',
98+
];
99+
}
100+
if (type === 'video/*') {
101+
return [
102+
'video/mp4',
103+
'video/mpeg',
104+
'video/quicktime',
105+
'video/x-msvideo',
106+
'video/webm',
107+
];
108+
}
109+
if (type === 'audio/*') {
110+
return ['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp3'];
111+
}
112+
return [type];
113+
});
114+
115+
for (const file of files) {
116+
if (fileIDs.includes(file.id)) {
117+
const fileType = file.type;
118+
119+
// Check if file type is allowed
120+
const isAllowed = expandedTypes.some((allowedType) => {
121+
if (allowedType.endsWith('/*')) {
122+
const baseType = allowedType.replace('/*', '/');
123+
return fileType?.startsWith(baseType);
124+
}
125+
return fileType === allowedType;
126+
});
127+
128+
if (!isAllowed) {
129+
const error = new Error(
130+
`File type "${fileType}" is not allowed for file "${file.name}". Allowed types: ${allowedFileTypes}`
131+
);
132+
uppy2.log(error.message, 'error');
133+
uppy2.info(error.message, 'error', 5000);
134+
toast.show(
135+
`File type "${fileType}" is not allowed. Allowed types: ${allowedFileTypes}`
136+
);
137+
uppy2.removeFile(file.id);
138+
return reject(error);
139+
}
140+
}
141+
}
142+
143+
resolve();
144+
});
145+
});
146+
80147
uppy2.addPreProcessor((fileIDs) => {
81148
return new Promise<void>((resolve, reject) => {
82149
const files = uppy2.getFiles();

0 commit comments

Comments
 (0)