Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
307 lines (271 loc) · 8.29 KB
/
Copy pathserver.js
File metadata and controls
307 lines (271 loc) · 8.29 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const http = require('http');
const socketIo = require('socket.io');
const QRCode = require('qrcode');
const crypto = require('crypto');
const os = require('os');
// 读取配置
const config = require('./config.json');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// 确保上传目录存在
let uploadPath;
// 检测是否在 Electron 环境中运行
function isElectronEnvironment() {
return process.versions && process.versions.electron;
}
if (isElectronEnvironment()) {
// 在 Electron 环境中,使用 app.getPath('userData') 作为基础路径
try {
const { app } = require('electron');
const userDataPath = app.getPath('userData');
uploadPath = path.join(userDataPath, 'uploads');
} catch (e) {
// 如果获取失败,使用配置路径
uploadPath = path.resolve(process.cwd(), config.uploadPath);
}
} else if (config.uploadPath.startsWith('./') || config.uploadPath.startsWith('../')) {
// 相对路径,使用当前工作目录
uploadPath = path.resolve(process.cwd(), config.uploadPath);
} else {
// 绝对路径
uploadPath = path.resolve(config.uploadPath);
}
// 创建目录(如果不存在)
try {
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath, { recursive: true });
}
} catch (error) {
console.error('创建上传目录失败:', error);
// 如果失败,尝试使用临时目录
uploadPath = path.join(os.tmpdir(), 'qr-file-transfer-uploads');
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath, { recursive: true });
}
}
// 存储活跃的上传令牌
const activeTokens = new Map();
// 配置文件存储
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadPath);
},
filename: (req, file, cb) => {
// 使用时间戳和原始文件名
const timestamp = Date.now();
const originalName = Buffer.from(file.originalname, 'latin1').toString('utf8');
cb(null, `${timestamp}-${originalName}`);
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: config.maxFileSize * 1024 * 1024 // 转换为字节
}
});
// 获取本机IP地址
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return 'localhost';
}
// 生成唯一令牌
function generateToken() {
return crypto.randomBytes(16).toString('hex');
}
// 清理过期令牌
function cleanExpiredTokens() {
const now = Date.now();
for (const [token, data] of activeTokens.entries()) {
if (now - data.createdAt > config.tokenExpireMinutes * 60 * 1000) {
activeTokens.delete(token);
}
}
}
// 每分钟清理一次过期令牌
setInterval(cleanExpiredTokens, 60000);
// 静态文件服务
// 在打包环境中,public 目录的路径需要特殊处理
const publicPath = path.join(__dirname, 'public');
app.use(express.static(publicPath));
app.use(express.json());
// 添加根路由,重定向到 index.html
app.get('/', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
// 生成新的上传令牌和二维码
app.get('/api/generate-qr', async (req, res) => {
try {
const token = generateToken();
const localIP = getLocalIP();
const uploadUrl = `http://${localIP}:${config.port}/upload/${token}`;
activeTokens.set(token, {
createdAt: Date.now(),
used: false
});
const qrCodeDataUrl = await QRCode.toDataURL(uploadUrl, {
width: 300,
margin: 2,
color: {
dark: '#000000',
light: '#FFFFFF'
}
});
res.json({
qrCode: qrCodeDataUrl,
token: token,
uploadUrl: uploadUrl,
expiresIn: config.tokenExpireMinutes * 60
});
} catch (error) {
res.status(500).json({ error: '生成二维码失败' });
}
});
// 上传页面路由
app.get('/upload/:token', (req, res) => {
const token = req.params.token;
if (!activeTokens.has(token)) {
return res.send(`
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>链接已失效</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.container {
background: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
}
h1 { color: #e74c3c; margin-bottom: 1rem; }
p { color: #666; }
</style>
</head>
<body>
<div class="container">
<h1>⚠️ 链接已失效</h1>
<p>该上传链接已过期或无效</p>
<p>请重新扫描二维码</p>
</div>
</body>
</html>
`);
}
res.sendFile(path.join(__dirname, 'public', 'upload.html'));
});
// 获取上传路径的 API
app.get('/api/upload-path', (req, res) => {
res.json({ path: uploadPath });
});
// 打开上传文件夹的 API
app.get('/api/open-folder', (req, res) => {
try {
const { exec } = require('child_process');
// 根据操作系统使用不同的命令
let command;
if (process.platform === 'darwin') {
// macOS - 使用 open 命令在 Finder 中打开
command = `open "${uploadPath}"`;
} else if (process.platform === 'win32') {
// Windows - 使用 explorer 命令,需要转换路径格式
// 将正斜杠转换为反斜杠,这在 Windows 上更可靠
const winPath = uploadPath.replace(/\//g, '\\');
command = `explorer "${winPath}"`;
} else {
// Linux - 使用 xdg-open
command = `xdg-open "${uploadPath}"`;
}
console.log(`执行命令: ${command}`);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error('打开文件夹失败:', error);
console.error('stderr:', stderr);
res.json({
success: false,
path: uploadPath,
error: error.message,
details: stderr
});
} else {
console.log('文件夹已打开');
res.json({ success: true, path: uploadPath });
}
});
} catch (error) {
console.error('打开文件夹异常:', error);
res.json({
success: false,
path: uploadPath,
error: error.message
});
}
});
// 文件上传处理
app.post('/api/upload/:token', upload.array('files', 10), (req, res) => {
const token = req.params.token;
if (!activeTokens.has(token)) {
return res.status(400).json({ error: '无效的上传令牌' });
}
if (!req.files || req.files.length === 0) {
return res.status(400).json({ error: '没有文件被上传' });
}
// 标记令牌已使用
const tokenData = activeTokens.get(token);
tokenData.used = true;
// 通知所有连接的客户端刷新二维码
io.emit('upload-complete', {
fileCount: req.files.length,
files: req.files.map(f => ({
name: f.filename,
size: f.size,
path: f.path
}))
});
res.json({
success: true,
message: `成功上传 ${req.files.length} 个文件`,
files: req.files.map(f => f.filename)
});
});
// WebSocket连接
io.on('connection', (socket) => {
console.log('客户端已连接');
socket.on('disconnect', () => {
console.log('客户端已断开');
});
});
// 启动服务器
const PORT = config.port;
server.listen(PORT, () => {
const localIP = getLocalIP();
console.log('\n=================================');
console.log('🚀 二维码文件传输服务已启动');
console.log('=================================');
console.log(`📱 本地访问: http://localhost:${PORT}`);
console.log(`📱 局域网访问: http://${localIP}:${PORT}`);
console.log(`📁 文件保存位置: ${uploadPath}`);
console.log('=================================\n');
});