|
@@ -2,9 +2,8 @@ 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 fs = require('fs').promises;
|
|
const fs = require('fs').promises;
|
|
-const path = require('path');
|
|
|
|
-const createFolderIsExist = require('../helpers/create-directory');
|
|
|
|
-const DIR_STATIC = process.env.DIR_STATIC;
|
|
|
|
|
|
+const s3 = require('../helpers/aws');
|
|
|
|
+const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
|
|
require('dotenv').config();
|
|
require('dotenv').config();
|
|
|
|
|
|
const listMessages = async (req, res, next) => {
|
|
const listMessages = async (req, res, next) => {
|
|
@@ -21,6 +20,24 @@ const listMessages = async (req, res, next) => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+const listMessagesById = async (req, res, next) => {
|
|
|
|
+ try {
|
|
|
|
+ const userId = req.user.id;
|
|
|
|
+ const companionId = req.params.companionId;
|
|
|
|
+ const messages = await MessageModel.getList(
|
|
|
|
+ { owner: userId, companionId },
|
|
|
|
+ {}
|
|
|
|
+ );
|
|
|
|
+ return res.json({
|
|
|
|
+ status: 'success',
|
|
|
|
+ code: 200,
|
|
|
|
+ data: messages,
|
|
|
|
+ });
|
|
|
|
+ } catch (e) {
|
|
|
|
+ next(e);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
const removeMessage = async (req, res, next) => {
|
|
const removeMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const id = req.params.id;
|
|
const id = req.params.id;
|
|
@@ -31,8 +48,15 @@ const removeMessage = async (req, res, next) => {
|
|
userMessage.idTime,
|
|
userMessage.idTime,
|
|
userMessage.companionId
|
|
userMessage.companionId
|
|
);
|
|
);
|
|
- if (userMessage.type !== 'text')
|
|
|
|
- await fs.unlink(path.join(DIR_STATIC, userMessage.message));
|
|
|
|
|
|
+ if (userMessage.type !== 'text') {
|
|
|
|
+ const params = {
|
|
|
|
+ Bucket: AWS_BUCKET_NAME,
|
|
|
|
+ Key: `${userMessage.message}`,
|
|
|
|
+ };
|
|
|
|
+ s3.deleteObject(params, async function (err, _data) {
|
|
|
|
+ if (err) throw err;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
const isChat = await ChatModel.getByField(userMessage.companionId, userId);
|
|
const isChat = await ChatModel.getByField(userMessage.companionId, userId);
|
|
const isCompanionChat = await ChatModel.getByField(
|
|
const isCompanionChat = await ChatModel.getByField(
|
|
@@ -67,24 +91,6 @@ const removeMessage = async (req, res, next) => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-const listMessagesById = async (req, res, next) => {
|
|
|
|
- try {
|
|
|
|
- const userId = req.user.id;
|
|
|
|
- const companionId = req.params.companionId;
|
|
|
|
- const messages = await MessageModel.getList(
|
|
|
|
- { owner: userId, companionId },
|
|
|
|
- {}
|
|
|
|
- );
|
|
|
|
- return res.json({
|
|
|
|
- status: 'success',
|
|
|
|
- code: 200,
|
|
|
|
- data: messages,
|
|
|
|
- });
|
|
|
|
- } catch (e) {
|
|
|
|
- next(e);
|
|
|
|
- }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
const sentMessage = async (req, res, next) => {
|
|
const sentMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const { id, message } = req.body;
|
|
const { id, message } = req.body;
|
|
@@ -163,12 +169,19 @@ const imageMessage = async (req, res, next) => {
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await createFolderIsExist(path.join(DIR_STATIC, userId));
|
|
|
|
- await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameImg));
|
|
|
|
- const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
|
|
- if (isChat && isCompanionChat && imgUrl) {
|
|
|
|
|
|
+ const imgUrl = `${Math.round(Date.now() / 1000)}${userId}${originalName}`;
|
|
|
|
+ const fileContent = await fs.readFile(pathToFile);
|
|
|
|
+ const params = {
|
|
|
|
+ Bucket: AWS_BUCKET_NAME,
|
|
|
|
+ Key: `${imgUrl}`,
|
|
|
|
+ Body: fileContent,
|
|
|
|
+ };
|
|
|
|
+ s3.upload(params, async (err, _data) => {
|
|
|
|
+ if (err) throw err;
|
|
|
|
+ fs.unlink(pathToFile);
|
|
|
|
+ });
|
|
|
|
+ if (isChat && isCompanionChat) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const newMessage = await MessageModel.add({
|
|
const newMessage = await MessageModel.add({
|
|
message: imgUrl,
|
|
message: imgUrl,
|
|
@@ -238,12 +251,19 @@ const audioMessage = async (req, res, next) => {
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await createFolderIsExist(path.join(DIR_STATIC, userId));
|
|
|
|
- await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameAudio));
|
|
|
|
- const audioUrl = path.normalize(path.join(userId, newNameAudio));
|
|
|
|
- if (isChat && isCompanionChat && audioUrl) {
|
|
|
|
|
|
+ const audioUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
|
+ const fileContent = await fs.readFile(pathToFile);
|
|
|
|
+ const params = {
|
|
|
|
+ Bucket: AWS_BUCKET_NAME,
|
|
|
|
+ Key: `${audioUrl}`,
|
|
|
|
+ Body: fileContent,
|
|
|
|
+ };
|
|
|
|
+ s3.upload(params, async (err, _data) => {
|
|
|
|
+ if (err) throw err;
|
|
|
|
+ fs.unlink(pathToFile);
|
|
|
|
+ });
|
|
|
|
+ if (isChat && isCompanionChat) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const newMessage = await MessageModel.add({
|
|
const newMessage = await MessageModel.add({
|
|
message: audioUrl,
|
|
message: audioUrl,
|
|
@@ -313,12 +333,19 @@ const videoMessage = async (req, res, next) => {
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const originalName = req.file.originalname;
|
|
const originalName = req.file.originalname;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
|
|
|
|
+ const videoUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
- await createFolderIsExist(path.join(DIR_STATIC, userId));
|
|
|
|
- await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameVideo));
|
|
|
|
- const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
|
|
- if (isChat && isCompanionChat && videoUrl) {
|
|
|
|
|
|
+ const fileContent = await fs.readFile(pathToFile);
|
|
|
|
+ const params = {
|
|
|
|
+ Bucket: AWS_BUCKET_NAME,
|
|
|
|
+ Key: `${videoUrl}`,
|
|
|
|
+ Body: fileContent,
|
|
|
|
+ };
|
|
|
|
+ s3.upload(params, async (err, _data) => {
|
|
|
|
+ if (err) throw err;
|
|
|
|
+ fs.unlink(pathToFile);
|
|
|
|
+ });
|
|
|
|
+ if (isChat && isCompanionChat) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const newMessage = await MessageModel.add({
|
|
const newMessage = await MessageModel.add({
|
|
message: videoUrl,
|
|
message: videoUrl,
|
|
@@ -386,7 +413,6 @@ const fileMessage = 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 originalName = req.file.originalname;
|
|
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
let type;
|
|
let type;
|
|
@@ -403,11 +429,18 @@ const fileMessage = async (req, res, next) => {
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
- const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
|
|
- await createFolderIsExist(path.join(DIR_STATIC, userId));
|
|
|
|
- await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameFile));
|
|
|
|
- const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
|
|
- if (isChat && isCompanionChat && fileUrl) {
|
|
|
|
|
|
+ const fileUrl = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
|
|
+ const fileContent = await fs.readFile(pathToFile);
|
|
|
|
+ const params = {
|
|
|
|
+ Bucket: AWS_BUCKET_NAME,
|
|
|
|
+ Key: `${fileUrl}`,
|
|
|
|
+ Body: fileContent,
|
|
|
|
+ };
|
|
|
|
+ s3.upload(params, async (err, _data) => {
|
|
|
|
+ if (err) throw err;
|
|
|
|
+ fs.unlink(pathToFile);
|
|
|
|
+ });
|
|
|
|
+ if (isChat && isCompanionChat) {
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const { name, lastName, avatarUrl, color, number } = req.user;
|
|
const newMessage = await MessageModel.add({
|
|
const newMessage = await MessageModel.add({
|
|
message: fileUrl,
|
|
message: fileUrl,
|