Multimodal: deleting uploads on delete of all chatmessages

This commit is contained in:
vinodkiran
2024-01-31 19:16:58 -05:00
parent 5c8f48c2f1
commit aa5d1417a1
2 changed files with 28 additions and 1 deletions
+6 -1
View File
@@ -46,7 +46,8 @@ import {
getSessionChatHistory,
getAllConnectedNodes,
clearSessionMemory,
findMemoryNode
findMemoryNode,
deleteFolderRecursive
} from './utils'
import { cloneDeep, omit, uniqWith, isEqual } from 'lodash'
import { getDataSource } from './DataSource'
@@ -618,6 +619,10 @@ 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)
const results = await this.AppDataSource.getRepository(ChatMessage).delete(deleteOptions)
return res.json(results)
})
+22
View File
@@ -1078,3 +1078,25 @@ export const getAllValuesFromJson = (obj: any): any[] => {
extractValues(obj)
return values
}
export const deleteFolderRecursive = (directory: string) => {
fs.readdir(directory, (error, files) => {
if (error) throw new Error('Could not read directory')
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')
if (!stat.isDirectory()) {
fs.unlink(file_path, (error) => {
if (error) throw new Error('Could not delete file')
})
} else {
deleteFolderRecursive(file_path)
}
})
})
})
}