You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
upload-file-backend/src/api/controllers/v1/path.controller.js

211 lines
6.4 KiB

/* eslint-disable import/no-extraneous-dependencies */
2 years ago
// import httpStatus from 'http-status';
import fs from 'fs';
import archiver from 'archiver';
import multer from 'multer';
2 years ago
import { handler as ErrorHandel } from '../../middlewares/errors';
// import ApiException from '../../../common/utils/APIException';
import {
cdn as cdnConfig,
storage as storageConfig
} from '../../../config/vars';
import uploadAdapter from '../../../common/services/adapters/upload-adapter';
/**
* get file and folder
*
* @param {Formdata} file
*/
exports.get = (req, res, next) => {
try {
const user = req.user;
let path = `${storageConfig.uri}/${user.id}`;
// let path = storageConfig.uri;
if (req.body.path) {
path += req.body.path;
}
const listFile = [];
fs.readdir(path, (err, files) => {
if (files && files.length > 0) {
files.forEach((item) => {
listFile.push({
name: item,
path: `${cdnConfig.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;
/** resize image uploaded */
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
};
/**
* get file and folder
*
* @param {Formdata} file
*/
exports.create = (req, res, next) => {
const user = req.user;
let dir = `${user.id}`;
console.log(dir);
if (req.body.path) {
dir += req.body.path;
}
if (req.body.name) {
dir += `/${req.body.name}`;
}
if (!fs.existsSync(dir)) {
uploadAdapter.createFolder({ path: dir });
}
return res.json({ code: 0, message: 'success' });
};
/**
* get file and folder
*
* @param {Formdata} file
*/
exports.update = (req, res, next) => {
try {
console.log(cdnConfig.uri, storageConfig.uri);
const oldPath = req.body.oldPath.replace(cdnConfig.uri, storageConfig.uri);
const newPath = req.body.newPath.replace(cdnConfig.uri, storageConfig.uri);
2 years ago
fs.rename(oldPath, newPath, (err) => {
if (err) {
console.log(err);
return res.status(400).json({ code: 400, message: 'lỗi' });
}
return res.json({ code: 0, message: 'success' });
});
return null;
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
};
exports.delete = (req, res, next) => {
try {
const user = req.user;
const dir = `${storageConfig.uri_backup}/${user.id}`;
multer({ dest: `${dir}` });
2 years ago
const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri);
const newpath = req.body.path.replace(cdnConfig.uri, storageConfig.uri_backup);
2 years ago
fs.rm(path, { recursive: true }, err => {
if (err) {
return res.status(400).json({ code: 400, message: 'lỗi', detail: err });
}
2 years ago
return res.json({ code: 0, message: 'success' });
});
2 years ago
// fs.rename(path, newpath, (err) => {
// if (err) throw err;
// return res.json({ code: 0, message: 'success' });
// });
2 years ago
return null;
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
};
exports.download = (req, res, next) => {
try {
const user = req.user;
const namefile = `${user.name}-${Date.now()}.zip`;
const dir = `${storageConfig.uri}/${user.id}/${namefile}`;
const output = fs.createWriteStream(dir);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
archive.pipe(output);
if (req.body.data) {
req.body.data.forEach((e) => {
const path = e.path.replace(cdnConfig.uri, storageConfig.uri);
if (e.isFolder) {
archive.directory(path, e.name);
} else {
archive.file(path, { name: e.name });
}
});
}
archive.finalize();
return res.json({
code: 0,
data: {
name: namefile,
path: `${cdnConfig.uri}/${user.id}/${namefile}`,
}
});
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
};
exports.forceDelete = (req, res, next) => {
try {
const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri);
// const newpath = req.body.path.replace(cdnConfig.uri, storageConfig.uri_backup);
fs.rm(path, { recursive: true }, err => {
if (err) {
return res.status(400).json({ code: 400, message: 'lỗi', detail: err });
}
return res.json({ code: 0, message: 'success' });
});
return null;
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
};
2 years ago
// exports.deleteMultiple = (req, res, next) => {
// try {
// const user = req.user;
// const dir = `${storageConfig.uri_backup}/${user.id}`;
// multer({ dest: `${dir}` });
// if (req.body.data) {
// req.body.data.forEach((e) => {
// const path = e.path.replace(cdnConfig.uri, storageConfig.uri);
// const newpath = e.path.replace(cdnConfig.uri, storageConfig.uri_backup);
// console.log(path, newpath);
// fs.rename(path, newpath, (err) => {
// if (err) throw err;
// return { code: 0, message: 'success' };
// });
// });
// }
// return res.json({ code: 0, message: 'success' });
// } catch (ex) {
// return ErrorHandel(ex, req, res, next);
// }
// };
exports.deleteMultiple = (req, res, next) => {
try {
2 years ago
const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri);
fs.rm(path, { recursive: true }, err => {
if (err) {
return res.status(400).json({ code: 400, message: 'lỗi', detail: err });
}
return res.json({ code: 0, message: 'success' });
});
return null;
} catch (ex) {
return ErrorHandel(ex, req, res, next);
}
2 years ago
};