|
@@ -5,6 +5,11 @@ 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');
|
|
|
|
+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();
|
|
require('dotenv').config();
|
|
|
|
|
|
const listMessages = async (req, res, next) => {
|
|
const listMessages = async (req, res, next) => {
|
|
@@ -25,10 +30,6 @@ const removeMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const id = req.params.id;
|
|
const id = req.params.id;
|
|
const userId = req.user.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);
|
|
const userMessage = await MessageModel.remove(id, userId);
|
|
await MessageModel.removeByFields(
|
|
await MessageModel.removeByFields(
|
|
userId,
|
|
userId,
|
|
@@ -178,14 +179,14 @@ const imageMessage = async (req, res, next) => {
|
|
const idTime = Math.round(Date.now() / 1000);
|
|
const idTime = Math.round(Date.now() / 1000);
|
|
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_IMAGES = process.env.DIR_IMAGES;
|
|
|
|
- const pathToFile = req.file.path;
|
|
|
|
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 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(
|
|
|
|
+ path.join(DIR_UPLOAD, originalName),
|
|
|
|
+ path.join(DIR_IMAGES, userId, newNameImg)
|
|
|
|
+ );
|
|
const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
if (isChat && isCompanionChat && imgUrl) {
|
|
if (isChat && isCompanionChat && imgUrl) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
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 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 DIR_UPLOAD = process.env.DIR_UPLOAD;
|
|
- 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;
|
|
@@ -336,12 +336,14 @@ const videoMessage = 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_VIDEOS = process.env.DIR_VIDEOS;
|
|
const DIR_VIDEOS = process.env.DIR_VIDEOS;
|
|
- const pathToFile = req.file.path;
|
|
|
|
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 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(
|
|
|
|
+ path.join(DIR_UPLOAD, originalName),
|
|
|
|
+ path.join(DIR_VIDEOS, userId, newNameVideo)
|
|
|
|
+ );
|
|
const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
if (isChat && isCompanionChat && videoUrl) {
|
|
if (isChat && isCompanionChat && videoUrl) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
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 isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_FILES = process.env.DIR_FILES;
|
|
const DIR_FILES = process.env.DIR_FILES;
|
|
- const pathToFile = req.file.path;
|
|
|
|
|
|
+ const originalName = req.file.originalname;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
let type;
|
|
let type;
|
|
switch (fullType) {
|
|
switch (fullType) {
|
|
@@ -430,7 +432,10 @@ const fileMessage = async (req, res, next) => {
|
|
}
|
|
}
|
|
const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
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(
|
|
|
|
+ path.join(DIR_UPLOAD, originalName),
|
|
|
|
+ path.join(DIR_FILES, userId, newNameFile)
|
|
|
|
+ );
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
if (isChat && isCompanionChat && fileUrl) {
|
|
if (isChat && isCompanionChat && fileUrl) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|