diff --git a/libs/basic/utils.js b/libs/basic/utils.js index ef66d23a..7bcaf77d 100644 --- a/libs/basic/utils.js +++ b/libs/basic/utils.js @@ -1,4 +1,5 @@ const fs = require('fs'); +const fsP = require('fs').promises; const path = require('path'); const moment = require('moment'); const fetch = require('node-fetch'); @@ -240,6 +241,17 @@ module.exports = (processCwd,config) => { } } } + async function deleteFilesInFolder(folderPath) { + try { + const files = await fsP.readdir(folderPath); + for (const file of files) { + const filePath = path.join(folderPath, file); + await fsP.rm(filePath, { recursive: true }); + } + } catch (error) { + console.error(`Error deleting files: ${error.message}`); + } + } return { parseJSON: parseJSON, stringJSON: stringJSON, @@ -261,5 +273,6 @@ module.exports = (processCwd,config) => { copyFile: copyFile, hmsToSeconds, setDefaultIfUndefined, + deleteFilesInFolder, } } diff --git a/libs/dropInEvents.js b/libs/dropInEvents.js index 8acbaccb..d4d7b9c5 100644 --- a/libs/dropInEvents.js +++ b/libs/dropInEvents.js @@ -26,6 +26,9 @@ module.exports = function(s,config,lang,app,io){ const { triggerEvent, } = require('./events/utils.js')(s,config,lang) + const { + deleteFilesInFolder, + } = require('./basic/utils.js')(process.cwd(), config) if(config.dropInEventServer === true){ if(config.dropInEventForceSaveEvent === undefined)config.dropInEventForceSaveEvent = true if(config.dropInEventDeleteFileAfterTrigger === undefined)config.dropInEventDeleteFileAfterTrigger = true @@ -142,7 +145,7 @@ module.exports = function(s,config,lang,app,io){ if(config.dropInEventDeleteFileAfterTrigger){ clearTimeout(fileQueue[filePath]) fileQueue[filePath] = setTimeout(function(){ - exec('rm -rf ' + filePath,function(err){ + fs.rm(filePath, { recursive: true },(err) => { delete(fileQueue[filePath]) }) },1000 * 60 * 5) @@ -152,7 +155,7 @@ module.exports = function(s,config,lang,app,io){ if(config.dropInEventDeleteFileAfterTrigger){ clearTimeout(fileQueueForDeletion[deletionKey]) fileQueueForDeletion[deletionKey] = setTimeout(function(){ - exec('rm -rf ' + deletionKey,function(err){ + fs.rm(filePath, { recursive: true },(err) => { delete(fileQueueForDeletion[deletionKey]) }) },1000 * 60 * 5) @@ -194,7 +197,7 @@ module.exports = function(s,config,lang,app,io){ directory = s.dir.dropInEvents + e.ke + '/' + (e.id || e.mid) + '/' fs.mkdir(directory,function(err){ s.handleFolderError(err) - exec('rm -rf "' + directory + '*"',function(){}) + deleteFilesInFolder(directory) callback(err,directory) }) })