diff --git a/src/api/controllers/v1/path.controller.js b/src/api/controllers/v1/path.controller.js index 7db8cd0..1c13e3b 100644 --- a/src/api/controllers/v1/path.controller.js +++ b/src/api/controllers/v1/path.controller.js @@ -14,7 +14,19 @@ import { import uploadAdapter from '../../../common/services/adapters/upload-adapter'; import { cloneDeep, forEach } from 'lodash'; - +function deleteFolderRecursive(folderPath) { + if (fs.existsSync(folderPath)) { + fs.readdirSync(folderPath).forEach((file) => { + const curPath = path.join(folderPath, file); + if (fs.lstatSync(curPath).isDirectory()) { // delete folder + deleteFolderRecursive(curPath); // recursively call deleteFolderRecursive function + fs.rmdirSync(curPath); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + } +} /** * get file and folder * @@ -143,7 +155,7 @@ exports.download = async (req, res, next) => { const namefile = `${user.name}-${Date.now()}.zip`; const dir = `${storageConfig.uri}/download/${user.id}/${namefile}`; const folder = `${storageConfig.uri}/download/${user.id}`; - multer({ dest: `${dir}` }); + multer({ dest: `${folder}` }); await deleteFolderRecursive(folder); const output = fs.createWriteStream(dir); const archive = archiver('zip', { @@ -154,8 +166,9 @@ exports.download = async (req, res, next) => { archive.pipe(output); if (req.body.data) { req.body.data.forEach((e) => { - // const path = e.path.replace(cdnConfig.uri, storageConfig.uri).replace(/ /g, '%20'); - const path = e.path.replace(cdnConfig.uri, storageConfig.uri); + const path = e.path.replace(cdnConfig.uri, storageConfig.uri).replace(/ /g, '\\ '); + // const path = e.path.replace(cdnConfig.uri, storageConfig.uri); + console.log('path', path); // const download = path.split("/"); // download.splice( download.indexOf(`${user.id}`), 0, "download"); // const final_path = download.join("/"); @@ -166,6 +179,7 @@ exports.download = async (req, res, next) => { } }); } + archive.finalize(); return res.json({ code: 0, @@ -220,19 +234,3 @@ exports.deleteMultiple = (req, res, next) => { } }; - -function deleteFolderRecursive(folderPath) { - let files = []; - if (fs.existsSync(folderPath)) { - files = fs.readdirSync(folderPath); - files.forEach((file) => { - const currentPath = path.join(folderPath, file); - if (fs.lstatSync(currentPath).isDirectory()) { - deleteFolderRecursive(currentPath); - } else { - fs.unlinkSync(currentPath); - console.log('Deleted file:', currentPath); - } - }); - } -}