fix: alert message on invalid file in file upload question (#6431)

This commit is contained in:
Anshuman Pandey
2025-08-19 18:12:31 +05:30
committed by GitHub
parent 59859d0e4f
commit 3eb18bb120
3 changed files with 23 additions and 18 deletions

View File

@@ -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}
/>
);

View File

@@ -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) {

View File

@@ -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<TAllowedFileExtension, string> = {
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",