From b7cd38c83d4631054cec21e59fa6f8c17944e26f Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 9 Nov 2023 16:12:26 +0000 Subject: [PATCH] fix when directory is not created --- .../agents/OpenAIAssistant/OpenAIAssistant.ts | 8 ++++++-- packages/server/src/index.ts | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/components/nodes/agents/OpenAIAssistant/OpenAIAssistant.ts b/packages/components/nodes/agents/OpenAIAssistant/OpenAIAssistant.ts index 21b2ce6b7..7c353492f 100644 --- a/packages/components/nodes/agents/OpenAIAssistant/OpenAIAssistant.ts +++ b/packages/components/nodes/agents/OpenAIAssistant/OpenAIAssistant.ts @@ -253,9 +253,10 @@ class OpenAIAssistant_Agents implements INode { const content = assistantMessages[0].content[i] as MessageContentImageFile const fileId = content.image_file.file_id const fileObj = await openai.files.retrieve(fileId) + const dirPath = path.join(getUserHome(), '.flowise', 'openai-assistant') const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', `${fileObj.filename}.png`) - await downloadFile(fileObj, filePath, openAIApiKey) + await downloadFile(fileObj, filePath, dirPath, openAIApiKey) const bitmap = fsDefault.readFileSync(filePath) const base64String = Buffer.from(bitmap).toString('base64') @@ -276,7 +277,7 @@ class OpenAIAssistant_Agents implements INode { } } -const downloadFile = async (fileObj: any, filePath: string, openAIApiKey: string) => { +const downloadFile = async (fileObj: any, filePath: string, dirPath: string, openAIApiKey: string) => { try { const response = await fetch(`https://api.openai.com/v1/files/${fileObj.id}/content`, { method: 'GET', @@ -288,6 +289,9 @@ const downloadFile = async (fileObj: any, filePath: string, openAIApiKey: string } await new Promise((resolve, reject) => { + if (!fsDefault.existsSync(dirPath)) { + fsDefault.mkdirSync(path.dirname(filePath), { recursive: true }) + } const dest = fsDefault.createWriteStream(filePath) response.body.pipe(dest) response.body.on('end', () => resolve()) diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index d10c29037..98ad7ea71 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -750,7 +750,12 @@ export class App { const filename = splitDataURI.pop()?.split(':')[1] ?? '' const bf = Buffer.from(splitDataURI.pop() || '', 'base64') const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', filename) - if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, bf) + if (!fs.existsSync(path.join(getUserHome(), '.flowise', 'openai-assistant'))) { + fs.mkdirSync(path.dirname(filePath), { recursive: true }) + } + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, bf) + } const createdFile = await openai.files.create({ file: fs.createReadStream(filePath), @@ -858,7 +863,12 @@ export class App { const filename = splitDataURI.pop()?.split(':')[1] ?? '' const bf = Buffer.from(splitDataURI.pop() || '', 'base64') const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', filename) - if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, bf) + if (!fs.existsSync(path.join(getUserHome(), '.flowise', 'openai-assistant'))) { + fs.mkdirSync(path.dirname(filePath), { recursive: true }) + } + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, bf) + } const createdFile = await openai.files.create({ file: fs.createReadStream(filePath),