From 8c0b294d219da1b8b2532b894f5241be55b79006 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 8 May 2023 15:33:01 +0700 Subject: [PATCH] change path controller --- src/api/controllers/v1/auth.controller.js | 4 +- src/api/controllers/v1/path.controller.js | 79 ++++++++++++----------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/api/controllers/v1/auth.controller.js b/src/api/controllers/v1/auth.controller.js index e8d29b9..b2c3601 100644 --- a/src/api/controllers/v1/auth.controller.js +++ b/src/api/controllers/v1/auth.controller.js @@ -1,6 +1,6 @@ -import messages from '../../../config/messages'; import { hash, compare } from 'bcryptjs'; +import messages from '../../../config/messages'; import { handler as ErrorHandler } from '../../middlewares/error'; import User from '../../../common/models/user.model'; @@ -62,4 +62,4 @@ exports.updatePassword = async (req, res, next) => { ErrorHandler(ex, req, res, next); }); } -}; \ No newline at end of file +}; diff --git a/src/api/controllers/v1/path.controller.js b/src/api/controllers/v1/path.controller.js index a4da57f..f7523b5 100644 --- a/src/api/controllers/v1/path.controller.js +++ b/src/api/controllers/v1/path.controller.js @@ -104,16 +104,16 @@ exports.delete = (req, res, next) => { multer({ dest: `${dir}` }); 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' }); - }); - // fs.rename(path, newpath, (err) => { - // if (err) throw err; + // 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' }); // }); + fs.rename(path, newpath, (err) => { + if (err) throw err; + return res.json({ code: 0, message: 'success' }); + }); return null; } catch (ex) { return ErrorHandel(ex, req, res, next); @@ -172,40 +172,41 @@ exports.forceDelete = (req, res, next) => { } }; +exports.deleteMultiple = (req, res, next) => { + try { + const user = req.user; + const dir = `${storageConfig.uri_backup}/${user.id}/${Date.now()}`; + multer({ dest: `${dir}` }); + if (req.body.data) { + req.body.data.forEach((e) => { + const path = e.path.replace(cdnConfig.uri, storageConfig.uri); + let newpath = e.path.replace(cdnConfig.uri, storageConfig.uri_backup); + const split = newpath.split('/'); + newpath = `${dir}/${split[split.length - 1]}`; + 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 { -// 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' }); +// 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); // } // }; - - -exports.deleteMultiple = (req, res, next) => { - try { - 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); - } -}; \ No newline at end of file