diff --git a/packages/surveys/src/components/general/file-input.test.tsx b/packages/surveys/src/components/general/file-input.test.tsx index bd78fd7476..51a3449a8d 100644 --- a/packages/surveys/src/components/general/file-input.test.tsx +++ b/packages/surveys/src/components/general/file-input.test.tsx @@ -44,7 +44,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={1} allowMultipleFiles={true} /> @@ -55,7 +55,7 @@ describe("FileInput", () => { await waitFor(() => { expect(onFileUpload).toHaveBeenCalledWith(expect.objectContaining({ name: "test.txt" }), { - allowedFileExtensions: ["plain"], + allowedFileExtensions: ["txt"], surveyId: "survey1", }); expect(onUploadCallback).toHaveBeenCalledWith(["uploaded-url"]); @@ -108,7 +108,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={initialUrls} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); @@ -128,7 +128,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={["dup.txt"]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); @@ -151,7 +151,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={1} allowMultipleFiles={true} /> @@ -185,7 +185,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={1} allowMultipleFiles={true} /> @@ -242,7 +242,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={["dup.txt"]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); @@ -274,7 +274,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={0.5} // 500KB limit allowMultipleFiles={true} /> @@ -311,7 +311,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={0.1} // Very small limit to ensure filtering allowMultipleFiles={true} /> @@ -339,7 +339,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={initialUrls} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); @@ -359,7 +359,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); @@ -402,7 +402,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} maxSizeInMB={1} allowMultipleFiles={true} /> @@ -426,7 +426,7 @@ describe("FileInput", () => { onFileUpload={onFileUpload} onUploadCallback={onUploadCallback} fileUrls={[]} - allowedFileExtensions={["plain"]} + allowedFileExtensions={["txt"]} allowMultipleFiles={true} /> ); diff --git a/packages/surveys/src/components/general/file-input.tsx b/packages/surveys/src/components/general/file-input.tsx index 6bc0cf3f18..5e1a2cfc1f 100644 --- a/packages/surveys/src/components/general/file-input.tsx +++ b/packages/surveys/src/components/general/file-input.tsx @@ -4,7 +4,7 @@ import { getMimeType, isFulfilled, isRejected } from "@/lib/utils"; import { useAutoAnimate } from "@formkit/auto-animate/react"; import { useCallback, useEffect, useMemo, useState } from "preact/hooks"; import { type JSXInternal } from "preact/src/jsx"; -import { type TAllowedFileExtension } from "@formbricks/types/common"; +import { type TAllowedFileExtension, ZAllowedFileExtension } from "@formbricks/types/common"; import { type TJsFileUploadParams } from "@formbricks/types/js"; import { type TUploadFileConfig } from "@formbricks/types/storage"; @@ -182,11 +182,14 @@ export function FileInput({ // filter out files that are not allowed const validFiles = fileArray.filter((file) => { - const fileExtension = file.type.substring(file.type.lastIndexOf("/") + 1) as TAllowedFileExtension; + const fileExtension = file.name.split(".").pop()?.toLowerCase() as TAllowedFileExtension; + if (!fileExtension || fileExtension === file.name.toLowerCase()) return false; + if (allowedFileExtensions) { return allowedFileExtensions.includes(fileExtension); } - return true; + + return Object.values(ZAllowedFileExtension.enum).includes(fileExtension); }); if (!validFiles.length) { diff --git a/packages/types/common.ts b/packages/types/common.ts index baf44b9106..b212e8519f 100644 --- a/packages/types/common.ts +++ b/packages/types/common.ts @@ -25,13 +25,14 @@ export const ZAllowedFileExtension = z.enum([ "jpg", "webp", "pdf", + "eml", "doc", "docx", "xls", "xlsx", "ppt", "pptx", - "plain", + "txt", "csv", "mp4", "mov", @@ -51,13 +52,14 @@ export const mimeTypes: Record = { jpg: "image/jpeg", webp: "image/webp", pdf: "application/pdf", + eml: "message/rfc822", doc: "application/msword", docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", xls: "application/vnd.ms-excel", xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ppt: "application/vnd.ms-powerpoint", pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation", - plain: "text/plain", + txt: "text/plain", csv: "text/csv", mp4: "video/mp4", mov: "video/quicktime",