瀏覽代碼

mayby ready

unknown 2 年之前
父節點
當前提交
820dae2ce6
共有 5 個文件被更改,包括 6 次插入34 次删除
  1. 2 2
      bin/server.js
  2. 2 20
      controllers/chats.js
  3. 2 12
      controllers/user.js
  4. 二進制
      files/6271155a86a19c6468893c42/1651578350we.jpg
  5. 二進制
      files/6271169f7d65962628657734/1651578539download.png

+ 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_IMAGES = process.env.DIR_IMAGES;
+		const DIR_FILES = process.env.DIR_FILES;
 		await createFolderIsExist(DIR_UPLOAD);
-		await createFolderIsExist(DIR_IMAGES);
+		await createFolderIsExist(DIR_FILES);
 		console.log(`Server running. Use our API on port: ${PORT}`);
 	});
 }).catch((err) => {

+ 2 - 20
controllers/chats.js

@@ -3,6 +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;
 require('dotenv').config();
 
 const listChats = async (req, res, next) => {
@@ -126,30 +127,11 @@ const removeChatForBoth = async (req, res, next) => {
 	try {
 		const companionId = req.params.id;
 		const userId = req.user.id;
-		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 isUserChat = await ChatModel.getByField(companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, companionId);
 		if (isUserChat && isCompanionChat) {
 			const deleteFile = async (type, message) => {
-				if (type !== 'text') {
-					switch (type) {
-						case 'image':
-							await fs.unlink(path.join(DIR_IMAGES, message));
-							break;
-						case 'audio':
-							await fs.unlink(path.join(DIR_AUDIOS, message));
-							break;
-						case 'video':
-							await fs.unlink(path.join(DIR_VIDEOS, message));
-							break;
-						default:
-							await fs.unlink(path.join(DIR_FILES, message));
-							break;
-					}
-				}
+				if (type !== 'text') await fs.unlink(path.join(DIR_FILES, message));
 			};
 			const { messages } = await MessageModel.getList(
 				{ owner: userId, companionId },

+ 2 - 12
controllers/user.js

@@ -4,7 +4,6 @@ const MessageModel = require('../model/message');
 const ContactModel = require('../model/contact');
 const fs = require('fs').promises;
 const path = require('path');
-const Jimp = require('jimp');
 const jwt = require('jsonwebtoken');
 const createFolderIsExist = require('../helpers/create-directory');
 require('dotenv').config();
@@ -205,17 +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}`;
-		const img = await Jimp.read(pathToFile);
-		await img
-			.autocrop()
-			.cover(
-				250,
-				250,
-				Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
-			)
-			.writeAsync(pathToFile);
-		await createFolderIsExist(path.join(DIR_IMAGES, userId));
-		await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameAvatar));
+		await createFolderIsExist(path.join(DIR_FILES, userId));
+		await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameAvatar));
 		const avatarUrl = path.normalize(path.join(userId, newNameAvatar));
 		const avatarsArr = [
 			{ avatarUrl, updatedAt: new Date() },

二進制
files/6271155a86a19c6468893c42/1651578350we.jpg


二進制
files/6271169f7d65962628657734/1651578539download.png