From 9d72f238c0a8dc1d10a04d631d2017cf2aaebb4f Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Sun, 7 Dec 2025 15:02:36 +0800 Subject: [PATCH] fix: csv check (#6044) * fix: csv check * fix: template * Update packages/global/common/file/utils.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- packages/global/common/file/utils.ts | 6 ++++++ .../src/pages/api/core/dataset/collection/create/backup.ts | 6 +++--- .../pages/api/core/dataset/collection/create/template.ts | 6 +++--- 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 packages/global/common/file/utils.ts diff --git a/packages/global/common/file/utils.ts b/packages/global/common/file/utils.ts new file mode 100644 index 000000000..a8118bf2b --- /dev/null +++ b/packages/global/common/file/utils.ts @@ -0,0 +1,6 @@ +import path from 'path'; + +export const isCSVFile = (filename: string) => { + const extension = path.extname(filename).toLowerCase(); + return extension === '.csv'; +}; diff --git a/projects/app/src/pages/api/core/dataset/collection/create/backup.ts b/projects/app/src/pages/api/core/dataset/collection/create/backup.ts index 863a7bbb3..beda0bb53 100644 --- a/projects/app/src/pages/api/core/dataset/collection/create/backup.ts +++ b/projects/app/src/pages/api/core/dataset/collection/create/backup.ts @@ -13,7 +13,7 @@ import { } from '@fastgpt/global/core/dataset/constants'; import { i18nT } from '@fastgpt/web/i18n/utils'; import { uploadFile } from '@fastgpt/service/common/file/gridfs/controller'; -import { getFileS3Key } from '@fastgpt/service/common/s3/utils'; +import { isCSVFile } from '@fastgpt/global/common/file/utils'; export type backupQuery = {}; @@ -31,8 +31,8 @@ async function handler(req: ApiRequestProps, res: ApiRe const { file, data } = await upload.getUploadFile<{ datasetId: string }>(req, res); filePaths.push(file.path); - if (file.mimetype !== 'text/csv') { - throw new Error('File must be a CSV file'); + if (!isCSVFile(file.originalname)) { + return Promise.reject('File must be a CSV file'); } const { teamId, tmbId, dataset } = await authDataset({ diff --git a/projects/app/src/pages/api/core/dataset/collection/create/template.ts b/projects/app/src/pages/api/core/dataset/collection/create/template.ts index cac959456..8b0d84ff4 100644 --- a/projects/app/src/pages/api/core/dataset/collection/create/template.ts +++ b/projects/app/src/pages/api/core/dataset/collection/create/template.ts @@ -12,8 +12,8 @@ import { DatasetCollectionTypeEnum } from '@fastgpt/global/core/dataset/constants'; import { i18nT } from '@fastgpt/web/i18n/utils'; +import { isCSVFile } from '@fastgpt/global/common/file/utils'; import { uploadFile } from '@fastgpt/service/common/file/gridfs/controller'; -import { getFileS3Key } from '@fastgpt/service/common/s3/utils'; export type templateImportQuery = {}; @@ -34,8 +34,8 @@ async function handler( const { file, data } = await upload.getUploadFile(req, res); filePaths.push(file.path); - if (file.mimetype !== 'text/csv') { - throw new Error('File must be a CSV file'); + if (!isCSVFile(file.originalname)) { + return Promise.reject('File must be a CSV file'); } const { teamId, tmbId, dataset } = await authDataset({