mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-08 00:40:09 -06:00
fix: alert message on invalid file in file upload question (#6431)
This commit is contained in:
@@ -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}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user