forked from gbishop/OS-DPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
477 lines (440 loc) · 13.5 KB
/
db.js
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
import { openDB } from "idb/with-async-ittr";
import { zipSync, strToU8, unzipSync, strFromU8 } from "fflate";
import { fileSave } from "browser-fs-access";
class DB {
constructor() {
this.dbPromise = openDB("os-dpi", 4, {
upgrade(db, oldVersion, newVersion) {
if (oldVersion < 3) {
for (const name of ["store", "media", "saved", "url"]) {
try {
db.deleteObjectStore(name);
} catch (e) {}
}
} else if (oldVersion == 3) {
db.deleteObjectStore("images");
}
if (oldVersion < 3) {
let objectStore = db.createObjectStore("store", {
keyPath: "id",
autoIncrement: true,
});
objectStore.createIndex("by-name", "name");
objectStore.createIndex("by-name-type", ["name", "type"]);
}
if (newVersion >= 4) {
db.createObjectStore("media");
}
if (oldVersion < 3) {
// keep track of the name and ETag (if any) of designs that have been saved
let savedStore = db.createObjectStore("saved", {
keyPath: "name",
});
savedStore.createIndex("by-etag", "etag");
// track etags for urls
db.createObjectStore("url", {
keyPath: "url",
});
}
},
});
this.updateListeners = [];
this.designName = "";
this.fileName = "";
this.fileHandle = null;
this.fileVersion = 0.0;
this.fileUid = "";
}
/** set the name for the current design
* @param {string} name
*/
setDesignName(name) {
this.designName = name;
}
/** rename the design
* @param {string} newName
*/
async renameDesign(newName) {
const db = await this.dbPromise;
newName = await this.uniqueName(newName);
const tx = db.transaction(["store", "media", "saved"], "readwrite");
const index = tx.objectStore("store").index("by-name");
for await (const cursor of index.iterate(this.designName)) {
const record = { ...cursor.value };
record.name = newName;
cursor.update(record);
}
const mst = tx.objectStore("media");
for await (const cursor of mst.iterate()) {
if (cursor && cursor.key[0] == this.designName) {
const record = { ...cursor.value };
const key = cursor.key;
cursor.delete();
key[0] = newName;
mst.put(record, key);
}
}
const cursor = await tx.objectStore("saved").openCursor(this.designName);
if (cursor) {
const saved = cursor.value;
cursor.delete();
}
await tx.done;
this.fileHandle = null;
this.fileName = "";
this.notify({ action: "rename", name: this.designName, newName });
this.designName = newName;
}
/**
* return list of names of designs in the db
* @returns {Promise<string[]>}
*/
async names() {
const db = await this.dbPromise;
const index = db.transaction("store", "readonly").store.index("by-name");
const result = [];
for await (const cursor of index.iterate(null, "nextunique")) {
result.push(/** @type {string} */ (cursor.key));
}
return result;
}
/**
* return list of names of saved designs in the db
* @returns {Promise<string[]>}
*/
async saved() {
const db = await this.dbPromise;
const result = [];
for (const key of await db.getAllKeys("saved")) {
result.push(key.toString());
}
return result;
}
/**
* Create a unique name for new design
* @param {string} name - the desired name
* @returns {Promise<string>}
*/
async uniqueName(name = "new") {
// strip off any suffix
name = name.replace(/\.osdpi$|\.zip$/, "");
// strip any -number off the end of base
name = name.replace(/-\d+$/, "") || name;
// replace characters we don't want with _
name = name.replaceAll(/[^a-zA-Z0-9]/g, "_");
// replace multiple _ with one
name = name.replaceAll(/_+/g, "_");
// remove trailing _
name = name.replace(/_+$/, "");
// remove leading _
name = name.replace(/^_+/, "");
// if we're left with nothing the call it noname
name = name || "noname";
const allNames = await this.names();
if (allNames.indexOf(name) < 0) return name;
const base = name;
for (let i = 1; true; i++) {
const name = `${base}-${i}`;
if (allNames.indexOf(name) < 0) return name;
}
}
/** Return the most recent record for the type
* @param {string} type
* @param {any} defaultValue
* @returns {Promise<Object>}
*/
async read(type, defaultValue) {
const db = await this.dbPromise;
const index = db
.transaction("store", "readonly")
.store.index("by-name-type");
const cursor = await index.openCursor([this.designName, type], "prev");
return cursor?.value.data || defaultValue;
}
/** Add a new record
* @param {string} type
* @param {Object} data
*/
async write(type, data) {
const db = await this.dbPromise;
await db.put("store", { name: this.designName, type, data });
await db.delete("saved", this.designName);
this.notify({ action: "update", name: this.designName });
}
/** Undo by deleting the most recent record
* @param {string} type
*/
async undo(type) {
const db = await this.dbPromise;
const index = db
.transaction("store", "readwrite")
.store.index("by-name-type");
const cursor = await index.openCursor([this.designName, type], "prev");
if (cursor) await cursor.delete();
await db.delete("saved", this.designName);
this.notify({ action: "update", name: this.designName });
}
/** Read a design from a local file
* @param {import("browser-fs-access").FileWithHandle} file
*/
async readDesignFromFile(file) {
// keep the handle so we can save to it later
this.fileHandle = file.handle;
return this.readDesignFromBlob(file, file.name);
}
/** Read a design from a URL
* @param {string} url
*/
async readDesignFromURL(url) {
const db = await this.dbPromise;
// have we seen this url before?
const urlRecord = await db.get("url", url);
/** @type {HeadersInit} */
const headers = {}; // for the fetch
let name = "";
if (urlRecord) {
/** @type {string} */
const etag = urlRecord.etag;
// do we have any saved designs with this etag?
const savedKey = await db.getKeyFromIndex("saved", "by-etag", etag);
if (savedKey) {
// yes we have a previously saved design from this url
// set the headers to check if it has changed
headers["If-None-Match"] = etag;
name = savedKey.toString();
}
}
const response = await fetch(url, { headers });
if (response.status == 304) {
// we already have it
console.log("no need to fetch, we have it", name);
this.designName = name;
return;
}
if (!response.ok) {
throw new Error(`Fetching the URL (${url}) failed: ${response.status}`);
}
const etag = response.headers.get("ETag");
await db.put("url", { url, etag });
const urlParts = new URL(url, window.location.origin);
const pathParts = urlParts.pathname.split("/");
if (
pathParts.length > 0 &&
pathParts[pathParts.length - 1].endsWith(".osdpi")
) {
name = pathParts[pathParts.length - 1];
} else {
throw new Error(`Design files should have .osdpi suffix`);
}
const blob = await response.blob();
// parse the URL
return this.readDesignFromBlob(blob, name, etag);
}
/** Read a design from a zip file
* @param {Blob} blob
* @param {string} filename
*/
async readDesignFromBlob(blob, filename, etag = "none") {
const db = await this.dbPromise;
this.fileName = filename;
const zippedBuf = await readAsArrayBuffer(blob);
const zippedArray = new Uint8Array(zippedBuf);
const unzipped = unzipSync(zippedArray);
// normalize the fileName to make the design name
let name = this.fileName;
// make sure it is unique
name = await this.uniqueName(name);
this.designName = name;
for (const fname in unzipped) {
const mimetype = mime(fname) || "application/octet-stream";
if (mimetype == "application/json") {
const text = strFromU8(unzipped[fname]);
const obj = JSON.parse(text);
const type = fname.split(".")[0];
await this.write(type, obj);
} else if (mimetype.startsWith("image") || mimetype.startsWith("audio")) {
const blob = new Blob([unzipped[fname]], {
type: mimetype,
});
await db.put(
"media",
{
name: fname,
content: blob,
},
[name, fname]
);
}
}
await db.put("saved", { name: this.designName, etag });
this.notify({ action: "update", name: this.designName });
return;
}
/** Save a design into a zip file
*/
async saveDesign() {
const db = await this.dbPromise;
// collect the parts of the design
const layout = await this.read("layout");
const actions = await this.read("actions");
const content = await this.read("content");
const zipargs = {
"layout.json": strToU8(JSON.stringify(layout)),
"actions.json": strToU8(JSON.stringify(actions)),
"content.json": strToU8(JSON.stringify(content)),
};
const mediaKeys = (await db.getAllKeys("media")).filter((pair) =>
Object.values(pair).includes(this.designName)
);
// add the encoded image to the zipargs
for (const key of mediaKeys) {
const record = await db.get("media", key);
if (record) {
const contentBuf = await record.content.arrayBuffer();
const contentArray = new Uint8Array(contentBuf);
zipargs[key[1]] = contentArray;
}
}
// zip it
const zip = zipSync(zipargs);
// create a blob from the zipped result
const blob = new Blob([zip], { type: "application/octet-stream" });
const options = {
fileName: this.fileName || this.designName + ".osdpi",
extensions: [".osdpi", ".zip"],
id: "osdpi",
};
await fileSave(blob, options, this.fileHandle);
await db.put("saved", { name: this.designName });
}
/** Unload a design from the database
* @param {string} name - the name of the design to delete
*/
async unload(name) {
const db = await this.dbPromise;
const tx = db.transaction("store", "readwrite");
const index = tx.store.index("by-name");
for await (const cursor of index.iterate(name)) {
cursor.delete();
}
await tx.done;
await db.delete("saved", name);
}
/** Return an image from the database
* @param {string} name
* @returns {Promise<HTMLImageElement>}
*/
async getImage(name) {
const db = await this.dbPromise;
const record = await db.get("media", [this.designName, name]);
const img = new Image();
if (record) {
img.src = URL.createObjectURL(record.content);
}
img.title = record.name;
return img;
}
/** Return an audio file from the database
* @param {string} name
* @returns {Promise<HTMLAudioElement>}
*/
async getAudio(name) {
const db = await this.dbPromise;
const record = await db.get("media", [this.designName, name]);
const audio = new Audio();
if (record) {
audio.src = URL.createObjectURL(record.content);
}
return audio;
}
/** Return an image URL from the database
* @param {string} name
* @returns {Promise<string>}
*/
async getMediaURL(name) {
const db = await this.dbPromise;
const record = await db.get("media", [this.designName, name]);
if (record) return URL.createObjectURL(record.content);
else return "";
}
/** Add media to the database
* @param {Blob} blob
* @param {string} name
*/
async addMedia(blob, name) {
const db = await this.dbPromise;
return await db.put(
"media",
{
name: name,
content: blob,
},
[this.designName, name]
);
}
/** List media entries from a given store
* @returns {Promise<string[]>}
* */
async listMedia() {
const db = await this.dbPromise;
const keys = (await db.getAllKeys("media")).filter(
(key) => key[0] == this.designName //only show resources from this design
);
const result = [];
for (const key of keys) {
result.push(key[1].toString());
}
return result;
}
/** Listen for database update
* @param {(message: UpdateNotification) =>void} callback
*/
addUpdateListener(callback) {
this.updateListeners.push(callback);
}
/** Notify listeners of database update
* @param {UpdateNotification} message
*/
notify(message) {
for (const listener of this.updateListeners) {
listener(message);
}
}
}
export default new DB();
/** Convert a blob into an array buffer
* @param {Blob} blob */
function readAsArrayBuffer(blob) {
return new Promise((resolve) => {
const fr = new FileReader();
fr.onloadend = () => fr.result instanceof ArrayBuffer && resolve(fr.result);
fr.readAsArrayBuffer(blob);
});
}
const mimetypes = {
".json": "application/json",
".aac": "audio/aac",
".mp3": "audio/mpeg",
".mp4": "audio/mp4",
".oga": "audio/ogg",
".wav": "audio/wav",
".weba": "audio/webm",
".avif": "image/avif",
".bmp": "image/bmp",
".gif": "image/gif",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".svg": "image/svg+xml",
".tif": "image/tiff",
".tiff": "image/tiff",
".webp": "image/webp",
};
/** Map filenames to mimetypes for unpacking the zip file
* @param {string} fname
*/
function mime(fname) {
const extension = /\.[-a-zA-Z0-9]+$/.exec(fname);
if (!extension) return false;
return mimetypes[extension] || false;
}