-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
232 lines (207 loc) · 6.94 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const express = require('express')
const app = express()
const server = require('http').Server(app)
const multer = require('multer')
const path = require('path')
const url = require('url')
const fs = require('fs')
const uuid = require('node-uuid')
const io = require('socket.io')(server, {
cors: {
origin: "http://localhost:3000"
}
})
const cors = require('cors')
const jwt = require('jsonwebtoken')
const accountsService=require('./accounts')
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}))
io.on('connection', socket => {
const id = socket.handshake.query.id
socket.join(id)
socket.on('send-message', ({ recipients, text }) => {
recipients.forEach(recipient => {
//为接收到信息的人正确设置信息接收人和发送者
const newRecipients = recipients.filter(r => r !== recipient)
newRecipients.push(id)
socket.broadcast.to(recipient).emit('receive-message', {
recipients: newRecipients,
sender: id,
text
})
})
})
socket.on('send-post', (post) => {
post.recipients.forEach(recipient => {
socket.broadcast.to(recipient).emit('receive-post', { ...post })
})
})
socket.on('like-post', (post) => {
const { posterId, UUID, likerId } = post
post.recipients.push(posterId)
post.recipients.forEach(recipient => {
socket.to(recipient).emit('receive-like', {
UUID: UUID,
likerId: likerId
})
})
})
socket.on('dislike-post', (post) => {
const { posterId, UUID, dislikerId } = post
post.recipients.push(posterId)
post.recipients.forEach(recipient => {
socket.to(recipient).emit('cancel-like', {
UUID: UUID,
dislikerId: dislikerId
})
})
})
socket.on('send-comment', (data) => {
const { UUID, posterId, recipients, text, commenterId } = data
recipients.push(posterId)
recipients.forEach(recipient => {
socket.to(recipient).emit('receive-comment', {
UUID,
commenterId,
text
})
})
})
})
// app.post('/login', (req, res) => {
// // 假设用户身份验证成功,生成令牌
// const token = jwt.sign({ userId: req.body.id }, 'Token', { expiresIn: '1h' })
// const refreshToken = jwt.sign({ userId: req.body.id }, 'RefreshToken')
// // 将令牌发送给客户
// res.json({ token, refreshToken })
// })
//
// // Refresh Token 验证中间件
// const authenticateRefreshToken = (req, res, next) => {
// const refreshToken = req.body.refreshToken // 假设 Refresh Token 存储在请求体的 refreshToken 字段中
//
// if (!refreshToken) {
// return res.status(401).json({ message: '未提供 Refresh Token' })
// }
//
// // 验证 Refresh Token
// jwt.verify(refreshToken, 'RefreshToken', (err, decoded) => {
// if (err) {
// return res.status(403).json({ message: 'RefreshToken 无效' })
// }
// // Refresh Token 验证通过,将解码后的用户信息保存到 req 对象中
// req.user = decoded
// next()
// })
// }
//
// app.post('/update', authenticateRefreshToken, (req, res) => {
// // Refresh Token 验证通过,生成新的 Access Token
// const token = jwt.sign({ userId: req.user.userId }, 'Token', { expiresIn: '1h' })
// // 将新的 Access Token 发送给客户端
// res.json({ token })
// })
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './avatars')
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname)
const fileName = req.query.id + ext
cb(null, fileName)
}
})
const uploadFile = multer({ storage: storage })
app.post("/upload", uploadFile.single('avatar'), (req, res) => {
// 构建文件的完整URL
try {
// 处理上传成功逻辑
res.send("success")
} catch (error) {
// 处理上传失败逻辑
res.status(500).send("Upload failed")
}
})
app.get('/avatar', (req, res) => {
const id = req.query.id
const filePathJPG = path.resolve(__dirname, 'avatars', id + '.jpg') // 构建 JPG 文件路径
const filePathPNG = path.resolve(__dirname, 'avatars', id + '.png') // 构建 PNG 文件路径
const filePathJPEG = path.resolve(__dirname, 'avatars', id + '.jpeg') // 构建 JPG 文件路径
let filePath = ''
// 检查 JPG 文件是否存在
if (fs.existsSync(filePathJPG)) {
filePath = filePathJPG
}
// 检查 PNG 文件是否存在
else if (fs.existsSync(filePathPNG)) {
filePath = filePathPNG
} else if (fs.existsSync(filePathJPEG)) {
filePath = filePathJPEG
}
if (filePath !== '') {
res.sendFile(filePath)
} else {
res.status(404).send("Avatar not found")
}
})
//瞬间的贴文相关
const postimgStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './posts')
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname)
const newuuid = uuid.v4()
const fileName = newuuid + ext
cb(null, fileName)
}
})
const uploadPostImgFile = multer({ storage: postimgStorage })
app.post('/uploadpostimg', uploadPostImgFile.single('file'), (req, res) => {
// 构建文件的完整URL
const newuuid = req.file.filename.split('.')[0]
const resUrl = "http://localhost:4998/postimg?uuid=" + newuuid
try {
res.json({
success: true,
url: resUrl
})
} catch (error) {
// 处理上传失败逻辑
res.status(500).send("Upload failed")
}
})
app.get('/postimg', (req, res) => {
const uuid = req.query.uuid // 获取客户端传递的 UUID
if (!uuid) {
return res.status(400).send('Missing UUID')
}
const filePathJPG = path.resolve(__dirname, 'posts', uuid + '.jpg') // 构建 JPG 文件路径
const filePathPNG = path.resolve(__dirname, 'posts', uuid + '.png') // 构建 PNG 文件路径
const filePathJPEG = path.resolve(__dirname, 'posts', uuid + '.jpeg') // 构建 JPG 文件路径
let filePath = ''
// 检查 JPG 文件是否存在
if (fs.existsSync(filePathJPG)) {
filePath = filePathJPG
}
// 检查 PNG 文件是否存在
else if (fs.existsSync(filePathPNG)) {
filePath = filePathPNG
} else if (fs.existsSync(filePathJPEG)) {
filePath = filePathJPEG
}
res.sendFile(filePath, (err) => {
if (err) {
// 如果发送文件时出错,则返回错误响应
console.error(err)
res.status(500).send('Error sending file')
}
})
})
accountsService(app)
// 启动服务器
server.listen(4998, () => {
console.log('Socket.IO server started on port 4998')
})