From bf478aeba26b6ab760917f36a772307d3fdbbd59 Mon Sep 17 00:00:00 2001 From: KVOJJJin Date: Wed, 23 Oct 2024 17:35:07 +0800 Subject: [PATCH] Revert "Feat: use file size limit from api" (#9714) --- .../base/file-uploader/constants.ts | 4 - .../components/base/file-uploader/hooks.ts | 116 ++---------------- .../_base/components/file-upload-setting.tsx | 10 +- web/i18n/en-US/app-debug.ts | 2 +- web/i18n/en-US/common.ts | 2 +- web/i18n/zh-Hans/app-debug.ts | 2 +- web/i18n/zh-Hans/common.ts | 2 +- web/models/common.ts | 6 +- 8 files changed, 17 insertions(+), 127 deletions(-) diff --git a/web/app/components/base/file-uploader/constants.ts b/web/app/components/base/file-uploader/constants.ts index 629fe2566b..e6cc2995f9 100644 --- a/web/app/components/base/file-uploader/constants.ts +++ b/web/app/components/base/file-uploader/constants.ts @@ -1,7 +1,3 @@ -// fallback for file size limit of dify_config -export const IMG_SIZE_LIMIT = 10 * 1024 * 1024 export const FILE_SIZE_LIMIT = 15 * 1024 * 1024 -export const AUDIO_SIZE_LIMIT = 50 * 1024 * 1024 -export const VIDEO_SIZE_LIMIT = 100 * 1024 * 1024 export const FILE_URL_REGEX = /^(https?|ftp):\/\// diff --git a/web/app/components/base/file-uploader/hooks.ts b/web/app/components/base/file-uploader/hooks.ts index ed299eca3e..5e126a87b5 100644 --- a/web/app/components/base/file-uploader/hooks.ts +++ b/web/app/components/base/file-uploader/hooks.ts @@ -3,7 +3,6 @@ import { useCallback, useState, } from 'react' -import useSWR from 'swr' import { useParams } from 'next/navigation' import produce from 'immer' import { v4 as uuid4 } from 'uuid' @@ -15,113 +14,19 @@ import { getSupportFileType, isAllowedFileExtension, } from './utils' -import { - AUDIO_SIZE_LIMIT, - FILE_SIZE_LIMIT, - IMG_SIZE_LIMIT, - VIDEO_SIZE_LIMIT, -} from '@/app/components/base/file-uploader/constants' +import { FILE_SIZE_LIMIT } from './constants' import { useToastContext } from '@/app/components/base/toast' import { TransferMethod } from '@/types/app' import { SupportUploadFileTypes } from '@/app/components/workflow/types' import type { FileUpload } from '@/app/components/base/features/types' import { formatFileSize } from '@/utils/format' -import { fetchFileUploadConfig, fetchRemoteFileInfo } from '@/service/common' - -export const useFileSizeLimit = () => { - const { data: fileUploadConfigResponse } = useSWR({ url: '/files/upload' }, fetchFileUploadConfig) - const imgSizeLimit = Number(fileUploadConfigResponse?.image_file_size_limit) * 1024 * 1024 || IMG_SIZE_LIMIT - const docSizeLimit = Number(fileUploadConfigResponse?.file_size_limit) * 1024 * 1024 || FILE_SIZE_LIMIT - const audioSizeLimit = Number(fileUploadConfigResponse?.audio_file_size_limit) * 1024 * 1024 || AUDIO_SIZE_LIMIT - const videoSizeLimit = Number(fileUploadConfigResponse?.video_file_size_limit) * 1024 * 1024 || VIDEO_SIZE_LIMIT - - return { - imgSizeLimit, - docSizeLimit, - audioSizeLimit, - videoSizeLimit, - } -} +import { fetchRemoteFileInfo } from '@/service/common' export const useFile = (fileConfig: FileUpload) => { const { t } = useTranslation() const { notify } = useToastContext() const fileStore = useFileStore() const params = useParams() - const { imgSizeLimit, docSizeLimit, audioSizeLimit, videoSizeLimit } = useFileSizeLimit() - - const checkSizeLimit = (fileType: string, fileSize: number) => { - switch (fileType) { - case SupportUploadFileTypes.image: { - if (fileSize > imgSizeLimit) { - notify({ - type: 'error', - message: t('common.fileUploader.uploadFromComputerLimit', { - type: SupportUploadFileTypes.image, - size: formatFileSize(imgSizeLimit), - }), - }) - return false - } - return true - } - case SupportUploadFileTypes.document: { - if (fileSize > docSizeLimit) { - notify({ - type: 'error', - message: t('common.fileUploader.uploadFromComputerLimit', { - type: SupportUploadFileTypes.document, - size: formatFileSize(docSizeLimit), - }), - }) - return false - } - return true - } - case SupportUploadFileTypes.audio: { - if (fileSize > audioSizeLimit) { - notify({ - type: 'error', - message: t('common.fileUploader.uploadFromComputerLimit', { - type: SupportUploadFileTypes.audio, - size: formatFileSize(audioSizeLimit), - }), - }) - return false - } - return true - } - case SupportUploadFileTypes.video: { - if (fileSize > videoSizeLimit) { - notify({ - type: 'error', - message: t('common.fileUploader.uploadFromComputerLimit', { - type: SupportUploadFileTypes.video, - size: formatFileSize(videoSizeLimit), - }), - }) - return false - } - return true - } - case SupportUploadFileTypes.custom: { - if (fileSize > docSizeLimit) { - notify({ - type: 'error', - message: t('common.fileUploader.uploadFromComputerLimit', { - type: SupportUploadFileTypes.document, - size: formatFileSize(docSizeLimit), - }), - }) - return false - } - return true - } - default: { - return true - } - } - } const handleAddFile = useCallback((newFile: FileEntity) => { const { @@ -212,15 +117,12 @@ export const useFile = (fileConfig: FileUpload) => { progress: 100, supportFileType: getSupportFileType(url, res.file_type, allowedFileTypes?.includes(SupportUploadFileTypes.custom)), } - if (!checkSizeLimit(newFile.supportFileType, newFile.size)) - handleRemoveFile(uploadingFile.id) - else - handleUpdateFile(newFile) + handleUpdateFile(newFile) }).catch(() => { notify({ type: 'error', message: t('common.fileUploader.pasteFileLinkInvalid') }) handleRemoveFile(uploadingFile.id) }) - }, [checkSizeLimit, handleAddFile, handleUpdateFile, notify, t, handleRemoveFile, fileConfig?.allowed_file_types]) + }, [handleAddFile, handleUpdateFile, notify, t, handleRemoveFile, fileConfig?.allowed_file_types]) const handleLoadFileFromLinkSuccess = useCallback(() => { }, []) @@ -238,13 +140,13 @@ export const useFile = (fileConfig: FileUpload) => { notify({ type: 'error', message: t('common.fileUploader.fileExtensionNotSupport') }) return } - const allowedFileTypes = fileConfig.allowed_file_types - const fileType = getSupportFileType(file.name, file.type, allowedFileTypes?.includes(SupportUploadFileTypes.custom)) - if (!checkSizeLimit(fileType, file.size)) + if (file.size > FILE_SIZE_LIMIT) { + notify({ type: 'error', message: t('common.fileUploader.uploadFromComputerLimit', { size: formatFileSize(FILE_SIZE_LIMIT) }) }) return - + } const reader = new FileReader() const isImage = file.type.startsWith('image') + const allowedFileTypes = fileConfig.allowed_file_types reader.addEventListener( 'load', @@ -285,7 +187,7 @@ export const useFile = (fileConfig: FileUpload) => { false, ) reader.readAsDataURL(file) - }, [checkSizeLimit, notify, t, handleAddFile, handleUpdateFile, params.token, fileConfig?.allowed_file_types, fileConfig?.allowed_file_extensions]) + }, [notify, t, handleAddFile, handleUpdateFile, params.token, fileConfig?.allowed_file_types, fileConfig?.allowed_file_extensions]) const handleClipboardPasteFile = useCallback((e: ClipboardEvent) => { const file = e.clipboardData?.files[0] diff --git a/web/app/components/workflow/nodes/_base/components/file-upload-setting.tsx b/web/app/components/workflow/nodes/_base/components/file-upload-setting.tsx index 72df3b20d2..77ff2fd5c1 100644 --- a/web/app/components/workflow/nodes/_base/components/file-upload-setting.tsx +++ b/web/app/components/workflow/nodes/_base/components/file-upload-setting.tsx @@ -10,7 +10,7 @@ import FileTypeItem from './file-type-item' import InputNumberWithSlider from './input-number-with-slider' import Field from '@/app/components/app/configuration/config-var/config-modal/field' import { TransferMethod } from '@/types/app' -import { useFileSizeLimit } from '@/app/components/base/file-uploader/hooks' +import { FILE_SIZE_LIMIT } from '@/app/components/base/file-uploader/constants' import { formatFileSize } from '@/utils/format' type Props = { @@ -36,7 +36,6 @@ const FileUploadSetting: FC = ({ allowed_file_types, allowed_file_extensions, } = payload - const { imgSizeLimit, docSizeLimit, audioSizeLimit, videoSizeLimit } = useFileSizeLimit() const handleSupportFileTypeChange = useCallback((type: SupportUploadFileTypes) => { const newPayload = produce(payload, (draft) => { @@ -143,12 +142,7 @@ const FileUploadSetting: FC = ({ title={t('appDebug.variableConfig.maxNumberOfUploads')!} >
-
{t('appDebug.variableConfig.maxNumberTip', { - imgLimit: formatFileSize(imgSizeLimit), - docLimit: formatFileSize(docSizeLimit), - audioLimit: formatFileSize(audioSizeLimit), - videoLimit: formatFileSize(videoSizeLimit), - })}
+
{t('appDebug.variableConfig.maxNumberTip', { size: formatFileSize(FILE_SIZE_LIMIT) })}