Browse Source

ready to start server

unknown 2 years ago
parent
commit
97ee8b1e45

+ 3 - 4
app.js

@@ -9,15 +9,14 @@ const messagesRouter = require('./routes/messages');
 const contactsRouter = require('./routes/contacts');
 const userRoute = require('./routes/user');
 const app = express();
-const DIR_FILES = process.env.DIR_FILES;
-const DIR_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
+const DIR_STATIC = process.env.DIR_STATIC;
+
 const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
 app.use(helmet());
 app.use(logger(formatsLogger));
 app.use(cors());
 app.use(express.json());
-app.use(express.static(path.join(__dirname, DIR_FILES)));
-app.use(express.static(path.join(__dirname, DIR_NOTIFICATIONS)));
+app.use(express.static(path.join(__dirname, DIR_STATIC)));
 app.use('/api/chats', apiLimiter, chatsRouter);
 app.use('/api/messages', apiLimiter, messagesRouter);
 app.use('/api/contacts', apiLimiter, contactsRouter);

+ 2 - 2
bin/server.js

@@ -6,9 +6,9 @@ const PORT = process.env.PORT || 3000;
 db.then(() => {
 	app.listen(PORT, async () => {
 		const DIR_UPLOAD = process.env.DIR_UPLOAD;
-		const DIR_FILES = process.env.DIR_FILES;
+		const DIR_STATIC = process.env.DIR_STATIC;
 		await createFolderIsExist(DIR_UPLOAD);
-		await createFolderIsExist(DIR_FILES);
+		await createFolderIsExist(DIR_STATIC);
 		console.log(`Server running. Use our API on port: ${PORT}`);
 	});
 }).catch((err) => {

+ 2 - 2
controllers/chats.js

@@ -3,7 +3,7 @@ const UserModel = require('../model/user');
 const MessageModel = require('../model/message');
 const fs = require('fs').promises;
 const path = require('path');
-const DIR_FILES = process.env.DIR_FILES;
+const DIR_STATIC = process.env.DIR_STATIC;
 require('dotenv').config();
 
 const listChats = async (req, res, next) => {
@@ -131,7 +131,7 @@ 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_FILES, message));
+				if (type !== 'text') await fs.unlink(path.join(DIR_STATIC, message));
 			};
 			const { messages } = await MessageModel.getList(
 				{ owner: userId, companionId },

+ 10 - 10
controllers/messages.js

@@ -4,7 +4,7 @@ const ChatModel = require('../model/chat');
 const fs = require('fs').promises;
 const path = require('path');
 const createFolderIsExist = require('../helpers/create-directory');
-const DIR_FILES = process.env.DIR_FILES;
+const DIR_STATIC = process.env.DIR_STATIC;
 require('dotenv').config();
 
 const listMessages = async (req, res, next) => {
@@ -32,7 +32,7 @@ const removeMessage = async (req, res, next) => {
 			userMessage.companionId
 		);
 		if (userMessage.type !== 'text')
-			await fs.unlink(path.join(DIR_FILES, userMessage.message));
+			await fs.unlink(path.join(DIR_STATIC, userMessage.message));
 
 		const isChat = await ChatModel.getByField(userMessage.companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(
@@ -165,8 +165,8 @@ const imageMessage = async (req, res, next) => {
 		const pathToFile = req.file.path;
 		const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameImg));
+		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 { name, lastName, avatarUrl, color, number } = req.user;
@@ -240,8 +240,8 @@ const audioMessage = async (req, res, next) => {
 		const pathToFile = req.file.path;
 		const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameAudio));
+		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 { name, lastName, avatarUrl, color, number } = req.user;
@@ -315,8 +315,8 @@ const videoMessage = async (req, res, next) => {
 		const pathToFile = req.file.path;
 		const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameVideo));
+		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 { name, lastName, avatarUrl, color, number } = req.user;
@@ -404,8 +404,8 @@ const fileMessage = async (req, res, next) => {
 				break;
 		}
 		const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
-		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
+		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 { name, lastName, avatarUrl, color, number } = req.user;

+ 9 - 9
controllers/user.js

@@ -10,7 +10,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_FILES = process.env.DIR_FILES;
+const DIR_STATIC = process.env.DIR_STATIC;
 
 const createNewUser = async (req, res, next) => {
 	try {
@@ -23,11 +23,11 @@ const createNewUser = async (req, res, next) => {
 		} else {
 			await UserModel.createUser({ number, country, color, code });
 		}
-		// client.messages.create({
-		// 	body: `${code}`,
-		// 	to: `${number}`,
-		// 	from: '+18305875860',
-		// });
+		client.messages.create({
+			body: `${code}`,
+			to: `${number}`,
+			from: '+18305875860',
+		});
 		return res.status(201).json({
 			status: 'success',
 			code: 201,
@@ -176,7 +176,7 @@ const removeAvatar = async (req, res, next) => {
 		const filteredAvatars = [...avatarsArr].filter(
 			({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
 		);
-		await fs.unlink(path.join(DIR_FILES, toDelete.avatarUrl));
+		await fs.unlink(path.join(DIR_STATIC, toDelete.avatarUrl));
 		const avatarUrl = filteredAvatars[0].avatarUrl;
 		await UserModel.updateUser(id, { avatarUrl, avatarsArr: filteredAvatars });
 		await ChatModel.updateCompanionsChat(id, {
@@ -204,8 +204,8 @@ const updateAvatar = async (req, res, next) => {
 		const pathToFile = req.file.path;
 		const originalName = req.file.originalname;
 		const newNameAvatar = `${Math.round(Date.now() / 1000)}${originalName}`;
-		await createFolderIsExist(path.join(DIR_FILES, userId));
-		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameAvatar));
+		await createFolderIsExist(path.join(DIR_STATIC, userId));
+		await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameAvatar));
 		const avatarUrl = path.normalize(path.join(userId, newNameAvatar));
 		const avatarsArr = [
 			{ avatarUrl, updatedAt: new Date() },

BIN
files/6271155a86a19c6468893c42/1651578350we.jpg


BIN
files/6271169f7d65962628657734/1651578539download.png


BIN
notifications/song.mp3


notifications/recive.mp3 → static/notifications/receive.mp3


notifications/send.mp3 → static/notifications/send.mp3