Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/global/core/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,33 @@ export const getUploadFileType = ({
}
return types.join(', ');
};

export const isFileTypeAllowedByAccept = (file: Pick<File, 'name' | 'type'>, accept: string) => {
const acceptTypes = accept
.split(',')
.map((item) => item.trim().toLowerCase())
.filter(Boolean);

if (acceptTypes.length === 0) return true;

const filename = file.name.toLowerCase();
const mimeType = file.type.toLowerCase();

return acceptTypes.some((acceptType) => {
if (acceptType === '*' || acceptType === '*/*') return true;

if (acceptType.startsWith('.')) {
return filename.endsWith(acceptType);
}

if (acceptType.endsWith('/*')) {
return mimeType.startsWith(acceptType.slice(0, -1));
}

if (acceptType.includes('/')) {
return mimeType === acceptType;
}

return filename.endsWith(`.${acceptType}`);
});
};
31 changes: 31 additions & 0 deletions packages/global/test/core/app/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest';
import { isFileTypeAllowedByAccept } from '@fastgpt/global/core/app/constants';

const createFile = ({ name, type = '' }: { name: string; type?: string }) => ({ name, type });

describe('isFileTypeAllowedByAccept', () => {
it('accepts extension matches without relying on browser MIME type', () => {
expect(isFileTypeAllowedByAccept(createFile({ name: 'report.DOCX' }), '.pdf, .docx')).toBe(
true
);
});

it('accepts MIME wildcard matches', () => {
expect(
isFileTypeAllowedByAccept(createFile({ name: 'photo', type: 'image/png' }), 'image/*')
).toBe(true);
});

it('accepts custom extensions without a leading dot', () => {
expect(isFileTypeAllowedByAccept(createFile({ name: 'data.jsonl' }), 'jsonl')).toBe(true);
});

it('rejects unmatched files', () => {
expect(
isFileTypeAllowedByAccept(
createFile({ name: 'movie.mp4', type: 'video/mp4' }),
'.pdf, image/*'
)
).toBe(false);
});
});
2 changes: 1 addition & 1 deletion pro
Submodule pro updated from 469721 to a10974
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dynamic from 'next/dynamic';
import { useContextSelector } from 'use-context-selector';
import { WorkflowRuntimeContext } from '../../context/workflowRuntimeContext';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
import { documentFileType } from '@fastgpt/global/common/file/constants';
import { isFileTypeAllowedByAccept } from '@fastgpt/global/core/app/constants';
import FilePreview from '../../components/FilePreview';
import { useFileUpload } from '../hooks/useFileUpload';
import ComplianceTip from '@/components/common/ComplianceTip/index';
Expand All @@ -25,13 +25,6 @@ import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workf

const InputGuideBox = dynamic(() => import('./InputGuideBox'));

const fileTypeFilter = (file: File) => {
return (
file.type.includes('image') ||
documentFileType.split(',').some((type) => file.name.endsWith(type.trim()))
);
};

const ChatInput = ({
lastInteractive,
onSendMessage,
Expand Down Expand Up @@ -84,6 +77,7 @@ const ChatInput = ({
uploadFiles,
selectFileIcon,
selectFileLabel,
fileType,
showSelectFile,
showSelectImg,
showSelectVideo,
Expand All @@ -107,6 +101,10 @@ const ChatInput = ({
showSelectVideo ||
showSelectAudio ||
showSelectCustomFileExtension;
const fileTypeFilter = useCallback(
(file: File) => isFileTypeAllowedByAccept(file, fileType),
[fileType]
);

// Upload files
useRequest(uploadFiles, {
Expand Down Expand Up @@ -263,6 +261,7 @@ const ChatInput = ({
setValue,
handleSend,
canUploadFile,
fileTypeFilter,
onSelectFile
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export const useFileUpload = (props: UseFileUploadOptions) => {
uploadFiles,
selectFileIcon,
selectFileLabel,
fileType,
showSelectFile,
showSelectImg,
showSelectVideo,
Expand Down
15 changes: 7 additions & 8 deletions projects/app/src/components/core/chat/HelperBot/Chatinput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ import dynamic from 'next/dynamic';
import { useContextSelector } from 'use-context-selector';
import { WorkflowRuntimeContext } from '../ChatContainer/context/workflowRuntimeContext';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
import { documentFileType } from '@fastgpt/global/common/file/constants';
import { isFileTypeAllowedByAccept } from '@fastgpt/global/core/app/constants';
import FilePreview from '../ChatContainer/components/FilePreview';
import { useFileUpload } from './hooks/useFileUpload';
import ComplianceTip from '@/components/common/ComplianceTip/index';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { HelperBotContext } from './context';
import type { onSendMessageFnType } from './type';

const fileTypeFilter = (file: File) => {
return (
file.type.includes('image') ||
documentFileType.split(',').some((type) => file.name.endsWith(type.trim()))
);
};

const ChatInput = ({
chatId,
onSendMessage,
Expand Down Expand Up @@ -69,6 +62,7 @@ const ChatInput = ({
onSelectFile,
selectFileIcon,
selectFileLabel,
fileType,
showSelectFile,
showSelectImg,
showSelectVideo,
Expand All @@ -91,6 +85,10 @@ const ChatInput = ({
showSelectVideo ||
showSelectAudio ||
showSelectCustomFileExtension;
const fileTypeFilter = useCallback(
(file: File) => isFileTypeAllowedByAccept(file, fileType),
[fileType]
);

/* on send */
const handleSend = useCallback(
Expand Down Expand Up @@ -224,6 +222,7 @@ const ChatInput = ({
setValue,
handleSend,
canUploadFile,
fileTypeFilter,
onSelectFile
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export const useFileUpload = (props: UseFileUploadOptions) => {
uploadFiles,
selectFileIcon,
selectFileLabel,
fileType,
showSelectFile,
showSelectImg,
showSelectVideo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import MyBox from '@fastgpt/web/components/common/MyBox';
import { useForm, useFieldArray } from 'react-hook-form';
import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { documentFileType } from '@fastgpt/global/common/file/constants';
import { isFileTypeAllowedByAccept } from '@fastgpt/global/core/app/constants';
import type { PreviewInputFormType, UserInputFileItemType } from './type';
import { textareaMinH } from './constants';
import { usePreviewFileUpload } from './usePreviewFileUpload';
import FilePreview from './FilePreview';
import { ACCEPTED_FILE_TYPE } from './usePreviewFileUpload';

type PreviewInputProps = {
appId: string;
Expand All @@ -20,9 +21,7 @@ type PreviewInputProps = {
onStop: () => void;
};

const fileTypeFilter = (file: File) =>
file.type.includes('image') ||
documentFileType.split(',').some((type) => file.name.endsWith(type.trim()));
const fileTypeFilter = (file: File) => isFileTypeAllowedByAccept(file, ACCEPTED_FILE_TYPE);

const PreviewInput = ({ appId, isChatting, onSend, onStop }: PreviewInputProps) => {
const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { putFileToS3 } from '@fastgpt/web/common/file/utils';
import type { PreviewInputFormType, UserInputFileItemType } from './type';

// 支持图片 + 文档,固定配置
const ACCEPTED_FILE_TYPE = 'image/*, .txt, .docx, .csv, .xlsx, .pdf, .md, .html, .pptx';
export const ACCEPTED_FILE_TYPE = 'image/*, .txt, .docx, .csv, .xlsx, .pdf, .md, .html, .pptx';

type UsePreviewFileUploadOptions = {
fileCtrl: UseFieldArrayReturn<PreviewInputFormType, 'files', 'id'>;
Expand Down
Loading