diff --git a/package-lock.json b/package-lock.json index a5a93b26..24b62c82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4877,6 +4877,12 @@ "integrity": "sha512-F5DIZ36YVLE+PN+Zwws4kJogq47hNgX3Nx6WyDJ3kcplxyke3XIzB8uK5n/Lpm1HBsbGzd6nmGehL8cPekP+Tg==", "dev": true }, + "@types/wicg-file-system-access": { + "version": "2020.9.4", + "resolved": "https://registry.npmjs.org/@types/wicg-file-system-access/-/wicg-file-system-access-2020.9.4.tgz", + "integrity": "sha512-o43jUljwP0ZrQ927mPjGdJaBMfS12nf3VPj6Z52fMucxILrSs8tnfLbMDSn6cP3hrrLChc3SYneeEvecknNVtA==", + "dev": true + }, "@vitejs/plugin-react": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-1.0.1.tgz", diff --git a/package.json b/package.json index 60bc8cdd..28e1c836 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "@danmarshall/deckgl-typings": "^4.3.10", "@types/node": "^14.14.5", "@types/react-dom": "^17.0.0", + "@types/wicg-file-system-access": "^2020.9.4", "@vitejs/plugin-react": "^1.0.1", "prettier": "^2.2.0", "typescript": "^4.4.3", diff --git a/src/fsstore.ts b/src/fsstore.ts new file mode 100644 index 00000000..824553c6 --- /dev/null +++ b/src/fsstore.ts @@ -0,0 +1,45 @@ +import type { AsyncStore } from 'zarr/types/storage/types'; +import { KeyError } from 'zarr'; + +async function getFileHandle(root: FileSystemDirectoryHandle, key: string) { + const dirs = key.split('/'); + const fname = dirs.pop()!; + for (const dir of dirs) { + root = await root.getDirectoryHandle(dir); + } + return root.getFileHandle(fname); +} + +export class FileSystemStore implements AsyncStore { + constructor(public root: FileSystemDirectoryHandle) {} + + static async fromDirectoryPicker() { + return new FileSystemStore(await window.showDirectoryPicker()); + } + + async getItem(key: string) { + const fileHandle = await getFileHandle(this.root, key).catch((_) => { + throw new KeyError(key); + }); + const file = await fileHandle.getFile(); + return file.arrayBuffer(); + } + + containsItem(key: string) { + return getFileHandle(this.root, key) + .then((_) => true) + .catch((_) => false); + } + + keys() { + return Promise.resolve([]); + } + + deleteItem(key: string): never { + throw new Error('deleteItem not implemented'); + } + + setItem(key: string, value: ArrayBuffer): never { + throw new Error('setItem not implemented'); + } +} diff --git a/tsconfig.json b/tsconfig.json index 731b86d7..7da5d78f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,8 @@ "zarr": ["node_modules/zarr"] }, "types": [ - "vite-plugin-pwa/client" + "vite-plugin-pwa/client", + "@types/wicg-file-system-access" ] } }