From 880a1267a3cdb31ef8163c35c76588725f0c170e Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 9 Nov 2024 15:15:50 +0000 Subject: [PATCH] address file name with special chars --- .../src/controllers/openai-assistants-vector-store/index.ts | 2 ++ packages/server/src/controllers/openai-assistants/index.ts | 2 ++ packages/server/src/utils/buildChatflow.ts | 3 ++- packages/server/src/utils/createAttachment.ts | 4 ++++ packages/server/src/utils/upsertVector.ts | 3 ++- packages/ui/src/views/chatmessage/ChatMessage.jsx | 3 ++- 6 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/server/src/controllers/openai-assistants-vector-store/index.ts b/packages/server/src/controllers/openai-assistants-vector-store/index.ts index 55a6d154228..2b82ca0d078 100644 --- a/packages/server/src/controllers/openai-assistants-vector-store/index.ts +++ b/packages/server/src/controllers/openai-assistants-vector-store/index.ts @@ -140,6 +140,8 @@ const uploadFilesToAssistantVectorStore = async (req: Request, res: Response, ne if (Array.isArray(files)) { for (const file of files) { + // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 + file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') uploadFiles.push({ filePath: file.path, fileName: file.originalname diff --git a/packages/server/src/controllers/openai-assistants/index.ts b/packages/server/src/controllers/openai-assistants/index.ts index 448d6787bc1..94c2afe69e7 100644 --- a/packages/server/src/controllers/openai-assistants/index.ts +++ b/packages/server/src/controllers/openai-assistants/index.ts @@ -81,6 +81,8 @@ const uploadAssistantFiles = async (req: Request, res: Response, next: NextFunct if (Array.isArray(files)) { for (const file of files) { + // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 + file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') uploadFiles.push({ filePath: file.path, fileName: file.originalname diff --git a/packages/server/src/utils/buildChatflow.ts b/packages/server/src/utils/buildChatflow.ts index b3a7e906c0f..b69c2ec7557 100644 --- a/packages/server/src/utils/buildChatflow.ts +++ b/packages/server/src/utils/buildChatflow.ts @@ -161,7 +161,8 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals const fileNames: string[] = [] for (const file of files) { const fileBuffer = fs.readFileSync(file.path) - + // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 + file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid) const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype) diff --git a/packages/server/src/utils/createAttachment.ts b/packages/server/src/utils/createAttachment.ts index 544a08aa70d..317dd8b752e 100644 --- a/packages/server/src/utils/createAttachment.ts +++ b/packages/server/src/utils/createAttachment.ts @@ -42,6 +42,10 @@ export const createFileAttachment = async (req: Request) => { for (const file of files) { const fileBuffer = fs.readFileSync(file.path) const fileNames: string[] = [] + + // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 + file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') + const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid, chatId) const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype) diff --git a/packages/server/src/utils/upsertVector.ts b/packages/server/src/utils/upsertVector.ts index 8b51489726b..3342c60a610 100644 --- a/packages/server/src/utils/upsertVector.ts +++ b/packages/server/src/utils/upsertVector.ts @@ -56,7 +56,8 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) => for (const file of files) { const fileNames: string[] = [] const fileBuffer = fs.readFileSync(file.path) - + // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 + file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid) const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype) diff --git a/packages/ui/src/views/chatmessage/ChatMessage.jsx b/packages/ui/src/views/chatmessage/ChatMessage.jsx index c7ae695fa6e..15c6aa5a676 100644 --- a/packages/ui/src/views/chatmessage/ChatMessage.jsx +++ b/packages/ui/src/views/chatmessage/ChatMessage.jsx @@ -702,7 +702,6 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview if (fullFileUpload) { const filesWithFullUploadType = uploadedFiles.filter((file) => file.type === 'file:full') - if (filesWithFullUploadType.length > 0) { const formData = new FormData() for (const file of filesWithFullUploadType) { @@ -712,12 +711,14 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview const response = await attachmentsApi.createAttachment(chatflowid, chatId, formData) const data = response.data + for (const extractedFileData of data) { const content = extractedFileData.content const fileName = extractedFileData.name // find matching name in previews and replace data with content const uploadIndex = uploads.findIndex((upload) => upload.name === fileName) + if (uploadIndex !== -1) { uploads[uploadIndex] = { ...uploads[uploadIndex],