Browse Source

worked with aws s3

unknown 1 year ago
parent
commit
3fa38b491a
1 changed files with 11 additions and 2 deletions
  1. 11 2
      controllers/chats.js

+ 11 - 2
controllers/chats.js

@@ -3,7 +3,8 @@ const UserModel = require('../model/user');
 const MessageModel = require('../model/message');
 const fs = require('fs').promises;
 const path = require('path');
-const DIR_STATIC = process.env.DIR_STATIC;
+const s3 = require('../helpers/aws');
+const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
 require('dotenv').config();
 
 const listChats = async (req, res, next) => {
@@ -114,7 +115,15 @@ const removeChatForBoth = async (req, res, next) => {
 		const isCompanionChat = await ChatModel.getByField(userId, companionId);
 		if (isUserChat && isCompanionChat) {
 			const deleteFile = async (type, message) => {
-				if (type !== 'text') await fs.unlink(path.join(DIR_STATIC, message));
+				if (type !== 'text') {
+					const params = {
+						Bucket: AWS_BUCKET_NAME,
+						Key: `${message}`,
+					};
+					s3.deleteObject(params, async function (err, _data) {
+						if (err) throw err;
+					});
+				}
 			};
 			const { messages } = await MessageModel.getList(
 				{ owner: userId, companionId },