-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
525 lines (432 loc) · 17.6 KB
/
api.js
File metadata and controls
525 lines (432 loc) · 17.6 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
/**
* @file API Service
* @author Bei-Chen-Leo
* @date 2025-08-06 14:57:39
*/
const fs = require('fs-extra');
const path = require('path');
const mimeTypes = require('mime-types');
const { log, safeReadJson } = require('./utils');
let config;
let cacheManager;
let imageList = {};
let imageDetails = [];
// 文件存在性缓存
const fileExistsCache = new Map();
const FILE_CACHE_TTL = 300000; // 5分钟缓存
const MAX_FILE_CACHE_SIZE = 10000;
// 限流器实现
const requestLimiter = {
windows: new Map(),
windowSize: 1000,
limit: 1,
maxClients: 50,
enabled: true,
cleanupInterval: 60000,
initialize(config) {
if (!config.rate_limit) {
log('Rate limit configuration missing, using defaults', 'WARN', 'API');
return;
}
this.windowSize = config.rate_limit.window_size;
this.limit = config.rate_limit.requests_per_second;
this.maxClients = config.rate_limit.max_clients;
this.enabled = config.rate_limit.enabled;
this.cleanupInterval = config.rate_limit.cleanup_interval;
setInterval(() => this.cleanup(), this.cleanupInterval);
log(`Rate limiter initialized: ${this.limit} req/s, window: ${this.windowSize}ms, max clients: ${this.maxClients}`, 'INFO', 'API');
},
isAllowed(clientIP) {
if (!this.enabled) return true;
const now = Date.now();
if (this.windows.size > this.maxClients) {
this.cleanup();
}
let requests = this.windows.get(clientIP);
if (!requests) {
if (this.windows.size >= this.maxClients) {
log(`Max clients limit reached (${this.maxClients}), rejecting new client: ${clientIP}`, 'WARN', 'API');
return false;
}
requests = [];
}
const windowStart = now - this.windowSize;
requests = requests.filter(time => time > windowStart);
if (requests.length >= this.limit) {
this.windows.set(clientIP, requests);
return false;
}
requests.push(now);
this.windows.set(clientIP, requests);
return true;
},
cleanup() {
const now = Date.now();
const windowStart = now - this.windowSize;
let cleaned = 0;
for (const [ip, requests] of this.windows.entries()) {
const validRequests = requests.filter(time => time > windowStart);
if (validRequests.length === 0) {
this.windows.delete(ip);
cleaned++;
} else {
this.windows.set(ip, validRequests);
}
}
if (this.windows.size > this.maxClients) {
const entries = [...this.windows.entries()]
.sort((a, b) => Math.max(...a[1]) - Math.max(...b[1]));
const toDelete = entries.slice(0, entries.length - this.maxClients);
toDelete.forEach(([ip]) => this.windows.delete(ip));
cleaned += toDelete.length;
}
if (cleaned > 0) {
log(`Cleaned up ${cleaned} rate limit records (current clients: ${this.windows.size})`, 'DEBUG', 'API');
}
},
getStatus(clientIP) {
const requests = this.windows.get(clientIP);
if (!requests) return null;
const now = Date.now();
const windowStart = now - this.windowSize;
const activeRequests = requests.filter(time => time > windowStart);
return {
requests: activeRequests.length,
window: this.windowSize,
limit: this.limit,
remaining: Math.max(0, this.limit - activeRequests.length),
reset: Math.ceil((Math.max(...activeRequests) + this.windowSize - now) / 1000)
};
}
};
async function cachedPathExists(filePath) {
const now = Date.now();
const cached = fileExistsCache.get(filePath);
if (cached && (now - cached.timestamp) < FILE_CACHE_TTL) {
return cached.exists;
}
const exists = await fs.pathExists(filePath);
fileExistsCache.set(filePath, { exists, timestamp: now });
if (fileExistsCache.size > MAX_FILE_CACHE_SIZE) {
cleanupFileCache();
}
return exists;
}
function cleanupFileCache() {
const now = Date.now();
let cleanedCount = 0;
for (const [filePath, data] of fileExistsCache.entries()) {
if (now - data.timestamp > FILE_CACHE_TTL) {
fileExistsCache.delete(filePath);
cleanedCount++;
}
}
if (cleanedCount > 0) {
log(`Cleaned up ${cleanedCount} expired file cache entries`, 'DEBUG', 'API');
}
}
async function loadImageList() {
try {
log('Starting to load image list...', 'DEBUG', 'API');
const [newImageList, newImageDetails] = await Promise.all([
safeReadJson(path.join(__dirname, 'list.json'), {}),
safeReadJson(path.join(__dirname, 'images-details.json'), [])
]);
if (newImageDetails.length === 0 && Object.keys(newImageList).length > 0) {
newImageDetails.push(...convertListToDetails(newImageList));
log(`Converted ${newImageDetails.length} images from list.json format`, 'WARN', 'API');
}
imageList = newImageList;
imageDetails = newImageDetails;
fileExistsCache.clear();
log(`Image list loaded: ${Object.keys(imageList).length} directories, ${imageDetails.length} total images`, 'INFO', 'API');
} catch (err) {
log(`Failed to load image list: ${err.message}`, 'ERROR', 'API');
log(`Error stack: ${err.stack}`, 'DEBUG', 'API');
}
}
function convertListToDetails(listData) {
const details = [];
const baseImagePath = path.resolve(config.paths.images);
for (const [directory, files] of Object.entries(listData)) {
if (!files || typeof files !== 'object') continue;
for (const [filename, uploadtime] of Object.entries(files)) {
try {
const dirPath = directory === '_root' ? '' : directory;
const fullPath = path.join(baseImagePath, dirPath, filename);
const relativePath = path.join(dirPath, filename).replace(/\\/g, '/');
details.push({
name: filename,
size: 0,
uploadtime,
path: relativePath,
_fullPath: fullPath,
_directory: directory,
_extension: path.extname(filename).toLowerCase(),
_mimeType: mimeTypes.lookup(fullPath) || 'application/octet-stream'
});
} catch (itemErr) {
log(`Error processing item ${filename} in ${directory}: ${itemErr.message}`, 'WARN', 'API');
}
}
}
return details;
}
function setCorsHeaders(res) {
if (config.server.cors.enabled) {
res.setHeader('Access-Control-Allow-Origin', config.server.cors.origins);
res.setHeader('Access-Control-Allow-Methods', config.server.cors.methods);
res.setHeader('Access-Control-Allow-Headers', config.server.cors.headers);
res.setHeader('Access-Control-Allow-Credentials', 'true');
}
}
function getRandomImage(directory = null) {
if (imageDetails.length === 0) {
log('No images available for random selection', 'DEBUG', 'API');
return null;
}
let filtered = imageDetails;
if (directory) {
filtered = imageDetails.filter(img => {
if (directory === '_root') {
return img._directory === '_root';
} else {
return img._directory === directory || img.path.startsWith(directory + '/');
}
});
if (filtered.length === 0) {
log(`No images found in directory: ${directory}`, 'DEBUG', 'API');
return null;
}
}
const randomIndex = Math.floor(Math.random() * filtered.length);
const selectedImage = filtered[randomIndex];
log(`Random image selected: ${selectedImage.name} from ${filtered.length} candidates`, 'DEBUG', 'API');
return selectedImage;
}
function findSpecificImage(directory, filename) {
const targetPath = path.join(directory, filename).replace(/\\/g, '/');
const found = imageDetails.find(img => {
if (directory === '_root') {
return img._directory === '_root' && img.name === filename;
} else {
return img.path === targetPath;
}
});
if (found) {
log(`Specific image found: ${found.name} at ${found.path}`, 'DEBUG', 'API');
} else {
log(`Specific image not found: ${targetPath}`, 'DEBUG', 'API');
}
return found;
}
function isRandomRequest(parts) {
return parts.length < 2;
}
function generateCacheKey(req, parts) {
if (isRandomRequest(parts)) {
return null;
}
const base = `api:${req.path}`;
const suffix = req.query.json === '1' ? ':json' : ':file';
return base + suffix;
}
function getClientIP(req) {
return req.headers['x-forwarded-for']?.split(',')[0] ||
req.headers['x-real-ip'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.ip ||
'unknown';
}
function getNotFoundMessage(parts) {
if (parts.length === 0) {
return 'No images available';
} else if (parts.length === 1) {
return `No images found in directory: ${parts[0]}`;
} else {
return `Image not found: ${parts.join('/')}`;
}
}
async function handleApiRequest(req, res) {
const startTime = Date.now();
setCorsHeaders(res);
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
const clientIP = getClientIP(req);
if (!requestLimiter.isAllowed(clientIP)) {
const status = requestLimiter.getStatus(clientIP);
log(`Rate limit exceeded for client: ${clientIP}`, 'WARN', 'API', req);
return res.status(429).json({
error: 'Too Many Requests',
message: 'Rate limit exceeded. Please slow down your requests.',
retryAfter: status ? status.reset : Math.ceil(requestLimiter.windowSize / 1000),
limit: requestLimiter.limit,
window: requestLimiter.windowSize / 1000,
remaining: status ? status.remaining : 0,
clientIP: clientIP
});
}
const isJson = req.query.json === '1';
const parts = req.path.split('/').filter(p => p && p.trim());
parts.shift(); // 移除 'api'
const isRandom = isRandomRequest(parts);
const cacheKey = generateCacheKey(req, parts);
const cleanPath = req.originalUrl.split('?')[0];
try {
if (!isRandom && cacheKey) {
const cached = await cacheManager.get(cacheKey);
if (cached) {
log(`Cache hit: ${cacheKey} (${Date.now() - startTime}ms)`, 'DEBUG', 'API', req);
if (isJson) {
return res.json(cached);
}
const filePath = cached._fullPath || cached.fullPath || path.resolve(cached.path);
if (await cachedPathExists(filePath)) {
res.setHeader('Content-Type', mimeTypes.lookup(filePath) || 'image/jpeg');
res.setHeader('Cache-Control', 'public, max-age=3600');
res.setHeader('X-Cache', 'HIT');
return res.sendFile(filePath);
} else {
await cacheManager.del(cacheKey);
log(`Cleared stale cache for missing file: ${cacheKey}`, 'WARN', 'API', req);
}
}
}
let selectedImage;
if (parts.length === 0) {
selectedImage = getRandomImage();
log(`Random selection from all images (${Date.now() - startTime}ms)`, 'DEBUG', 'API', req);
} else if (parts.length === 1) {
selectedImage = getRandomImage(parts[0]);
log(`Random selection from directory: ${parts[0]} (${Date.now() - startTime}ms)`, 'DEBUG', 'API', req);
} else {
selectedImage = findSpecificImage(parts[0], parts.slice(1).join('/'));
if (selectedImage) {
const exists = await cachedPathExists(selectedImage._fullPath);
if (!exists) {
log(`Specific image file not found: ${selectedImage._fullPath}`, 'WARN', 'API', req);
selectedImage = null;
} else {
log(`Specific image found: ${parts.join('/')} (${Date.now() - startTime}ms)`, 'DEBUG', 'API', req);
}
}
}
if (!selectedImage) {
const processingTime = Date.now() - startTime;
log(`Image not found (${processingTime}ms)`, 'WARN', 'API', req);
return res.status(404).json({
error: 'Image not found',
path: cleanPath,
message: getNotFoundMessage(parts),
processingTime
});
}
const imagePath = selectedImage._fullPath;
const webPath = '/api/' + selectedImage.path;
const imageInfo = {
name: selectedImage.name,
size: selectedImage.size,
uploadtime: selectedImage.uploadtime,
path: webPath,
processingTime: Date.now() - startTime
};
if (!isRandom && cacheKey) {
const cacheData = {
...imageInfo,
_fullPath: imagePath,
cached_at: require('./utils').getCurrentTimestamp()
};
try {
await cacheManager.set(cacheKey, cacheData);
log(`Cached: ${cacheKey}`, 'DEBUG', 'API', req);
} catch (cacheErr) {
log(`Failed to cache ${cacheKey}: ${cacheErr.message}`, 'WARN', 'API', req);
}
}
if (isJson) {
log(`JSON response returned (${imageInfo.processingTime}ms)`, 'DEBUG', 'API', req);
return res.json(imageInfo);
}
const mimeType = selectedImage._mimeType || mimeTypes.lookup(imagePath) || 'image/jpeg';
res.setHeader('Content-Type', mimeType);
res.setHeader('Cache-Control', isRandom ?
'no-cache, no-store, must-revalidate' :
'public, max-age=3600'
);
res.setHeader('X-Image-Name', selectedImage.name);
res.setHeader('X-Image-Path', imageInfo.path);
res.setHeader('X-Is-Random', isRandom.toString());
res.setHeader('X-Processing-Time', imageInfo.processingTime.toString());
res.setHeader('X-Cache', 'MISS');
log(`File response sent: ${selectedImage.name} (${imageInfo.processingTime}ms)`, 'DEBUG', 'API', req);
return res.sendFile(path.resolve(imagePath));
} catch (err) {
const processingTime = Date.now() - startTime;
log(`API error: ${err.message} (${processingTime}ms)`, 'ERROR', 'API', req);
log(`Error stack: ${err.stack}`, 'DEBUG', 'API');
return res.status(500).json({
error: 'Internal server error',
path: cleanPath,
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong',
processingTime
});
}
}
async function start(appConfig, cacheMgr) {
try {
config = appConfig;
cacheManager = cacheMgr;
process.env.WORKER_ID = process.env.WORKER_ID ||
(require('cluster').worker ? require('cluster').worker.id : '1');
await loadImageList();
// 初始化限流器
requestLimiter.initialize(config);
// 定时重新加载图片列表(5分钟)
const reloadInterval = setInterval(async () => {
try {
await loadImageList();
} catch (err) {
log(`Scheduled reload failed: ${err.message}`, 'ERROR', 'API');
}
}, 300000);
// 定时维护清理(10分钟)
const maintenanceInterval = setInterval(cleanupFileCache, 600000);
// 优雅关闭时清理定时器
const originalExit = process.exit;
process.exit = function(code) {
clearInterval(reloadInterval);
clearInterval(maintenanceInterval);
originalExit.call(process, code);
};
log(`API started with ${imageDetails.length} images`, 'INFO', 'API');
log('Cache policy: random ⛔, specific ✅', 'INFO', 'API');
log(`File exists cache TTL: ${FILE_CACHE_TTL/1000}s, Request limit: ${requestLimiter.limit}/s`, 'INFO', 'API');
return {
handleApiRequest,
loadImageList,
getApiStats: () => ({
images: {
total: imageDetails.length,
directories: Object.keys(imageList).length
},
cache: cacheManager.getStatus(),
rateLimiter: {
enabled: requestLimiter.enabled,
limit: requestLimiter.limit,
windowSize: requestLimiter.windowSize,
currentClients: requestLimiter.windows.size
},
fileCache: {
size: fileExistsCache.size,
ttl: FILE_CACHE_TTL / 1000 + 's'
}
})
};
} catch (err) {
log(`Failed to start API: ${err.message}`, 'ERROR', 'API');
throw err;
}
}
module.exports = { start };