diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index b7ddac512..e655406d3 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -431,6 +431,15 @@ export class App { // Delete chatflow via id this.app.delete('/api/v1/chatflows/:id', async (req: Request, res: Response) => { const results = await this.AppDataSource.getRepository(ChatFlow).delete({ id: req.params.id }) + + try { + /* Delete all multimodal uploads corresponding to this chatflow */ + const directory = path.join(getUserHome(), '.flowise', 'gptvision', req.params.id) + deleteFolderRecursive(directory) + } catch (e) { + logger.error(`[server]: Error deleting multimodal uploads: ${e}`) + } + return res.json(results) }) @@ -619,9 +628,13 @@ export class App { if (sessionId) deleteOptions.sessionId = sessionId if (chatType) deleteOptions.chatType = chatType - /* Delete all multimodal uploads corresponding to this chatflow */ - const directory = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid) - deleteFolderRecursive(directory) + try { + /* Delete all multimodal uploads corresponding to this chatflow */ + const directory = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid) + deleteFolderRecursive(directory) + } catch (e) { + logger.error(`[server]: Error deleting multimodal uploads: ${e}`) + } const results = await this.AppDataSource.getRepository(ChatMessage).delete(deleteOptions) return res.json(results) @@ -1629,7 +1642,7 @@ export class App { for (const upload of uploads) { if (upload.type === 'file' || upload.type === 'audio') { const filename = upload.name - const dir = path.join(getUserHome(), '.flowise', 'gptvision', chatId) + const dir = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid) if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }) } @@ -1639,7 +1652,7 @@ export class App { //writes data to a file, replacing the file if it already exists. fs.writeFileSync(filePath, bf) // don't need to store the file contents in chatmessage, just the filename and chatId - upload.data = chatId + upload.data = chatflowid upload.type = 'stored-file' } diff --git a/packages/server/src/utils/index.ts b/packages/server/src/utils/index.ts index eb4e1936f..2b6a22bd2 100644 --- a/packages/server/src/utils/index.ts +++ b/packages/server/src/utils/index.ts @@ -1080,23 +1080,25 @@ export const getAllValuesFromJson = (obj: any): any[] => { } export const deleteFolderRecursive = (directory: string) => { - fs.readdir(directory, (error, files) => { - if (error) throw new Error('Could not read directory') + if (fs.existsSync(directory)) { + fs.readdir(directory, (error, files) => { + if (error) throw new Error('Could not read directory') - files.forEach((file) => { - const file_path = path.join(directory, file) + files.forEach((file) => { + const file_path = path.join(directory, file) - fs.stat(file_path, (error, stat) => { - if (error) throw new Error('File do not exist') + fs.stat(file_path, (error, stat) => { + if (error) throw new Error('File do not exist') - if (!stat.isDirectory()) { - fs.unlink(file_path, (error) => { - if (error) throw new Error('Could not delete file') - }) - } else { - deleteFolderRecursive(file_path) - } + if (!stat.isDirectory()) { + fs.unlink(file_path, (error) => { + if (error) throw new Error('Could not delete file') + }) + } else { + deleteFolderRecursive(file_path) + } + }) }) }) - }) -} \ No newline at end of file + } +}