|
@@ -4,10 +4,6 @@ const ChatModel = require('../model/chat');
|
|
|
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();
|
|
|
|
|
@@ -35,22 +31,9 @@ const removeMessage = async (req, res, next) => {
|
|
|
userMessage.idTime,
|
|
|
userMessage.companionId
|
|
|
);
|
|
|
- if (userMessage.type !== 'text') {
|
|
|
- switch (userMessage.type) {
|
|
|
- case 'image':
|
|
|
- await fs.unlink(path.join(DIR_IMAGES, userMessage.message));
|
|
|
- break;
|
|
|
- case 'audio':
|
|
|
- await fs.unlink(path.join(DIR_AUDIOS, userMessage.message));
|
|
|
- break;
|
|
|
- case 'video':
|
|
|
- await fs.unlink(path.join(DIR_VIDEOS, userMessage.message));
|
|
|
- break;
|
|
|
- default:
|
|
|
- await fs.unlink(path.join(DIR_FILES, userMessage.message));
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (userMessage.type !== 'text')
|
|
|
+ await fs.unlink(path.join(DIR_FILES, userMessage.message));
|
|
|
+
|
|
|
const isChat = await ChatModel.getByField(userMessage.companionId, userId);
|
|
|
const isCompanionChat = await ChatModel.getByField(
|
|
|
userId,
|
|
@@ -179,13 +162,11 @@ const imageMessage = async (req, res, next) => {
|
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
|
const originalName = req.file.originalname;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
const fullType = req.file.mimetype;
|
|
|
- await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
|
- await fs.rename(
|
|
|
- path.join(DIR_UPLOAD, originalName),
|
|
|
- path.join(DIR_IMAGES, userId, newNameImg)
|
|
|
- );
|
|
|
+ await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameImg));
|
|
|
const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
|
if (isChat && isCompanionChat && imgUrl) {
|
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
@@ -256,13 +237,11 @@ const audioMessage = async (req, res, next) => {
|
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
|
const originalName = req.file.originalname;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
const fullType = req.file.mimetype;
|
|
|
- await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
|
- await fs.rename(
|
|
|
- path.join(DIR_UPLOAD, originalName),
|
|
|
- path.join(DIR_IMAGES, userId, newNameAudio)
|
|
|
- );
|
|
|
+ await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameAudio));
|
|
|
const audioUrl = path.normalize(path.join(userId, newNameAudio));
|
|
|
if (isChat && isCompanionChat && audioUrl) {
|
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
@@ -333,13 +312,11 @@ const videoMessage = async (req, res, next) => {
|
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
|
const originalName = req.file.originalname;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
const fullType = req.file.mimetype;
|
|
|
- await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
|
- await fs.rename(
|
|
|
- path.join(DIR_UPLOAD, originalName),
|
|
|
- path.join(DIR_VIDEOS, userId, newNameVideo)
|
|
|
- );
|
|
|
+ await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameVideo));
|
|
|
const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
|
if (isChat && isCompanionChat && videoUrl) {
|
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
@@ -410,6 +387,7 @@ const fileMessage = async (req, res, next) => {
|
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
|
const originalName = req.file.originalname;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
const fullType = req.file.mimetype;
|
|
|
let type;
|
|
|
switch (fullType) {
|
|
@@ -427,10 +405,7 @@ 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(
|
|
|
- path.join(DIR_UPLOAD, originalName),
|
|
|
- path.join(DIR_FILES, userId, newNameFile)
|
|
|
- );
|
|
|
+ await fs.rename(pathToFile, 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;
|