From 5459e466bd06e0af4c3b4e5b1b3af26c3c4c1ba3 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Mon, 28 Jul 2025 21:30:00 +0700 Subject: [PATCH] feat: not allowed file types --- .../src/components/media/new.uploader.tsx | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/apps/frontend/src/components/media/new.uploader.tsx b/apps/frontend/src/components/media/new.uploader.tsx index 6260f695..ba1e82a4 100644 --- a/apps/frontend/src/components/media/new.uploader.tsx +++ b/apps/frontend/src/components/media/new.uploader.tsx @@ -76,7 +76,74 @@ export function useUppyUploader(props: { }, }); - // Custom file size validation based on file type + // check for valid file types it can be something like this image/*,video/mp4. + // If it's an image, I need to replace image/* with image/png, image/jpeg, image/jpeg, image/gif (separately) + uppy2.addPreProcessor((fileIDs) => { + return new Promise((resolve, reject) => { + const files = uppy2.getFiles(); + const allowedTypes = allowedFileTypes + .split(',') + .map((type) => type.trim()); + + // Expand generic types to specific ones + const expandedTypes = allowedTypes.flatMap((type) => { + if (type === 'image/*') { + return [ + 'image/png', + 'image/jpeg', + 'image/jpg', + 'image/gif', + 'image/webp', + 'image/svg+xml', + ]; + } + if (type === 'video/*') { + return [ + 'video/mp4', + 'video/mpeg', + 'video/quicktime', + 'video/x-msvideo', + 'video/webm', + ]; + } + if (type === 'audio/*') { + return ['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp3']; + } + return [type]; + }); + + for (const file of files) { + if (fileIDs.includes(file.id)) { + const fileType = file.type; + + // Check if file type is allowed + const isAllowed = expandedTypes.some((allowedType) => { + if (allowedType.endsWith('/*')) { + const baseType = allowedType.replace('/*', '/'); + return fileType?.startsWith(baseType); + } + return fileType === allowedType; + }); + + if (!isAllowed) { + const error = new Error( + `File type "${fileType}" is not allowed for file "${file.name}". Allowed types: ${allowedFileTypes}` + ); + uppy2.log(error.message, 'error'); + uppy2.info(error.message, 'error', 5000); + toast.show( + `File type "${fileType}" is not allowed. Allowed types: ${allowedFileTypes}` + ); + uppy2.removeFile(file.id); + return reject(error); + } + } + } + + resolve(); + }); + }); + uppy2.addPreProcessor((fileIDs) => { return new Promise((resolve, reject) => { const files = uppy2.getFiles();