-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathimage-store.js
More file actions
333 lines (299 loc) · 10.2 KB
/
image-store.js
File metadata and controls
333 lines (299 loc) · 10.2 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
/**
* 图片存储管理控制器
*
* 功能:提供图片的上传、获取、删除、更新等RESTful API接口
* 支持base64格式图片的内存存储和管理,包括过期清理和内存监控
*
* @author drpy
* @version 1.0.0
*/
import {imageManager} from '../utils/imageManager.js'
import {validateBasicAuth} from '../utils/api_validate.js'
/**
* Fastify插件导出
* 注册图片管理相关的路由和处理器
*
* @param {Object} fastify - Fastify实例
* @param {Object} options - 插件配置选项
* @param {Function} done - 插件注册完成回调
*/
export default (fastify, options, done) => {
/**
* 图片上传接口
* POST /image/upload
*
* 功能:接收base64格式的图片数据并存储到内存中
* 需要基础认证,支持图片大小限制
*/
fastify.post('/image/upload', {
preHandler: validateBasicAuth, // 需要基础认证
schema: {
body: {
type: 'object',
required: ['imageId', 'base64Data'],
properties: {
imageId: {type: 'string', minLength: 1, maxLength: 100}, // 图片唯一标识
base64Data: {type: 'string', minLength: 1} // base64编码的图片数据
}
}
}
}, async (request, reply) => {
try {
const {imageId, base64Data} = request.body;
// 检查图片大小限制 (默认500KB)
const maxSize = options.MAX_IMAGE_SIZE || 0.5 * 1024 * 1024;
const imageSize = Buffer.byteLength(base64Data, 'utf8');
if (imageSize > maxSize) {
return reply.status(400).send({
success: false,
message: `图片大小超过限制 (${(maxSize / 1024 / 1024).toFixed(1)}MB)`
});
}
// 可以不检查,避免很多图片导致的浪费
// 检查是否已存在
// if (imageManager.getImage(imageId)) {
// return reply.status(409).send({
// success: false,
// message: '图片ID已存在,请使用不同的ID或先删除现有图片'
// });
// }
// 存储图片到内存
const result = imageManager.storeImage(imageId, base64Data);
reply.send({
success: true,
message: '图片上传成功',
data: result
});
} catch (error) {
reply.status(400).send({
success: false,
message: error.message
});
}
});
/**
* 获取图片接口
* GET /image/:imageId
*
* 功能:根据图片ID获取图片数据,返回二进制图片内容
* 支持浏览器缓存和适当的HTTP头设置
*/
fastify.get('/image/:imageId', {
schema: {
params: {
type: 'object',
properties: {
imageId: {type: 'string'} // 图片唯一标识
},
required: ['imageId']
}
}
}, async (request, reply) => {
try {
const {imageId} = request.params;
const imageInfo = imageManager.getImage(imageId);
// 检查图片是否存在
if (!imageInfo) {
return reply.status(404).send({
success: false,
message: '图片不存在'
});
}
// 解析base64数据为二进制缓冲区
const {mimeType, buffer} = imageManager.parseBase64ToBuffer(imageInfo.data);
// 设置响应头
reply.header('Content-Type', `image/${mimeType}`); // 设置MIME类型
reply.header('Content-Length', buffer.length); // 设置内容长度
reply.header('Cache-Control', 'public, max-age=3600'); // 缓存1小时
reply.header('Last-Modified', new Date(imageInfo.timestamp).toUTCString()); // 最后修改时间
// 返回图片数据
reply.send(buffer);
} catch (error) {
reply.status(500).send({
success: false,
message: '服务器错误: ' + error.message
});
}
});
/**
* 获取图片列表接口
* GET /image/list
*
* 功能:获取所有已存储图片的列表信息
* 返回图片ID、大小、时间戳等元数据
*/
fastify.get('/image/list', async (request, reply) => {
try {
const images = imageManager.getAllImages();
reply.send({
success: true,
data: {
images: images, // 图片列表
total: images.length // 图片总数
}
});
} catch (error) {
reply.status(500).send({
success: false,
message: '服务器错误: ' + error.message
});
}
});
/**
* 删除图片接口
* DELETE /image/:imageId
*
* 功能:根据图片ID删除指定图片
* 需要基础认证,支持释放内存空间
*/
fastify.delete('/image/:imageId', {
preHandler: validateBasicAuth, // 需要基础认证
schema: {
params: {
type: 'object',
properties: {
imageId: {type: 'string'} // 图片唯一标识
},
required: ['imageId']
}
}
}, async (request, reply) => {
try {
const {imageId} = request.params;
// 尝试删除图片
if (imageManager.deleteImage(imageId)) {
reply.send({
success: true,
message: '图片删除成功'
});
} else {
reply.status(404).send({
success: false,
message: '图片不存在'
});
}
} catch (error) {
reply.status(500).send({
success: false,
message: '服务器错误: ' + error.message
});
}
});
/**
* 获取内存使用情况接口
* GET /image/memory
*
* 功能:获取图片存储的内存使用统计信息
* 包括图片数量、总内存占用等信息
*/
fastify.get('/image/memory', async (request, reply) => {
try {
const usage = imageManager.getMemoryUsage();
reply.send({
success: true,
data: usage
});
} catch (error) {
reply.status(500).send({
success: false,
message: '服务器错误: ' + error.message
});
}
});
/**
* 清理过期图片接口
* POST /image/cleanup
*
* 功能:清理超过指定时间的过期图片,释放内存空间
* 需要基础认证,支持自定义过期时间
*/
fastify.post('/image/cleanup', {
preHandler: validateBasicAuth, // 需要基础认证
schema: {
body: {
type: 'object',
properties: {
maxAge: {type: 'number', minimum: 1000} // 最大存活时间(毫秒),最小1秒
}
}
}
}, async (request, reply) => {
try {
const {maxAge} = request.body || {};
const cleanedCount = imageManager.cleanExpiredImages(maxAge);
reply.send({
success: true,
message: `清理完成,删除了 ${cleanedCount} 张过期图片`,
data: {
cleanedCount: cleanedCount, // 清理的图片数量
remainingCount: imageManager.getMemoryUsage().imageCount // 剩余图片数量
}
});
} catch (error) {
reply.status(500).send({
success: false,
message: '服务器错误: ' + error.message
});
}
});
/**
* 更新图片接口
* PUT /image/:imageId
*
* 功能:更新指定ID的图片数据
* 需要基础认证,支持图片大小限制
*/
fastify.put('/image/:imageId', {
preHandler: validateBasicAuth, // 需要基础认证
schema: {
params: {
type: 'object',
properties: {
imageId: {type: 'string'} // 图片唯一标识
},
required: ['imageId']
},
body: {
type: 'object',
required: ['base64Data'],
properties: {
base64Data: {type: 'string', minLength: 1} // base64编码的新图片数据
}
}
}
}, async (request, reply) => {
try {
const {imageId} = request.params;
const {base64Data} = request.body;
// 检查图片是否存在
if (!imageManager.getImage(imageId)) {
return reply.status(404).send({
success: false,
message: '图片不存在'
});
}
// 检查图片大小限制
const maxSize = options.MAX_IMAGE_SIZE || 0.5 * 1024 * 1024;
const imageSize = Buffer.byteLength(base64Data, 'utf8');
if (imageSize > maxSize) {
return reply.status(400).send({
success: false,
message: `图片大小超过限制 (${(maxSize / 1024 / 1024).toFixed(1)}MB)`
});
}
// 更新图片数据
const result = imageManager.storeImage(imageId, base64Data);
reply.send({
success: true,
message: '图片更新成功',
data: result
});
} catch (error) {
reply.status(400).send({
success: false,
message: error.message
});
}
});
done(); // 插件注册完成
};