Multimodal: deleting uploads on delete of all chatmessages or chatflow

This commit is contained in:
vinodkiran 2024-01-31 20:03:54 -05:00
parent aa5d1417a1
commit eab8c19f8c
2 changed files with 35 additions and 20 deletions

View File

@ -431,6 +431,15 @@ export class App {
// Delete chatflow via id // Delete chatflow via id
this.app.delete('/api/v1/chatflows/:id', async (req: Request, res: Response) => { this.app.delete('/api/v1/chatflows/:id', async (req: Request, res: Response) => {
const results = await this.AppDataSource.getRepository(ChatFlow).delete({ id: req.params.id }) 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) return res.json(results)
}) })
@ -619,9 +628,13 @@ export class App {
if (sessionId) deleteOptions.sessionId = sessionId if (sessionId) deleteOptions.sessionId = sessionId
if (chatType) deleteOptions.chatType = chatType if (chatType) deleteOptions.chatType = chatType
try {
/* Delete all multimodal uploads corresponding to this chatflow */ /* Delete all multimodal uploads corresponding to this chatflow */
const directory = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid) const directory = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid)
deleteFolderRecursive(directory) deleteFolderRecursive(directory)
} catch (e) {
logger.error(`[server]: Error deleting multimodal uploads: ${e}`)
}
const results = await this.AppDataSource.getRepository(ChatMessage).delete(deleteOptions) const results = await this.AppDataSource.getRepository(ChatMessage).delete(deleteOptions)
return res.json(results) return res.json(results)
@ -1629,7 +1642,7 @@ export class App {
for (const upload of uploads) { for (const upload of uploads) {
if (upload.type === 'file' || upload.type === 'audio') { if (upload.type === 'file' || upload.type === 'audio') {
const filename = upload.name const filename = upload.name
const dir = path.join(getUserHome(), '.flowise', 'gptvision', chatId) const dir = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid)
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true }) fs.mkdirSync(dir, { recursive: true })
} }
@ -1639,7 +1652,7 @@ export class App {
//writes data to a file, replacing the file if it already exists. //writes data to a file, replacing the file if it already exists.
fs.writeFileSync(filePath, bf) fs.writeFileSync(filePath, bf)
// don't need to store the file contents in chatmessage, just the filename and chatId // 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' upload.type = 'stored-file'
} }

View File

@ -1080,6 +1080,7 @@ export const getAllValuesFromJson = (obj: any): any[] => {
} }
export const deleteFolderRecursive = (directory: string) => { export const deleteFolderRecursive = (directory: string) => {
if (fs.existsSync(directory)) {
fs.readdir(directory, (error, files) => { fs.readdir(directory, (error, files) => {
if (error) throw new Error('Could not read directory') if (error) throw new Error('Could not read directory')
@ -1099,4 +1100,5 @@ export const deleteFolderRecursive = (directory: string) => {
}) })
}) })
}) })
}
} }