unknown %!s(int64=2) %!d(string=hai) anos
pai
achega
e572ea8900
Modificáronse 2 ficheiros con 19 adicións e 15 borrados
  1. 18 13
      controllers/messages.js
  2. 1 2
      controllers/user.js

+ 18 - 13
controllers/messages.js

@@ -5,6 +5,11 @@ const Jimp = require('jimp');
 const fs = require('fs').promises;
 const path = require('path');
 const createFolderIsExist = require('../helpers/create-directory');
+const DIR_UPLOAD = process.env.DIR_UPLOAD;
+const DIR_IMAGES = process.env.DIR_IMAGES;
+const DIR_AUDIOS = process.env.DIR_AUDIOS;
+const DIR_VIDEOS = process.env.DIR_VIDEOS;
+const DIR_FILES = process.env.DIR_FILES;
 require('dotenv').config();
 
 const listMessages = async (req, res, next) => {
@@ -25,10 +30,6 @@ const removeMessage = async (req, res, next) => {
 	try {
 		const id = req.params.id;
 		const userId = req.user.id;
-		const DIR_IMAGES = process.env.DIR_IMAGES;
-		const DIR_AUDIOS = process.env.DIR_AUDIOS;
-		const DIR_VIDEOS = process.env.DIR_VIDEOS;
-		const DIR_FILES = process.env.DIR_FILES;
 		const userMessage = await MessageModel.remove(id, userId);
 		await MessageModel.removeByFields(
 			userId,
@@ -178,14 +179,14 @@ const imageMessage = async (req, res, next) => {
 		const idTime = Math.round(Date.now() / 1000);
 		const isChat = await ChatModel.getByField(id, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, id);
-		const DIR_IMAGES = process.env.DIR_IMAGES;
-		const pathToFile = req.file.path;
 		const originalName = req.file.originalname;
 		const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await Jimp.read(pathToFile);
 		await createFolderIsExist(path.join(DIR_IMAGES, userId));
-		await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
+		await fs.rename(
+			path.join(DIR_UPLOAD, originalName),
+			path.join(DIR_IMAGES, userId, newNameImg)
+		);
 		const imgUrl = path.normalize(path.join(userId, newNameImg));
 		if (isChat && isCompanionChat && imgUrl) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
@@ -257,7 +258,6 @@ const audioMessage = async (req, res, next) => {
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const DIR_AUDIOS = process.env.DIR_AUDIOS;
 		const DIR_UPLOAD = process.env.DIR_UPLOAD;
-		const pathToFile = req.file.path;
 		const originalName = req.file.originalname;
 		const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
@@ -336,12 +336,14 @@ const videoMessage = async (req, res, next) => {
 		const isChat = await ChatModel.getByField(id, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const DIR_VIDEOS = process.env.DIR_VIDEOS;
-		const pathToFile = req.file.path;
 		const originalName = req.file.originalname;
 		const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
 		await createFolderIsExist(path.join(DIR_VIDEOS, userId));
-		await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
+		await fs.rename(
+			path.join(DIR_UPLOAD, originalName),
+			path.join(DIR_VIDEOS, userId, newNameVideo)
+		);
 		const videoUrl = path.normalize(path.join(userId, newNameVideo));
 		if (isChat && isCompanionChat && videoUrl) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
@@ -412,7 +414,7 @@ const fileMessage = async (req, res, next) => {
 		const isChat = await ChatModel.getByField(id, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const DIR_FILES = process.env.DIR_FILES;
-		const pathToFile = req.file.path;
+		const originalName = req.file.originalname;
 		const fullType = req.file.mimetype;
 		let type;
 		switch (fullType) {
@@ -430,7 +432,10 @@ const fileMessage = async (req, res, next) => {
 		}
 		const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
 		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
+		await fs.rename(
+			path.join(DIR_UPLOAD, originalName),
+			path.join(DIR_FILES, userId, newNameFile)
+		);
 		const fileUrl = path.normalize(path.join(userId, newNameFile));
 		if (isChat && isCompanionChat && fileUrl) {
 			const { name, lastName, avatarUrl, color, number } = req.user;

+ 1 - 2
controllers/user.js

@@ -11,6 +11,7 @@ require('dotenv').config();
 const client = require('../helpers/twilio');
 const phoneToken = require('generate-sms-verification-code');
 const SECRET_KEY = process.env.JWT_SECRET;
+const DIR_IMAGES = process.env.DIR_IMAGES;
 
 const createNewUser = async (req, res, next) => {
 	try {
@@ -176,7 +177,6 @@ const removeAvatar = async (req, res, next) => {
 		const filteredAvatars = [...avatarsArr].filter(
 			({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
 		);
-		const DIR_IMAGES = process.env.DIR_IMAGES;
 		await fs.unlink(path.join(DIR_IMAGES, toDelete.avatarUrl));
 		const avatarUrl = filteredAvatars[0].avatarUrl;
 		await UserModel.updateUser(id, { avatarUrl, avatarsArr: filteredAvatars });
@@ -202,7 +202,6 @@ const updateAvatar = async (req, res, next) => {
 		const userId = req.user.id;
 		const userNumber = req.user.number;
 		const token = req.user.token;
-		const DIR_IMAGES = process.env.DIR_IMAGES;
 		const pathToFile = req.file.path;
 		const originalName = req.file.originalname;
 		const newNameAvatar = `${Math.round(Date.now() / 1000)}${originalName}`;