change path

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

@ -3,7 +3,7 @@
import fs from 'fs'; import fs from 'fs';
import archiver from 'archiver'; import archiver from 'archiver';
import multer from 'multer'; import multer from 'multer';
import path from 'path'; import path from 'path';
import { handler as ErrorHandel } from '../../middlewares/errors'; import { handler as ErrorHandel } from '../../middlewares/errors';
// import ApiException from '../../../common/utils/APIException'; // import ApiException from '../../../common/utils/APIException';
import { import {
@ -45,7 +45,7 @@ exports.get = (req, res, next) => {
data: listFile data: listFile
}); });
}); });
// test local // test local
// 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) => {
@ -88,7 +88,7 @@ exports.create = (req, res, next) => {
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' });
}; };
/** /**
@ -137,14 +137,14 @@ exports.delete = (req, res, next) => {
return ErrorHandel(ex, req, res, next); return ErrorHandel(ex, req, res, next);
} }
}; };
exports.download = (req, res, next) => { exports.download = async (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}/download/${user.id}/${namefile}`;
const folder = `${storageConfig.uri}/download/${user.id}`; const folder = `${storageConfig.uri}/download/${user.id}`;
multer({ dest: `${dir}` });
deleteFolderRecursive(folder); await 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.
@ -171,7 +171,7 @@ exports.download = (req, res, next) => {
code: 0, code: 0,
data: { data: {
name: namefile, name: namefile,
path: `${cdnConfig.uri}/${user.id}/${namefile}`, path: `${cdnConfig.uri}/download/${user.id}/${namefile}`,
} }
}); });
} catch (ex) { } catch (ex) {
@ -224,16 +224,15 @@ exports.deleteMultiple = (req, res, next) => {
function deleteFolderRecursive(folderPath) { function deleteFolderRecursive(folderPath) {
let files = []; let files = [];
if (fs.existsSync(folderPath)) { if (fs.existsSync(folderPath)) {
files = fs.readdirSync(folderPath); files = fs.readdirSync(folderPath);
files.forEach((file) => { files.forEach((file) => {
const currentPath = path.join(folderPath, file); const currentPath = path.join(folderPath, file);
if (fs.lstatSync(currentPath).isDirectory()) { if (fs.lstatSync(currentPath).isDirectory()) {
deleteFolderRecursive(currentPath); deleteFolderRecursive(currentPath);
} else { } else {
fs.unlinkSync(currentPath); fs.unlinkSync(currentPath);
console.log('Deleted file:', currentPath); console.log('Deleted file:', currentPath);
} }
}); });
} }
} }

Loading…
Cancel
Save