|
@@ -1,6 +1,7 @@
|
|
const MessageModel = require('../model/message');
|
|
const MessageModel = require('../model/message');
|
|
const UserModel = require('../model/user');
|
|
const UserModel = require('../model/user');
|
|
const ChatModel = require('../model/chat');
|
|
const ChatModel = require('../model/chat');
|
|
|
|
+const Jimp = require('jimp');
|
|
const fs = require('fs').promises;
|
|
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
const path = require('path');
|
|
const createFolderIsExist = require('../helpers/create-directory');
|
|
const createFolderIsExist = require('../helpers/create-directory');
|
|
@@ -182,7 +183,7 @@ const imageMessage = async (req, res, next) => {
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await fs.readFile(pathToFile);
|
|
|
|
|
|
+ await Jimp.read(pathToFile);
|
|
await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
|
|
await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
|
|
const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
@@ -255,13 +256,16 @@ const audioMessage = async (req, res, next) => {
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_AUDIOS = process.env.DIR_AUDIOS;
|
|
const DIR_AUDIOS = process.env.DIR_AUDIOS;
|
|
|
|
+ const DIR_UPLOAD = process.env.DIR_UPLOAD;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await fs.readFile(pathToFile);
|
|
|
|
await createFolderIsExist(path.join(DIR_AUDIOS, userId));
|
|
await createFolderIsExist(path.join(DIR_AUDIOS, userId));
|
|
- await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
|
|
|
|
|
|
+ await fs.rename(
|
|
|
|
+ path.join(DIR_UPLOAD, originalName),
|
|
|
|
+ path.join(DIR_AUDIOS, userId, newNameAudio)
|
|
|
|
+ );
|
|
const audioUrl = path.normalize(path.join(userId, newNameAudio));
|
|
const audioUrl = path.normalize(path.join(userId, newNameAudio));
|
|
if (isChat && isCompanionChat && audioUrl) {
|
|
if (isChat && isCompanionChat && audioUrl) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
@@ -336,7 +340,6 @@ const videoMessage = async (req, res, next) => {
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await fs.readFile(pathToFile);
|
|
|
|
await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
|
|
await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
|
|
const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
@@ -426,7 +429,6 @@ const fileMessage = async (req, res, next) => {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
- await fs.readFile(pathToFile);
|
|
|
|
await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
|
|
await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|