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
/* Delete all multimodal uploads corresponding to this chatflow */ try {
const directory = path.join(getUserHome(), '.flowise', 'gptvision', chatflowid) /* Delete all multimodal uploads corresponding to this chatflow */
deleteFolderRecursive(directory) 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) 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,23 +1080,25 @@ export const getAllValuesFromJson = (obj: any): any[] => {
} }
export const deleteFolderRecursive = (directory: string) => { export const deleteFolderRecursive = (directory: string) => {
fs.readdir(directory, (error, files) => { if (fs.existsSync(directory)) {
if (error) throw new Error('Could not read directory') fs.readdir(directory, (error, files) => {
if (error) throw new Error('Could not read directory')
files.forEach((file) => { files.forEach((file) => {
const file_path = path.join(directory, file) const file_path = path.join(directory, file)
fs.stat(file_path, (error, stat) => { fs.stat(file_path, (error, stat) => {
if (error) throw new Error('File do not exist') if (error) throw new Error('File do not exist')
if (!stat.isDirectory()) { if (!stat.isDirectory()) {
fs.unlink(file_path, (error) => { fs.unlink(file_path, (error) => {
if (error) throw new Error('Could not delete file') if (error) throw new Error('Could not delete file')
}) })
} else { } else {
deleteFolderRecursive(file_path) deleteFolderRecursive(file_path)
} }
})
}) })
}) })
}) }
} }