fileStorage: add file handles

This changes how files work. There is now an open function that
translates a path to a file handle (id). Then this handle is used
to perform other actions on the file. The file contents are moved
to a separate table so that the actual file storage is independent
of the database (e.g. in the future, we may use File Access API).

We store a hash of the file contents in the metadata file so we can
detect file changes without having to compare file contents.

We also separate the change and add actions.
This commit is contained in:
David Lechner
2022-04-01 13:41:10 -05:00
parent 75c3efc5ba
commit 1515e5ae4f
15 changed files with 462 additions and 255 deletions
+10
View File
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { sha256Digest } from './crypto';
test('sha256Digest', async () => {
await expect(sha256Digest('test')).resolves.toBe(
'9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
);
});
+11
View File
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
const encoder = new TextEncoder();
export async function sha256Digest(data: string): Promise<string> {
const hash = await window.crypto.subtle.digest('SHA-256', encoder.encode(data));
return Array.from(new Uint8Array(hash))
.map((n) => n.toString(16).padStart(2, '0'))
.join('');
}