|
|
@ -12,8 +12,6 @@ import { |
|
|
|
storage as storageConfig |
|
|
|
storage as storageConfig |
|
|
|
} from '../../../config/vars'; |
|
|
|
} from '../../../config/vars'; |
|
|
|
import uploadAdapter from '../../../common/services/adapters/upload-adapter'; |
|
|
|
import uploadAdapter from '../../../common/services/adapters/upload-adapter'; |
|
|
|
import { cloneDeep, forEach } from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* get file and folder |
|
|
|
* get file and folder |
|
|
@ -25,11 +23,12 @@ exports.get = (req, res, next) => { |
|
|
|
const user = req.user; |
|
|
|
const user = req.user; |
|
|
|
|
|
|
|
|
|
|
|
let path = `${storageConfig.uri}/${user.id}`; |
|
|
|
let path = `${storageConfig.uri}/${user.id}`; |
|
|
|
|
|
|
|
// let path = storageConfig.uri;
|
|
|
|
if (req.body.path) { |
|
|
|
if (req.body.path) { |
|
|
|
path += req.body.path; |
|
|
|
path += req.body.path; |
|
|
|
} |
|
|
|
} |
|
|
|
// console.log(path);
|
|
|
|
|
|
|
|
const listFile = []; |
|
|
|
const listFile = []; |
|
|
|
|
|
|
|
|
|
|
|
fs.readdir(path, (err, files) => { |
|
|
|
fs.readdir(path, (err, files) => { |
|
|
|
if (files && files.length > 0) { |
|
|
|
if (files && files.length > 0) { |
|
|
|
files.forEach((item) => { |
|
|
|
files.forEach((item) => { |
|
|
@ -45,24 +44,6 @@ exports.get = (req, res, next) => { |
|
|
|
data: listFile |
|
|
|
data: listFile |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
// test local
|
|
|
|
|
|
|
|
// fs.readdir(path, (err, files) => {
|
|
|
|
|
|
|
|
// if (files && files.length > 0) {
|
|
|
|
|
|
|
|
// files.forEach((item) => {
|
|
|
|
|
|
|
|
// listFile.push({
|
|
|
|
|
|
|
|
// name: item,
|
|
|
|
|
|
|
|
// path: `${storageConfig.uri}/${user.id}${req.body.path}/${item}`,
|
|
|
|
|
|
|
|
// isFolder: fs.lstatSync(`${storageConfig.uri}/${user.id}/${req.body.path}/${item}`).isDirectory()
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return res.json({
|
|
|
|
|
|
|
|
// code: 0,
|
|
|
|
|
|
|
|
// data: listFile
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
return null; |
|
|
|
/** resize image uploaded */ |
|
|
|
/** resize image uploaded */ |
|
|
|
} catch (ex) { |
|
|
|
} catch (ex) { |
|
|
@ -77,18 +58,19 @@ exports.get = (req, res, next) => { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
exports.create = (req, res, next) => { |
|
|
|
exports.create = (req, res, next) => { |
|
|
|
const user = req.user; |
|
|
|
const user = req.user; |
|
|
|
|
|
|
|
|
|
|
|
let dir = `${user.id}`; |
|
|
|
let dir = `${user.id}`; |
|
|
|
const name_folder = cloneDeep(req.body.name).trim(); |
|
|
|
console.log(dir); |
|
|
|
if (req.body.path) { |
|
|
|
if (req.body.path) { |
|
|
|
dir += req.body.path; |
|
|
|
dir += req.body.path; |
|
|
|
} |
|
|
|
} |
|
|
|
if (req.body.name) { |
|
|
|
if (req.body.name) { |
|
|
|
dir += `/${name_folder}`; |
|
|
|
dir += `/${req.body.name}`; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!fs.existsSync(dir)) { |
|
|
|
if (!fs.existsSync(dir)) { |
|
|
|
uploadAdapter.createFolder({ path: dir }); |
|
|
|
uploadAdapter.createFolder({ path: dir }); |
|
|
|
} |
|
|
|
} |
|
|
|
return res.json({ code: 0, message: 'success'}); |
|
|
|
return res.json({ code: 0, message: 'success' }); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -141,24 +123,16 @@ exports.download = (req, res, next) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const user = req.user; |
|
|
|
const user = req.user; |
|
|
|
const namefile = `${user.name}-${Date.now()}.zip`; |
|
|
|
const namefile = `${user.name}-${Date.now()}.zip`; |
|
|
|
const dir = `${storageConfig.uri}/download/${user.id}/${namefile}`; |
|
|
|
const dir = `${storageConfig.uri}/${user.id}/${namefile}`; |
|
|
|
const folder = `${storageConfig.uri}/download/${user.id}`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deleteFolderRecursive(folder); |
|
|
|
|
|
|
|
const output = fs.createWriteStream(dir); |
|
|
|
const output = fs.createWriteStream(dir); |
|
|
|
const archive = archiver('zip', { |
|
|
|
const archive = archiver('zip', { |
|
|
|
zlib: { level: 9 } // Sets the compression level.
|
|
|
|
zlib: { level: 9 } // Sets the compression level.
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
archive.pipe(output); |
|
|
|
archive.pipe(output); |
|
|
|
if (req.body.data) { |
|
|
|
if (req.body.data) { |
|
|
|
req.body.data.forEach((e) => { |
|
|
|
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).replace(/ /g, '%20'); |
|
|
|
const path = e.path.replace(cdnConfig.uri, storageConfig.uri); |
|
|
|
|
|
|
|
// const download = path.split("/");
|
|
|
|
|
|
|
|
// download.splice( download.indexOf(`${user.id}`), 0, "download");
|
|
|
|
|
|
|
|
// const final_path = download.join("/");
|
|
|
|
|
|
|
|
if (e.isFolder) { |
|
|
|
if (e.isFolder) { |
|
|
|
archive.directory(path, e.name); |
|
|
|
archive.directory(path, e.name); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -167,6 +141,7 @@ exports.download = (req, res, next) => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
archive.finalize(); |
|
|
|
archive.finalize(); |
|
|
|
|
|
|
|
|
|
|
|
return res.json({ |
|
|
|
return res.json({ |
|
|
|
code: 0, |
|
|
|
code: 0, |
|
|
|
data: { |
|
|
|
data: { |
|
|
@ -218,22 +193,4 @@ exports.deleteMultiple = (req, res, next) => { |
|
|
|
} catch (ex) { |
|
|
|
} catch (ex) { |
|
|
|
return ErrorHandel(ex, req, res, next); |
|
|
|
return ErrorHandel(ex, 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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|