unknown 2 éve
szülő
commit
f545f25bcd

+ 0 - 6
app.js

@@ -9,9 +9,6 @@ const messagesRouter = require('./routes/messages');
 const contactsRouter = require('./routes/contacts');
 const userRoute = require('./routes/user');
 const app = express();
-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 DIR_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
 const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
@@ -19,9 +16,6 @@ app.use(helmet());
 app.use(logger(formatsLogger));
 app.use(cors());
 app.use(express.json());
-app.use(express.static(path.join(__dirname, DIR_IMAGES)));
-app.use(express.static(path.join(__dirname, DIR_AUDIOS)));
-app.use(express.static(path.join(__dirname, DIR_VIDEOS)));
 app.use(express.static(path.join(__dirname, DIR_FILES)));
 app.use(express.static(path.join(__dirname, DIR_NOTIFICATIONS)));
 app.use('/api/chats', apiLimiter, chatsRouter);

+ 14 - 39
controllers/messages.js

@@ -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;

+ 2 - 2
controllers/user.js

@@ -11,7 +11,7 @@ require('dotenv').config();
 const client = require('../helpers/twilio');
 const phoneToken = require('generate-sms-verification-code');
 const SECRET_KEY = process.env.JWT_SECRET;
-const DIR_IMAGES = process.env.DIR_IMAGES;
+const DIR_FILES = process.env.DIR_FILES;
 
 const createNewUser = async (req, res, next) => {
 	try {
@@ -177,7 +177,7 @@ const removeAvatar = async (req, res, next) => {
 		const filteredAvatars = [...avatarsArr].filter(
 			({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
 		);
-		await fs.unlink(path.join(DIR_IMAGES, toDelete.avatarUrl));
+		await fs.unlink(path.join(DIR_FILES, toDelete.avatarUrl));
 		const avatarUrl = filteredAvatars[0].avatarUrl;
 		await UserModel.updateUser(id, { avatarUrl, avatarsArr: filteredAvatars });
 		await ChatModel.updateCompanionsChat(id, {

BIN
images/6270fce097abc354984962b7/1651571961we.jpg


BIN
images/6270fce097abc354984962b7/1651572744we.jpg


BIN
images/6270fe363c9767458820d73d/1651572289monkey.png


BIN
images/6270ff7f3c9767458820d741/1651572627we.jpg