-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
728 lines (606 loc) · 22.7 KB
/
server.js
File metadata and controls
728 lines (606 loc) · 22.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join, normalize } from 'path';
import fs from 'fs';
import multer from 'multer';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = 3000;
// Middleware
app.use(express.json());
// Admin authentication
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'admin123';
let adminToken = null;
function generateToken() {
return 'admin-token-' + Math.random().toString(36).substring(2) + Date.now();
}
function verifyToken(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: '未授权' });
}
const token = authHeader.substring(7);
if (token !== adminToken) {
return res.status(401).json({ error: '无效的令牌' });
}
next();
}
// Configure multer for file uploads
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
// Path will be set in the route handler
cb(null, STORAGE_DIR);
},
filename: (req, file, cb) => {
cb(null, Buffer.from(file.originalname, 'latin1').toString('utf8'));
}
})
});
// Use public directory for file storage
const STORAGE_DIR = join(__dirname, 'public', 'files');
// Ensure storage directory exists
if (!fs.existsSync(STORAGE_DIR)) {
fs.mkdirSync(STORAGE_DIR, { recursive: true });
}
// Prevent path traversal
function safePath(requestPath) {
const normalized = normalize(join(STORAGE_DIR, requestPath || ''));
if (!normalized.startsWith(STORAGE_DIR)) {
throw new Error('Invalid path');
}
return normalized;
}
// List files and folders
app.get('/api/browse', (req, res) => {
try {
const path = req.query.path || '';
const fullPath = safePath(path);
if (!fs.existsSync(fullPath)) {
return res.status(404).json({ error: 'Path not found' });
}
const stats = fs.statSync(fullPath);
if (!stats.isDirectory()) {
return res.status(400).json({ error: 'Not a directory' });
}
// Read metadata
let metadata = {};
let folderMetadata = {};
if (path === '') {
// Root level - read metadata-root.json
const rootMetadataPath = join(STORAGE_DIR, 'metadata-root.json');
if (fs.existsSync(rootMetadataPath)) {
folderMetadata = JSON.parse(fs.readFileSync(rootMetadataPath, 'utf8'));
}
}
// Read current directory metadata.json
const metadataPath = join(fullPath, 'metadata.json');
if (fs.existsSync(metadataPath)) {
const metaContent = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
// Support both formats: object and items array
if (Array.isArray(metaContent.items)) {
// New format: { items: [...] }
metaContent.items.forEach(item => {
metadata[item.name] = item;
});
} else if (typeof metaContent === 'object') {
// Old format: { "filename": { "description": "..." } }
metadata = metaContent;
}
}
const items = fs.readdirSync(fullPath)
.filter(name => {
// Hide all metadata-related files
if (name === 'metadata.json') return false;
if (name === 'metadata-index.json') return false;
if (name === 'metadata-root.json') return false;
if (name.startsWith('metadata-')) return false;
if (name.endsWith('.backup.json')) return false;
return true;
})
.map(name => {
const itemPath = join(fullPath, name);
const itemStats = fs.statSync(itemPath);
const isDirectory = itemStats.isDirectory();
let description = '';
let customModified = '';
let type = 'file';
let downloadSource = '';
let password = '';
if (isDirectory && path === '' && folderMetadata[name]) {
// Root level folder description
description = folderMetadata[name].description || '';
} else if (metadata[name]) {
// File or subfolder description from metadata.json
description = metadata[name].description || '';
// Check for custom modified time
customModified = metadata[name].modified || '';
// Check for type
type = metadata[name].type || 'file';
// Check for download source (admin only)
downloadSource = metadata[name].downloadSource || '';
// Check for password
password = metadata[name].password || '';
}
const item = {
name,
type,
isDirectory,
size: itemStats.size,
modified: itemStats.mtime,
customModified,
description,
downloadSource,
password
};
// Debug log
if (name === 'gzzk-article-card.pdf') {
console.log('PDF item:', JSON.stringify(item, null, 2));
}
return item;
});
// Add URL links from metadata
for (const [key, value] of Object.entries(metadata)) {
if (value.type === 'url' && value.url) {
items.push({
name: key,
type: 'url',
isDirectory: false,
size: 0,
fileSize: value.fileSize || null,
modified: value.modified || null,
customModified: value.modified || '',
description: value.description || '',
url: value.url,
password: value.password || '',
downloadSource: value.downloadSource || ''
});
}
}
// Sort: directories first, then by name
items.sort((a, b) => {
if (a.isDirectory && !b.isDirectory) return -1;
if (!a.isDirectory && b.isDirectory) return 1;
return a.name.localeCompare(b.name);
});
console.log('Browse API returning items:', JSON.stringify(items, null, 2));
res.json({ path, items });
} catch (err) {
console.error('Browse error:', err);
res.status(500).json({ error: 'Failed to list directory' });
}
});
// Download file
app.get('/api/download', async (req, res) => {
try {
const path = req.query.path || '';
console.log('Download request for path:', path);
// Parse path to get directory and filename
const pathParts = path.split('/');
const fileName = pathParts.pop();
const dirPath = pathParts.join('/');
console.log('Parsed - dirPath:', dirPath, 'fileName:', fileName);
// Check if this is a URL link
const metadataPath = join(safePath(dirPath), 'metadata.json');
console.log('Checking metadata at:', metadataPath);
if (fs.existsSync(metadataPath)) {
const metaContent = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
let metadata = {};
// Support both formats: object and items array
if (Array.isArray(metaContent.items)) {
// New format: { items: [...] }
metaContent.items.forEach(item => {
metadata[item.name] = item;
});
console.log('Loaded metadata (new format), items:', Object.keys(metadata));
} else if (typeof metaContent === 'object') {
// Old format: { "filename": { "description": "..." } }
metadata = metaContent;
console.log('Loaded metadata (old format), items:', Object.keys(metadata));
}
console.log('Looking for fileName:', fileName, 'in metadata');
console.log('Metadata entry:', metadata[fileName]);
if (metadata[fileName] && metadata[fileName].type === 'url' && metadata[fileName].url) {
const originalUrl = metadata[fileName].url;
console.log('Found URL link:', originalUrl);
// Redirect to the original URL
console.log('Redirecting to original URL:', originalUrl);
return res.redirect(originalUrl);
}
}
// Otherwise, handle as regular file download
console.log('Not a URL link, treating as regular file');
const fullPath = safePath(path);
if (!fs.existsSync(fullPath)) {
console.log('File not found at:', fullPath);
return res.status(404).json({ error: 'File not found' });
}
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
return res.status(400).json({ error: 'Cannot download directory' });
}
res.download(fullPath);
} catch (err) {
console.error('Download error:', err);
res.status(500).json({ error: 'Download failed' });
}
});
// Admin login
app.post('/api/admin/login', (req, res) => {
const { password } = req.body;
if (password === ADMIN_PASSWORD) {
adminToken = generateToken();
return res.json({ token: adminToken });
}
return res.status(401).json({ error: '密码错误' });
});
// Admin upload files
app.post('/api/admin/upload', verifyToken, upload.array('files'), (req, res) => {
try {
const description = req.body.description;
const uploadPath = req.body.path || '';
const targetDir = safePath(uploadPath);
// Move files from temp location to target directory
req.files.forEach(file => {
const fileName = Buffer.from(file.originalname, 'latin1').toString('utf8');
const targetPath = join(targetDir, fileName);
// Move file if not already in target directory
if (file.path !== targetPath) {
fs.renameSync(file.path, targetPath);
}
});
// Update metadata if description provided
if (description && req.files.length > 0) {
const metadataPath = join(targetDir, 'metadata.json');
let metadata = {};
if (fs.existsSync(metadataPath)) {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
}
// Support both formats
const isNewFormat = Array.isArray(metadata.items);
req.files.forEach(file => {
const fileName = Buffer.from(file.originalname, 'latin1').toString('utf8');
if (isNewFormat) {
const existingIndex = metadata.items.findIndex(item => item.name === fileName);
if (existingIndex >= 0) {
metadata.items[existingIndex].description = description;
} else {
metadata.items.push({ name: fileName, description });
}
} else {
// Old format
if (!metadata[fileName]) {
metadata[fileName] = {};
}
metadata[fileName].description = description;
}
});
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
}
res.json({ success: true, files: req.files.length });
} catch (err) {
console.error('Upload error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin create folder
app.post('/api/admin/create-folder', verifyToken, (req, res) => {
try {
const { path: dirPath, name } = req.body;
const fullPath = join(safePath(dirPath), name);
if (fs.existsSync(fullPath)) {
return res.status(400).json({ error: '文件夹已存在' });
}
fs.mkdirSync(fullPath, { recursive: true });
// Create empty metadata.json
const metadataPath = join(fullPath, 'metadata.json');
fs.writeFileSync(metadataPath, JSON.stringify({ items: [] }, null, 2));
res.json({ success: true });
} catch (err) {
console.error('Create folder error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin delete file or folder
app.delete('/api/admin/delete', verifyToken, (req, res) => {
try {
const { path: itemPath } = req.body;
// Parse path to check if it's a URL link
const pathParts = itemPath.split('/');
const itemName = pathParts.pop();
const dirPath = pathParts.join('/');
// Check metadata first to see if this is a URL link
const metadataPath = join(safePath(dirPath), 'metadata.json');
let isUrlLink = false;
if (fs.existsSync(metadataPath)) {
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
// Check if it's a URL link in metadata
if (metadata[itemName] && metadata[itemName].type === 'url') {
isUrlLink = true;
// Remove from metadata
delete metadata[itemName];
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
return res.json({ success: true });
}
}
// If not a URL link, delete physical file/folder
const fullPath = safePath(itemPath);
if (!fs.existsSync(fullPath)) {
return res.status(404).json({ error: '文件不存在' });
}
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
fs.rmSync(fullPath, { recursive: true, force: true });
} else {
fs.unlinkSync(fullPath);
}
res.json({ success: true });
} catch (err) {
console.error('Delete error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin save metadata
app.post('/api/admin/save-metadata', verifyToken, (req, res) => {
try {
const { path: metadataPath, content } = req.body;
const fullPath = safePath(metadataPath);
if (!fullPath.endsWith('metadata.json')) {
return res.status(400).json({ error: '只能编辑 metadata.json 文件' });
}
// Validate JSON
JSON.parse(content);
fs.writeFileSync(fullPath, content, 'utf8');
res.json({ success: true });
} catch (err) {
console.error('Save metadata error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin update file properties
app.post('/api/admin/update-file-properties', verifyToken, (req, res) => {
try {
const { path: dirPath, fileName, description, modified, url, password, fileSize, downloadSource } = req.body;
console.log('Update file properties:', { dirPath, fileName, description, modified, url, password: password ? '***' : '', fileSize, downloadSource });
const metadataPath = join(safePath(dirPath), 'metadata.json');
let metadata = {};
if (fs.existsSync(metadataPath)) {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
}
console.log('Existing metadata format:', Array.isArray(metadata.items) ? 'new (items array)' : 'old (object)');
// Support both formats
if (Array.isArray(metadata.items)) {
// New format: { items: [...] }
const existingIndex = metadata.items.findIndex(item => item.name === fileName);
if (existingIndex >= 0) {
// Update existing item
if (description) {
metadata.items[existingIndex].description = description;
} else {
delete metadata.items[existingIndex].description;
}
if (modified) {
metadata.items[existingIndex].modified = modified;
} else {
delete metadata.items[existingIndex].modified;
}
if (url) {
metadata.items[existingIndex].url = url;
} else {
delete metadata.items[existingIndex].url;
}
if (password) {
metadata.items[existingIndex].password = password;
} else {
delete metadata.items[existingIndex].password;
}
if (fileSize !== null && fileSize !== undefined) {
metadata.items[existingIndex].fileSize = fileSize;
} else {
delete metadata.items[existingIndex].fileSize;
}
if (downloadSource) {
metadata.items[existingIndex].downloadSource = downloadSource;
} else {
delete metadata.items[existingIndex].downloadSource;
}
// Remove item if no properties left
if (!metadata.items[existingIndex].description &&
!metadata.items[existingIndex].modified &&
!metadata.items[existingIndex].url &&
!metadata.items[existingIndex].password &&
!metadata.items[existingIndex].fileSize &&
!metadata.items[existingIndex].type &&
!metadata.items[existingIndex].downloadSource) {
metadata.items.splice(existingIndex, 1);
}
} else if (description || modified || url || password || fileSize || downloadSource) {
// Add new item
const newItem = { name: fileName };
if (description) newItem.description = description;
if (modified) newItem.modified = modified;
if (url) newItem.url = url;
if (password) newItem.password = password;
if (fileSize !== null && fileSize !== undefined) newItem.fileSize = fileSize;
if (downloadSource) newItem.downloadSource = downloadSource;
metadata.items.push(newItem);
}
} else {
// Old format: { "filename": { "description": "...", "modified": "..." } }
if (description || modified || url || password || fileSize || downloadSource) {
if (!metadata[fileName]) {
metadata[fileName] = {};
}
if (description) {
metadata[fileName].description = description;
} else {
delete metadata[fileName].description;
}
if (modified) {
metadata[fileName].modified = modified;
} else {
delete metadata[fileName].modified;
}
if (url) {
metadata[fileName].url = url;
} else {
delete metadata[fileName].url;
}
if (password) {
metadata[fileName].password = password;
} else {
delete metadata[fileName].password;
}
if (fileSize !== null && fileSize !== undefined) {
metadata[fileName].fileSize = fileSize;
} else {
delete metadata[fileName].fileSize;
}
if (downloadSource) {
metadata[fileName].downloadSource = downloadSource;
} else {
delete metadata[fileName].downloadSource;
}
// Remove entry if no properties left
if (Object.keys(metadata[fileName]).length === 0) {
delete metadata[fileName];
}
} else {
// Remove all properties if both empty
if (metadata[fileName]) {
delete metadata[fileName];
}
}
}
console.log('Saving metadata to:', metadataPath);
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
console.log('Metadata saved successfully');
res.json({ success: true });
} catch (err) {
console.error('Update file properties error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin update folder properties
app.post('/api/admin/update-folder-properties', verifyToken, (req, res) => {
try {
const { path: dirPath, folderName, description } = req.body;
// Determine which metadata file to update
let metadataPath;
if (dirPath === '') {
// Root level - update metadata-root.json
metadataPath = join(STORAGE_DIR, 'metadata-root.json');
} else {
// Subdirectory - update parent's metadata.json
metadataPath = join(safePath(dirPath), 'metadata.json');
}
let metadata = {};
if (fs.existsSync(metadataPath)) {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
}
// For root level, use old format (direct object)
if (dirPath === '') {
if (description) {
metadata[folderName] = { description };
} else {
delete metadata[folderName];
}
} else {
// For subdirectories, support both formats
if (Array.isArray(metadata.items)) {
// New format: { items: [...] }
const existingIndex = metadata.items.findIndex(item => item.name === folderName);
if (existingIndex >= 0) {
if (description) {
metadata.items[existingIndex].description = description;
} else {
delete metadata.items[existingIndex].description;
}
} else if (description) {
metadata.items.push({ name: folderName, description });
}
} else {
// Old format: { "foldername": { "description": "..." } }
if (description) {
if (!metadata[folderName]) {
metadata[folderName] = {};
}
metadata[folderName].description = description;
} else {
if (metadata[folderName]) {
delete metadata[folderName].description;
if (Object.keys(metadata[folderName]).length === 0) {
delete metadata[folderName];
}
}
}
}
}
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
res.json({ success: true });
} catch (err) {
console.error('Update folder properties error:', err);
res.status(500).json({ error: err.message });
}
});
// Admin add URL link
app.post('/api/admin/add-url', verifyToken, (req, res) => {
try {
const { path: dirPath, name, url, password, description, modified, downloadSource } = req.body;
console.log('Add URL:', { dirPath, name, url, password: password ? '***' : '', description, modified, downloadSource });
if (!name || !url) {
return res.status(400).json({ error: '名称和URL不能为空' });
}
const metadataPath = join(safePath(dirPath), 'metadata.json');
let metadata = {};
if (fs.existsSync(metadataPath)) {
metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
}
// Support both formats
if (Array.isArray(metadata.items)) {
// New format: { items: [...] }
const existingIndex = metadata.items.findIndex(item => item.name === name);
if (existingIndex >= 0) {
return res.status(400).json({ error: '该名称已存在' });
}
const newItem = {
name,
type: 'url',
url
};
if (description) newItem.description = description;
if (modified) newItem.modified = modified;
if (password) newItem.password = password;
if (downloadSource) newItem.downloadSource = downloadSource;
metadata.items.push(newItem);
} else {
// Old format: { "filename": { ... } }
if (metadata[name]) {
return res.status(400).json({ error: '该名称已存在' });
}
metadata[name] = {
type: 'url',
url
};
if (description) metadata[name].description = description;
if (modified) metadata[name].modified = modified;
if (password) metadata[name].password = password;
if (downloadSource) metadata[name].downloadSource = downloadSource;
}
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
res.json({ success: true });
} catch (err) {
console.error('Add URL error:', err);
res.status(500).json({ error: err.message });
}
});
// Serve admin panel
app.use('/admin', express.static(join(__dirname, 'admin')));
// Serve test files
app.use(express.static(__dirname));
app.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`);
console.log(`Admin panel: http://localhost:${PORT}/admin`);
console.log(`Admin password: ${ADMIN_PASSWORD}`);
});