feat: menambahkan manajemen akun mentor oleh admin
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
const cleanupUploadedFile = (file) => {
|
||||
if (file?.path && fs.existsSync(file.path)) {
|
||||
fs.unlinkSync(file.path);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
cleanupUploadedFile,
|
||||
};
|
||||
@@ -0,0 +1,81 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const multer = require('multer');
|
||||
const { response, error } = require('../../../helpers/utils/wrapper');
|
||||
const { BadRequestError } = require('../../../helpers/error');
|
||||
const { ERROR } = require('../../../helpers/http-status/status_code');
|
||||
|
||||
const mentorProfileUploadDir = path.join(
|
||||
process.cwd(),
|
||||
'src',
|
||||
'uploads',
|
||||
'mentor-profiles'
|
||||
);
|
||||
|
||||
if (!fs.existsSync(mentorProfileUploadDir)) {
|
||||
fs.mkdirSync(mentorProfileUploadDir, { recursive: true });
|
||||
}
|
||||
|
||||
const allowedMimeTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, mentorProfileUploadDir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const safeOriginalName = file.originalname
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^a-zA-Z0-9.\-_]/g, '');
|
||||
|
||||
cb(null, `${Date.now()}-${Math.round(Math.random() * 1e9)}-${safeOriginalName}`);
|
||||
},
|
||||
});
|
||||
|
||||
const fileFilter = (req, file, cb) => {
|
||||
if (!allowedMimeTypes.includes(file.mimetype)) {
|
||||
return cb(
|
||||
new BadRequestError('Profile picture must be a JPEG, JPG, PNG, or WEBP image'),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
cb(null, true);
|
||||
};
|
||||
|
||||
const mentorProfileUpload = multer({
|
||||
storage,
|
||||
fileFilter,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024,
|
||||
},
|
||||
});
|
||||
|
||||
const mentorProfileUploadErrorHandler = (err, req, res, next) => {
|
||||
if (!err) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (err instanceof multer.MulterError) {
|
||||
return response(
|
||||
res,
|
||||
'fail',
|
||||
error(new BadRequestError(err.message)),
|
||||
err.message,
|
||||
ERROR.BAD_REQUEST
|
||||
);
|
||||
}
|
||||
|
||||
return response(
|
||||
res,
|
||||
'fail',
|
||||
error(err),
|
||||
err.message || 'Invalid profile picture upload',
|
||||
ERROR.BAD_REQUEST
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
mentorProfileUpload,
|
||||
mentorProfileUploadErrorHandler,
|
||||
mentorProfileUploadDir,
|
||||
};
|
||||
Reference in New Issue
Block a user