From 0e2b8ef205fea51ed6fd75b631c16c9d71916d2a Mon Sep 17 00:00:00 2001 From: pronazmul Date: Wed, 9 Jun 2021 22:51:03 +0600 Subject: [PATCH] File Successfully Unlinked From Storage --- middlewares/users/avatarUpload.js | 2 +- middlewares/users/userValidator.js | 2 +- utilities/singleUploader.js | 2 ++ views/partials/add_user_modal.ejs | 27 ++++++++++++++++++--------- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/middlewares/users/avatarUpload.js b/middlewares/users/avatarUpload.js index cfbe6d9..03c48c5 100644 --- a/middlewares/users/avatarUpload.js +++ b/middlewares/users/avatarUpload.js @@ -11,7 +11,7 @@ const avatarUpload = (req, res, next) => { ); // Call The Multer Upload MiddleWare Function To Handle Error: - upload.any()((req, res, error) => { + upload.any()(req, res, (error) => { if (error) { res.status(500).json({ errors: { diff --git a/middlewares/users/userValidator.js b/middlewares/users/userValidator.js index 1006a28..46e9095 100644 --- a/middlewares/users/userValidator.js +++ b/middlewares/users/userValidator.js @@ -63,7 +63,7 @@ const addUserValidationHandler = (req, res, next) => { if (req.files.length > 0) { const { filename } = req.files[0]; unlink( - path.join(`${__dirname}/../public/uploads/avatars/${filename}`), + path.join(__dirname, `/../../public/uploads/avatars/${filename}`), (err) => { if (err) console.log(err); } diff --git a/utilities/singleUploader.js b/utilities/singleUploader.js index cf99e60..f103463 100644 --- a/utilities/singleUploader.js +++ b/utilities/singleUploader.js @@ -18,6 +18,7 @@ const singleUploader = ( destination: (req, file, cb) => { cb(null, UPLOADS_FOLDER); }, + filename: (req, file, cb) => { const fileExt = path.extname(file.originalname); const fileName = @@ -28,6 +29,7 @@ const singleUploader = ( .join("-") + "-" + Date.now(); + cb(null, fileName + fileExt); }, }); diff --git a/views/partials/add_user_modal.ejs b/views/partials/add_user_modal.ejs index 3c54b6d..20eb52c 100644 --- a/views/partials/add_user_modal.ejs +++ b/views/partials/add_user_modal.ejs @@ -10,21 +10,26 @@ method="post" action="/users" enctype="multipart/form-data" - id="add-user-form"> + id="add-user-form" + > -

This is error

+

+ -

This is error

+

+ -

This is error

+

+ -

This is error

+

+ -

This is error

+

This is error

@@ -50,7 +55,7 @@ form.onsubmit = async function(event){ event.preventDefault() - // Clear Field Errors First: + // Clear Errors First: const errorPlaceHolders = document.querySelectorAll("p.error"); for(let i=0; i