Compare commits

...

3 Commits

Author SHA1 Message Date
Hai Q. Le 8888a9b1d3 change 1 year ago
Hai Q. Le c4edd5eca2 Merge tag 'HEAD' 1 year ago
Hai Q. Le 785a3afd83 Revert "change a" 1 year ago
  1. 51
      src/api/controllers/v1/path.controller.js
  2. 38
      src/common/models/file.model.js
  3. 2
      src/common/services/adapters/upload-adapter.js

@ -2,10 +2,8 @@
// import httpStatus from 'http-status'; // import httpStatus from 'http-status';
import fs from 'fs'; import fs from 'fs';
import archiver from 'archiver'; import archiver from 'archiver';
import { exec } from 'child_process';
import multer from 'multer'; import multer from 'multer';
import path from 'path'; import path from 'path';
import { cloneDeep } from 'lodash';
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 {
@ -14,6 +12,7 @@ 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';
function deleteFolderRecursive(folderPath) { function deleteFolderRecursive(folderPath) {
if (fs.existsSync(folderPath)) { if (fs.existsSync(folderPath)) {
@ -153,46 +152,35 @@ exports.delete = (req, res, next) => {
exports.download = async (req, res, next) => { exports.download = async (req, res, next) => {
try { try {
const user = req.user; const user = req.user;
const namefile = `${user.name.replace(/\s/g, '')}-${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: `${folder}` }); multer({ dest: `${folder}` });
await deleteFolderRecursive(folder); await deleteFolderRecursive(folder);
let command = ` zip -r ${dir} `; const output = fs.createWriteStream(dir);
const archive = archiver('zip', {
// const output = fs.createWriteStream(dir); zlib: { level: 9 } // Sets the compression level.
// const archive = archiver('zip', { });
// 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 path1 = e.path.replace(cdnConfig.uri, storageConfig.uri).replace(/ /g, '\\ '); const path = e.path.replace(cdnConfig.uri, storageConfig.uri).replace(/ /g, '\\ ');
// const path = e.path.replace(cdnConfig.uri, storageConfig.uri);
command += `${path1} `; 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 });
} }
console.log(command);
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}`);
archive.finalize();
return res.json({ return res.json({
code: 0, code: 0,
data: { data: {
@ -200,7 +188,6 @@ exports.download = async (req, res, next) => {
path: `${cdnConfig.uri}/download/${user.id}/${namefile}`, path: `${cdnConfig.uri}/download/${user.id}/${namefile}`,
} }
}); });
});
} catch (ex) { } catch (ex) {
return ErrorHandel(ex, req, res, next); return ErrorHandel(ex, req, res, next);
} }

@ -31,40 +31,20 @@ File.init(
autoIncrement: true, autoIncrement: true,
primaryKey: true primaryKey: true
}, },
url: {
type: DataTypes.STRING(255),
allowNull: false
},
name: { name: {
type: DataTypes.STRING(255), type: DataTypes.STRING(255),
defaultValue: null allowNull: false
}, },
title: { url: {
type: DataTypes.STRING(255), type: DataTypes.STRING(255),
defaultValue: null allowNull: false
},
payload: {
type: DataTypes.JSONB,
defaultValue: null // id | code | name
},
// manager
is_active: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}, },
created_by: { download_count: {
type: DataTypes.JSONB, type: DataTypes.NUMBER,
defaultValue: null // id | name allowNull: true,
defaultValue: 0
} }
}, },
{ {
timestamps: false, timestamps: false,
@ -209,7 +189,7 @@ File.get = async (id) => {
if (!data) { if (!data) {
throw new APIError({ throw new APIError({
status: httpStatus.NOT_FOUND, status: httpStatus.NOT_FOUND,
message: 'Không tìm thấy địa chỉ tỉnh/thành!' message: 'Không tìm thấy địa chỉ file!'
}); });
} }
return data; return data;

@ -79,7 +79,7 @@ const storage = multer.diskStorage({
// : file.originalname; // : file.originalname;
cb( cb(
null, null,
`/${path}/${file.originalname.replace(/\s/g, '')}` `/${path}/${file.originalname.replace(/[\s()]/g, '')}`
); );
} }
}); });

Loading…
Cancel
Save