Repository navigation
Expand file tree
/
Copy pathimage-storage.ts
More file actions
134 lines (112 loc) · 3.77 KB
/
Copy pathimage-storage.ts
File metadata and controls
134 lines (112 loc) · 3.77 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
export type StoredImages = {
images: File[];
descriptions: string[];
};
const databaseName = 'devnull';
const storeName = 'images';
let database: Promise<IDBDatabase | null> | undefined;
const open = (): Promise<IDBDatabase | null> => {
database ??= new Promise((resolve) => {
try {
const request = window.indexedDB.open(databaseName, 1);
request.onupgradeneeded = () =>
request.result.createObjectStore(storeName);
request.onsuccess = () => resolve(request.result);
request.onerror = () => resolve(null);
} catch {
resolve(null);
}
});
return database;
};
/**
* Runs the operation in its own transaction, and tells whether it was stored,
* so a full disk or a browser without IndexedDB only costs the images their persistence.
*/
const transact = async (
mode: IDBTransactionMode,
operate: (store: IDBObjectStore) => void,
): Promise<boolean> => {
const connection = await open();
return new Promise((resolve) => {
if (connection === null) {
resolve(false);
return;
}
try {
const transaction = connection.transaction(storeName, mode);
transaction.oncomplete = () => resolve(true);
transaction.onabort = () => resolve(false);
operate(transaction.objectStore(storeName));
} catch {
resolve(false);
}
});
};
export async function readStoredImages(): Promise<Map<string, StoredImages>> {
const stored = new Map<string, StoredImages>();
await transact('readonly', (store) => {
const request = store.openCursor();
request.onsuccess = () => {
const cursor = request.result;
if (cursor === null) {
return;
}
if (typeof cursor.key === 'string' && Array.isArray(cursor.value)) {
const entries = cursor.value.flatMap((value: unknown) => {
if (value instanceof File) {
return [{ file: value, description: '' }];
}
if (
typeof value === 'object' &&
value !== null &&
'file' in value &&
value.file instanceof File
) {
return [
{
file: value.file,
description:
'description' in value &&
typeof value.description === 'string'
? value.description
: '',
},
];
}
return [];
});
stored.set(cursor.key, {
images: entries.map((entry) => entry.file),
descriptions: entries.map((entry) => entry.description),
});
}
cursor.continue();
};
});
return stored;
}
export function writeStoredImages(
key: string,
images: File[],
descriptions: string[],
): void {
void transact('readwrite', (store) => {
if (images.length === 0) {
store.delete(key);
} else {
store.put(
images.map((file, index) => ({
file,
description: descriptions[index] ?? '',
})),
key,
);
}
});
}
export function deleteStoredImages(prefix: string): void {
void transact('readwrite', (store) =>
store.delete(IDBKeyRange.bound(prefix, `${prefix}`)),
);
}