forked from teralomaniac/miaomiaomiao
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathimageStorage.mjs
More file actions
66 lines (58 loc) · 1.83 KB
/
imageStorage.mjs
File metadata and controls
66 lines (58 loc) · 1.83 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
const imageStorage = new Map();
let imageCounter = 0;
/**
* 保存图片到内存中
* @param {string} base64Data base64 数据
* @param {string} mediaType 媒体类型 ("image/jpeg")
* @returns {{ imageId: string, mediaType: string }} 返回图片信息
*/
export function storeImage(base64Data, mediaType) {
const randomPart = Math.floor(Math.random() * 10000);
const imageId = `image_${Date.now()}_${imageCounter++}_${randomPart}`;
imageStorage.set(imageId, { imageId, base64Data, mediaType });
// console.log(`Image stored with ID: ${imageId}, Media Type: ${mediaType}`);
// 打印存储的 base64
// console.log(`Base64 Data for Image ID ${imageId}: ${base64Data.substring(0, 100)}...`);
return { imageId, mediaType };
}
/**
* 获取图片数据
* @param {string} imageId 图片 ID
* @returns {object|null} base64 数据和媒体类型
*/
export function getImage(imageId) {
return imageStorage.get(imageId) || null;
}
/**
* 获取最后一个图片
* @returns {object|null} base64 数据和媒体类型
*/
export function getLastImage() {
const lastKey = Array.from(imageStorage.keys()).pop();
return lastKey ? imageStorage.get(lastKey) : null;
}
/**
* 删除图片数据
* @param {string} imageId 图片 ID
*/
export function removeImage(imageId) {
imageStorage.delete(imageId);
}
/**
* 清空所有存储的图片
*/
export function clearAllImages() {
imageStorage.clear();
imageCounter = 0;
// console.log("All images have been cleared from memory.");
}
/**
* 打印所有存储的图片
*/
export function printAllImages() {
console.log('Current images stored in memory:');
imageStorage.forEach((value, key) => {
console.log(`Image ID: ${key}, Media Type: ${value.mediaType}`);
console.log(`Base64 Data: ${value.base64Data.substring(0, 100)}...`);
});
}