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
|
import type { ThumbnailMeta } from '../flashair/types';
const DB_NAME = 'speedsync-cache';
const DB_VERSION = 1;
const STORE_NAME = 'images';
/** 30 days in milliseconds. */
const CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
export interface CachedImage {
/** The file path on the SD card, used as the primary key. */
readonly path: string;
/** 'thumbnail' or 'full' — separates the two image sizes. */
readonly kind: 'thumbnail' | 'full';
/** The image data. */
readonly blob: Blob;
/** EXIF metadata (only for thumbnails). */
readonly meta: ThumbnailMeta | undefined;
/** Unix timestamp (ms) when this entry was stored. */
readonly storedAt: number;
}
type CacheKey = `${'thumbnail' | 'full'}:${string}`;
function makeCacheKey(kind: 'thumbnail' | 'full', path: string): CacheKey {
return `${kind}:${path}`;
}
function openDb(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = () => {
const db = request.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME, { keyPath: 'key' });
}
};
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = () => {
reject(new Error(`Failed to open IndexedDB: ${request.error?.message ?? 'unknown error'}`));
};
});
}
interface StoredRecord {
readonly key: CacheKey;
readonly path: string;
readonly kind: 'thumbnail' | 'full';
readonly blob: Blob;
readonly meta: ThumbnailMeta | undefined;
readonly storedAt: number;
}
function isExpired(storedAt: number): boolean {
return Date.now() - storedAt > CACHE_TTL_MS;
}
export const imageCache = {
/**
* Retrieve a cached image. Returns undefined if not found or expired.
*/
async get(kind: 'thumbnail' | 'full', path: string): Promise<CachedImage | undefined> {
let db: IDBDatabase;
try {
db = await openDb();
} catch {
return undefined;
}
return new Promise((resolve) => {
const tx = db.transaction(STORE_NAME, 'readonly');
const store = tx.objectStore(STORE_NAME);
const key = makeCacheKey(kind, path);
const request = store.get(key);
request.onsuccess = () => {
const record = request.result as StoredRecord | undefined;
if (record === undefined || record === null) {
resolve(undefined);
return;
}
if (isExpired(record.storedAt)) {
// Expired — remove in background, return undefined
void imageCache.delete(kind, path);
resolve(undefined);
return;
}
resolve({
path: record.path,
kind: record.kind,
blob: record.blob,
meta: record.meta,
storedAt: record.storedAt,
});
};
request.onerror = () => {
resolve(undefined);
};
tx.oncomplete = () => {
db.close();
};
});
},
/**
* Store an image in the cache.
*/
async put(kind: 'thumbnail' | 'full', path: string, blob: Blob, meta?: ThumbnailMeta): Promise<void> {
let db: IDBDatabase;
try {
db = await openDb();
} catch {
return;
}
const record: StoredRecord = {
key: makeCacheKey(kind, path),
path,
kind,
blob,
meta,
storedAt: Date.now(),
};
return new Promise((resolve) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
store.put(record);
tx.oncomplete = () => {
db.close();
resolve();
};
tx.onerror = () => {
db.close();
resolve();
};
});
},
/**
* Delete a single entry from the cache.
*/
async delete(kind: 'thumbnail' | 'full', path: string): Promise<void> {
let db: IDBDatabase;
try {
db = await openDb();
} catch {
return;
}
return new Promise((resolve) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
store.delete(makeCacheKey(kind, path));
tx.oncomplete = () => {
db.close();
resolve();
};
tx.onerror = () => {
db.close();
resolve();
};
});
},
/**
* Remove all expired entries from the cache. Call periodically or on startup.
*/
async pruneExpired(): Promise<void> {
let db: IDBDatabase;
try {
db = await openDb();
} catch {
return;
}
return new Promise((resolve) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
const request = store.openCursor();
const keysToDelete: IDBValidKey[] = [];
request.onsuccess = () => {
const cursor = request.result;
if (cursor !== null) {
const record = cursor.value as StoredRecord;
if (isExpired(record.storedAt)) {
keysToDelete.push(cursor.key);
}
cursor.continue();
} else {
for (const key of keysToDelete) {
store.delete(key);
}
}
};
tx.oncomplete = () => {
db.close();
resolve();
};
tx.onerror = () => {
db.close();
resolve();
};
});
},
/**
* Clear the entire cache.
*/
async clear(): Promise<void> {
let db: IDBDatabase;
try {
db = await openDb();
} catch {
return;
}
return new Promise((resolve) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
store.clear();
tx.oncomplete = () => {
db.close();
resolve();
};
tx.onerror = () => {
db.close();
resolve();
};
});
},
} as const;
|