Skip to content

Commit

Permalink
#258 fix bug media
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 23, 2023
1 parent 00449e6 commit c474923
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
27 changes: 18 additions & 9 deletions backend-manager-student/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!@ Author: Nguyễn Tiến Tài.
#! Description: Docker Compose Production
#!@ Created_At: 20-12-2022.
#!@ Updated_At: 12-04-2023
#! Description: Docker Compose Dev
#!@ Created_At : 20-12-2022.
#!@ Update_At: 21-12-2022, 22-12-2022, 16-01-2023, 12-03-2023, 18-03-2023.
#!@ Updated_At: 12-04-2023

version: '3.7'

Expand All @@ -12,6 +12,7 @@ services:
container_name: server_admin_api
depends_on:
- redis-master
- redis-slave
- postgresql
restart: always
build:
Expand All @@ -20,7 +21,6 @@ services:
environment:
NODE_ENV: PRODUCTION
volumes:
# - ./:/app
- './src:/usr/src/app/src'
env_file:
- .env
Expand All @@ -33,6 +33,7 @@ services:
container_name: server_user_api
depends_on:
- redis-master
- redis-slave
- postgresql
restart: always
build:
Expand All @@ -56,6 +57,7 @@ services:
context: .
depends_on:
- redis-master
- redis-slave
- postgresql
restart: always
environment:
Expand All @@ -71,24 +73,28 @@ services:
###! Redis Master ###
redis-master:
container_name: redis-master
image: redis
build:
dockerfile: dockerfile.redis
context: .
image: redis
restart: always
environment:
REDIS_PASSWORD: '${REDIS_PASSWORD}'
NODE_ENV: PRODUCTION
TZ: '${TIME_DATE_TZ}'
command: sh -c "redis-server /usr/local/etc/redis/redis.conf --requirepass ${REDIS_PASSWORD} --maxmemory ${MAXMEMORY} & cd /usr/local/bin/scrip && ./scrip_redis.sh && logrotate /usr/local/bin/logs/logrotate.conf"
env_file:
- .env
volumes:
- redis-master:/data
- /setup:/usr/local/etc/redis/redis.conf
- ./setup:/usr/local/etc/redis/redis.conf
- ./scrip:/usr/local/bin/scrip
- ./scrip:/usr/local/bin/logs
ports:
- ${REDIS_PORT}:${REDIS_PORT}
networks:
- libary_school
- server-send-email-student_sendemail
- server-media-service_upload-network
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 30s
Expand All @@ -98,12 +104,13 @@ services:
###! Redis Slave from Master ###
redis-slave:
container_name: redis-slave
restart: always
depends_on:
- redis-master
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD_SLAVE}
NODE_ENV: PRODUCTION
restart: always
image: redis

volumes:
- redis-slave:/data
ports:
Expand Down Expand Up @@ -156,6 +163,7 @@ services:
- .env
networks:
- libary_school
- server-media-service_upload-network
healthcheck:
test: ['CMD-SHELL', "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"]
interval: 10s
Expand Down Expand Up @@ -200,6 +208,7 @@ services:
- postgresql
- mongodb
- redis-master
- redis-slave
- admin_api
- user_api
- cron_job
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { handleUpload } = require('../../../share/services/upload.service');
const { handleRemoveTmp } = require('../../../share/services/remove_tmp.service');
const {
handleResizeImage,
handleValideResizeImage,
handleValidResizeImage,
} = require('../../../share/services/resize_img_service');
const CONSTANTS = require('../../../share/configs/constants');
const MESSAGES = require('../../../share/configs/message');
Expand All @@ -15,7 +15,7 @@ const uploadController = {
/**
* @author Nguyễn Tiến Tài
* @created_at 29/12/2022
* @update_at 12/01/2023, 15/03/2023, 23/03/2023
* @update_at 12/01/2023, 15/03/2023, 23/03/2023, 23/04/2023
* @description Upload image storage cloud
* @function uploadCloudinary
* @param { files }
Expand Down Expand Up @@ -50,24 +50,33 @@ const uploadController = {
// Get the uploaded file
const file_upload = file.file;
// Get the temporary file path
const path_image = file_upload.tempFilePath;
let 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 {
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,
},
});
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 handleValidResizeImage(path_image);
if (
resized_image_size > CONSTANTS.SIZE_IMAGE ||
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
Expand Down Expand Up @@ -144,14 +153,14 @@ const uploadController = {
}),
)
.catch((error) => {
console.error(error);
console.error(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);
console.error(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),
Expand Down
6 changes: 2 additions & 4 deletions server-media-service/src/share/services/resize_img_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ module.exports = {
const path_image_convert = STORAGE.getURIFromTemplate(CONSTANTS.PATH_RESIZE, {
path_image,
});
await sharp(path_image)
.resize(1024, 764)
.toFile(path_image_convert);
await sharp(path_image).resize(1024, 764).toFile(path_image_convert);
// Update the temporary file path to the resized image
return path_image_convert;
},
handleValideResizeImage: async (path_image) => await sharp(path_image).metadata().size,
handleValidResizeImage: async (path_image) => await sharp(path_image).metadata().size,
};
8 changes: 6 additions & 2 deletions server-media-service/src/share/services/upload.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ module.exports = {
{ resource_type: CONSTANTS.OPTION_CLOUD, folder: CONSTANTS.OPTION_FOLDER_DOCUMENT, public_id: `${name_image}`, format: CONSTANTS.OPTION_CLOUD_DOCUMENT_FORMAT }
: { resource_type: CONSTANTS.OPTION_CLOUD, folder: CONSTANTS.OPTION_FOLDER_IMAGE, public_id: `${name_image}` },
async (err, result) => {
if (err) throw (data = err);
if (err) {
data = err;
} else {
data = result
}
handleRemoveTmp(path_image);
return (data = result);
return data
},
);
return data;
Expand Down
2 changes: 0 additions & 2 deletions server-send-email-student/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ services:
container_name: send_email_student
external_links:
- backend-manager-student_libary_school
depends_on:
- redis-master
image: send_email_student
restart: always
build:
Expand Down

0 comments on commit c474923

Please sign in to comment.