fix when directory is not created
This commit is contained in:
parent
4d2ef9b7ec
commit
b7cd38c83d
|
|
@ -253,9 +253,10 @@ class OpenAIAssistant_Agents implements INode {
|
||||||
const content = assistantMessages[0].content[i] as MessageContentImageFile
|
const content = assistantMessages[0].content[i] as MessageContentImageFile
|
||||||
const fileId = content.image_file.file_id
|
const fileId = content.image_file.file_id
|
||||||
const fileObj = await openai.files.retrieve(fileId)
|
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`)
|
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 bitmap = fsDefault.readFileSync(filePath)
|
||||||
const base64String = Buffer.from(bitmap).toString('base64')
|
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 {
|
try {
|
||||||
const response = await fetch(`https://api.openai.com/v1/files/${fileObj.id}/content`, {
|
const response = await fetch(`https://api.openai.com/v1/files/${fileObj.id}/content`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
|
@ -288,6 +289,9 @@ const downloadFile = async (fileObj: any, filePath: string, openAIApiKey: string
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
if (!fsDefault.existsSync(dirPath)) {
|
||||||
|
fsDefault.mkdirSync(path.dirname(filePath), { recursive: true })
|
||||||
|
}
|
||||||
const dest = fsDefault.createWriteStream(filePath)
|
const dest = fsDefault.createWriteStream(filePath)
|
||||||
response.body.pipe(dest)
|
response.body.pipe(dest)
|
||||||
response.body.on('end', () => resolve())
|
response.body.on('end', () => resolve())
|
||||||
|
|
|
||||||
|
|
@ -750,7 +750,12 @@ export class App {
|
||||||
const filename = splitDataURI.pop()?.split(':')[1] ?? ''
|
const filename = splitDataURI.pop()?.split(':')[1] ?? ''
|
||||||
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
|
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
|
||||||
const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', filename)
|
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({
|
const createdFile = await openai.files.create({
|
||||||
file: fs.createReadStream(filePath),
|
file: fs.createReadStream(filePath),
|
||||||
|
|
@ -858,7 +863,12 @@ export class App {
|
||||||
const filename = splitDataURI.pop()?.split(':')[1] ?? ''
|
const filename = splitDataURI.pop()?.split(':')[1] ?? ''
|
||||||
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
|
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
|
||||||
const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', filename)
|
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({
|
const createdFile = await openai.files.create({
|
||||||
file: fs.createReadStream(filePath),
|
file: fs.createReadStream(filePath),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue