From 7cfc4dba25ec4506eeabca463a8d9e1961787c6d Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 26 May 2023 15:15:04 +0700 Subject: [PATCH 1/2] change to 5gb --- src/common/services/adapters/upload-adapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/services/adapters/upload-adapter.js b/src/common/services/adapters/upload-adapter.js index f59892c..4a04c27 100644 --- a/src/common/services/adapters/upload-adapter.js +++ b/src/common/services/adapters/upload-adapter.js @@ -38,7 +38,7 @@ const createFolder = ({ path }) => { try { console.log(`${filePath}/${path}`); multer({ dest: `${filePath}/${path}` }); - + // multer({ dest: `${filePath}/images/games` }); // multer({ dest: `${filePath}/images/stories` }); @@ -101,7 +101,7 @@ const fileFilter = (req, file, cb) => { const uploader = multer({ storage, limits: { - fileSize: 1024 * 1024 * 2048 // 5MB + fileSize: 1024 * 1024 * 5048 // 5MB }, fileFilter }); From 7bd4b58a0df174b4a8c26c34b9d388fc8ab3c4fd Mon Sep 17 00:00:00 2001 From: "Hai Q. Le" Date: Mon, 31 Jul 2023 16:32:50 +0700 Subject: [PATCH 2/2] change a --- src/api/controllers/v1/path.controller.js | 61 ++++++++++++++--------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/api/controllers/v1/path.controller.js b/src/api/controllers/v1/path.controller.js index 1c13e3b..bc871a7 100644 --- a/src/api/controllers/v1/path.controller.js +++ b/src/api/controllers/v1/path.controller.js @@ -2,8 +2,10 @@ // import httpStatus from 'http-status'; import fs from 'fs'; import archiver from 'archiver'; +import { exec } from 'child_process'; import multer from 'multer'; import path from 'path'; +import { cloneDeep } from 'lodash'; import { handler as ErrorHandel } from '../../middlewares/errors'; // import ApiException from '../../../common/utils/APIException'; import { @@ -12,7 +14,6 @@ import { storage as storageConfig } from '../../../config/vars'; import uploadAdapter from '../../../common/services/adapters/upload-adapter'; -import { cloneDeep, forEach } from 'lodash'; function deleteFolderRecursive(folderPath) { if (fs.existsSync(folderPath)) { @@ -152,41 +153,53 @@ exports.delete = (req, res, next) => { exports.download = async (req, res, next) => { try { const user = req.user; - const namefile = `${user.name}-${Date.now()}.zip`; + const namefile = `${user.name.replace(/\s/g, '')}-${Date.now()}.zip`; const dir = `${storageConfig.uri}/download/${user.id}/${namefile}`; const folder = `${storageConfig.uri}/download/${user.id}`; multer({ dest: `${folder}` }); await deleteFolderRecursive(folder); - const output = fs.createWriteStream(dir); - const archive = archiver('zip', { - zlib: { level: 9 } // Sets the compression level. - }); + let command = ` zip -r ${dir} `; + + // const output = fs.createWriteStream(dir); + // const archive = archiver('zip', { + // zlib: { level: 9 } // Sets the compression level. + // }); - archive.pipe(output); + // archive.pipe(output); if (req.body.data) { req.body.data.forEach((e) => { - 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("/"); - if (e.isFolder) { - archive.directory(path, e.name); - } else { - archive.file(path, { name: e.name }); - } + const path1 = e.path.replace(cdnConfig.uri, storageConfig.uri).replace(/ /g, '\\ '); + + command += `${path1} `; }); } + console.log(command); + - archive.finalize(); - return res.json({ - code: 0, - data: { - name: namefile, - path: `${cdnConfig.uri}/download/${user.id}/${namefile}`, + exec(command, (error, stdout, stderr) => { + if (error) { + console.error(`Command execution error: ${error.message}`); + return res.status(400).json({ + code: 400, + message: 'error' + }); } + if (stderr) { + console.error(`Command stderr: ${stderr}`); + return res.status(400).json({ + code: 400, + message: 'error' + }); + } + console.log(`Command output: ${stdout}`); + return res.json({ + code: 0, + data: { + name: namefile, + path: `${cdnConfig.uri}/download/${user.id}/${namefile}`, + } + }); }); } catch (ex) { return ErrorHandel(ex, req, res, next);