瀏覽代碼

fixes read files

unknown 2 年之前
父節點
當前提交
964138f3b0
共有 2 個文件被更改,包括 5 次插入3 次删除
  1. 4 2
      controllers/messages.js
  2. 1 1
      routes/messages.js

+ 4 - 2
controllers/messages.js

@@ -1,7 +1,6 @@
 const MessageModel = require('../model/message');
 const UserModel = require('../model/user');
 const ChatModel = require('../model/chat');
-const Jimp = require('jimp');
 const fs = require('fs').promises;
 const path = require('path');
 const createFolderIsExist = require('../helpers/create-directory');
@@ -183,7 +182,7 @@ const imageMessage = async (req, res, next) => {
 		const originalName = req.file.originalname;
 		const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await Jimp.read(pathToFile);
+		await fs.readFile(pathToFile);
 		await createFolderIsExist(path.join(DIR_IMAGES, userId));
 		await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
 		const imgUrl = path.normalize(path.join(userId, newNameImg));
@@ -260,6 +259,7 @@ const audioMessage = async (req, res, next) => {
 		const originalName = req.file.originalname;
 		const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
+		await fs.readFile(pathToFile);
 		await createFolderIsExist(path.join(DIR_AUDIOS, userId));
 		await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
 		const audioUrl = path.normalize(path.join(userId, newNameAudio));
@@ -336,6 +336,7 @@ const videoMessage = async (req, res, next) => {
 		const originalName = req.file.originalname;
 		const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
+		await fs.readFile(pathToFile);
 		await createFolderIsExist(path.join(DIR_VIDEOS, userId));
 		await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
 		const videoUrl = path.normalize(path.join(userId, newNameVideo));
@@ -425,6 +426,7 @@ const fileMessage = async (req, res, next) => {
 				break;
 		}
 		const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
+		await fs.readFile(pathToFile);
 		await createFolderIsExist(path.join(DIR_FILES, userId));
 		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
 		const fileUrl = path.normalize(path.join(userId, newNameFile));

+ 1 - 1
routes/messages.js

@@ -15,7 +15,7 @@ router
 	)
 	.post(
 		'/audio/:companionId',
-		[guard, upload.uploadAudio.single('audio')],
+		[guard, upload.uploadAudio.single('audio'), validation.validateUploadFile],
 		controllers.audioMessage
 	)
 	.post(