diff --git a/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js b/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js index d55aee5..1b44699 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js @@ -32,15 +32,15 @@ const StudentController = { // Check input if ( - !name || - !mssv || - !phone_number || - !dob || - !class_room || - !email || - !gender || - !role || - !HELPER.validateBigInt(admin_id) + !name + || !mssv + || !phone_number + || !dob + || !class_room + || !email + || !gender + || !role + || !HELPER.validateBigInt(admin_id) ) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, diff --git a/server-media-service/src/media-service/v1/controllers/upload.controller.js b/server-media-service/src/media-service/v1/controllers/upload.controller.js index a2875dc..c4fdf7d 100644 --- a/server-media-service/src/media-service/v1/controllers/upload.controller.js +++ b/server-media-service/src/media-service/v1/controllers/upload.controller.js @@ -2,7 +2,10 @@ const { returnReasons } = require('../../../share/middlewares/handle_error'); const { handleUpload } = require('../../../share/services/upload.service'); const { handleRemoveTmp } = require('../../../share/services/remove_tmp.service'); -const { handleResizeImage, handleValideResizeImage } = require('../../../share/services/resize_img_service'); +const { + handleResizeImage, + handleValideResizeImage, +} = require('../../../share/services/resize_img_service'); const CONSTANTS = require('../../../share/configs/constants'); const MESSAGES = require('../../../share/configs/message'); const STORAGE = require('../../../share/utils/storage'); @@ -47,74 +50,41 @@ const uploadController = { // Get the uploaded file const file_upload = file.file; // Get the temporary file path - let path_image = file_upload.tempFilePath; + const path_image = file_upload.tempFilePath; // Get the file mimetype const mime_image = file_upload.mimetype; // Get the file name const name_image = file_upload.name; + // Size image + const size_image = file_upload.size; - // try { - // Resize the image to 1024x764 if it's an image - const type_image = mime_image.startsWith(CONSTANTS.MIME_IMAGE); - if (type_image) { - const path_image_new = await handleResizeImage(path_image); - path_image = path_image_new; - } - - // Check the resized image size, if it's larger than 1024*1024, return an error - const resized_image_size = await handleValideResizeImage(path_image); - if (resized_image_size > CONSTANTS.SIZE_IMAGE) { - handleRemoveTmp(path_image); - return res.status(CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE).json({ - status: CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE, - message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE), - element: { - result: MESSAGES.MEDIA.NO_SIZE_IMAGE_BIG, - }, - }); - } + try { + if (size_image > CONSTANTS.SIZE_IMAGE) { + handleRemoveTmp(path_image); + return res.status(CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE).json({ + status: CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_PAYLOAD_TOO_LARGE), + element: { + result: MESSAGES.MEDIA.NO_SIZE_IMAGE_BIG, + }, + }); + } - // Change type media - const check_type = STORAGE.detectedFileType(mime_image); - const name_image_new = STORAGE.removeFileExtension(name_image); - const media_id = HELPER.createID(check_type); - const date = new Date().getTime(); + // Change type media + const check_type = STORAGE.detectedFileType(mime_image); + const name_image_new = STORAGE.removeFileExtension(name_image); + const media_id = HELPER.createID(check_type); + const date = new Date().getTime(); - // Check is type - const is_type = (type) => type !== CONSTANTS.MIME_IMAGE - || type !== CONSTANTS.MIME_VIDEO - || type !== CONSTANTS.MIME_AUDIO - || type !== CONSTANTS.MIME_DOCUMENT; + // Check is type + const is_type = (type) => + type !== CONSTANTS.MIME_IMAGE || + type !== CONSTANTS.MIME_VIDEO || + type !== CONSTANTS.MIME_AUDIO || + type !== CONSTANTS.MIME_DOCUMENT; - if (!is_type(check_type)) { - handleRemoveTmp(path_image); - return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ - status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, - message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), - element: { - result: MESSAGES.MEDIA.NO_TYPE_INVALID, - }, - }); - } - // Take tail file - let template_upload; - let cloud_bucket; - switch (check_type) { - // Check the media type and set the corresponding template_upload and cloud_bucket - case CONSTANTS.MIME_IMAGE: - template_upload = CONSTANTS.STORAGE_FOLDER_IMAGES_TEMPLATE; - cloud_bucket = CONSTANTS.MIME_IMAGE; - break; - case CONSTANTS.MIME_VIDEO: - template_upload = CONSTANTS.STORAGE_FOLDER_VIDEOS_TEMPLATE; - cloud_bucket = CONSTANTS.MIME_VIDEO; - break; - case CONSTANTS.MIME_DOCUMENT: - template_upload = CONSTANTS.STORAGE_FOLDER_DOCUMENT_TEMPLATE; - cloud_bucket = CONSTANTS.MIME_DOCUMENT; - break; - // In case of an invalid media type, return a 400 error - default: + if (!is_type(check_type)) { + handleRemoveTmp(path_image); return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), @@ -122,43 +92,71 @@ const uploadController = { result: MESSAGES.MEDIA.NO_TYPE_INVALID, }, }); - } + } + // Take tail file + let template_upload; + let cloud_bucket; + switch (check_type) { + // Check the media type and set the corresponding template_upload and cloud_bucket + case CONSTANTS.MIME_IMAGE: + template_upload = CONSTANTS.STORAGE_FOLDER_IMAGES_TEMPLATE; + cloud_bucket = CONSTANTS.MIME_IMAGE; + break; + case CONSTANTS.MIME_VIDEO: + template_upload = CONSTANTS.STORAGE_FOLDER_VIDEOS_TEMPLATE; + cloud_bucket = CONSTANTS.MIME_VIDEO; + break; + case CONSTANTS.MIME_DOCUMENT: + template_upload = CONSTANTS.STORAGE_FOLDER_DOCUMENT_TEMPLATE; + cloud_bucket = CONSTANTS.MIME_DOCUMENT; + break; + // In case of an invalid media type, return a 400 error + default: + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.MEDIA.NO_TYPE_INVALID, + }, + }); + } - // Link file - const uri_key = STORAGE.getURIFromTemplate(template_upload, { - user_id: auth_general, - media_id, - time: date, - file_name: name_image_new, - }); + // Link file + const uri_key = STORAGE.getURIFromTemplate(template_upload, { + user_id: auth_general, + media_id, + time: date, + file_name: name_image_new, + }); - // Upload cloud - handleUpload(path_image, uri_key, cloud_bucket) - .then((result) => res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({ - status: CONSTANTS.HTTP.STATUS_2XX_OK, - message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK), - element: { - result: { - public_id: result.public_id, - url: result.secure_url, - }, - }, - }), - ) - .catch((error) => { - console.error(error); - return res.status(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR).json({ - status: CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR, - message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR), + // Upload cloud + handleUpload(path_image, uri_key, cloud_bucket) + .then((result) => + res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({ + status: CONSTANTS.HTTP.STATUS_2XX_OK, + message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK), + element: { + result: { + public_id: result.public_id, + url: result.secure_url, + }, + }, + }), + ) + .catch((error) => { + console.error(error); + return res.status(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR).json({ + status: CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR, + message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR), + }); }); + } catch (error) { + console.error(error); + return res.status(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE).json({ + status: CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE, + message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE), }); - // } catch (error) { - // console.error(error); - // return res.status(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE).json({ - // status: CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE, - // message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE), - // }); - // } + } }, }; module.exports.uploadController = uploadController;