change path

master
Lucas 2 years ago
parent 2994342be0
commit 17a3eb84db
  1. 41
      src/api/controllers/v1/path.controller.js

@ -3,7 +3,7 @@
import fs from 'fs';
import archiver from 'archiver';
import multer from 'multer';
import path from 'path';
import path from 'path';
import { handler as ErrorHandel } from '../../middlewares/errors';
// import ApiException from '../../../common/utils/APIException';
import {
@ -45,7 +45,7 @@ exports.get = (req, res, next) => {
data: listFile
});
});
// test local
// test local
// fs.readdir(path, (err, files) => {
// if (files && files.length > 0) {
// files.forEach((item) => {
@ -61,7 +61,7 @@ exports.get = (req, res, next) => {
// data: listFile
// });
// });
return null;
/** resize image uploaded */
@ -88,7 +88,7 @@ exports.create = (req, res, next) => {
if (!fs.existsSync(dir)) {
uploadAdapter.createFolder({ path: dir });
}
return res.json({ code: 0, message: 'success'});
return res.json({ code: 0, message: 'success' });
};
/**
@ -137,14 +137,14 @@ exports.delete = (req, res, next) => {
return ErrorHandel(ex, req, res, next);
}
};
exports.download = (req, res, next) => {
exports.download = async (req, res, next) => {
try {
const user = req.user;
const namefile = `${user.name}-${Date.now()}.zip`;
const dir = `${storageConfig.uri}/download/${user.id}/${namefile}`;
const folder = `${storageConfig.uri}/download/${user.id}`;
deleteFolderRecursive(folder);
multer({ dest: `${dir}` });
await deleteFolderRecursive(folder);
const output = fs.createWriteStream(dir);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
@ -157,7 +157,7 @@ exports.download = (req, res, next) => {
// 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");
// download.splice( download.indexOf(`${user.id}`), 0, "download");
// const final_path = download.join("/");
if (e.isFolder) {
archive.directory(path, e.name);
@ -171,7 +171,7 @@ exports.download = (req, res, next) => {
code: 0,
data: {
name: namefile,
path: `${cdnConfig.uri}/${user.id}/${namefile}`,
path: `${cdnConfig.uri}/download/${user.id}/${namefile}`,
}
});
} catch (ex) {
@ -224,16 +224,15 @@ 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);
}
});
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);
}
});
}
}
}

Loading…
Cancel
Save