|
| 1 | +// Picking a Zarr store that lives in a folder on disk. |
| 2 | +// |
| 3 | +// A Zarr store is a directory tree, so opening one locally means read access to |
| 4 | +// a folder rather than to a file. Two platforms offer that, and this module |
| 5 | +// hides the difference behind `@geolibre/plugins`' `ZarrDirectoryReader`: |
| 6 | +// |
| 7 | +// - the desktop app, through the Tauri folder dialog, whose `recursive: true` |
| 8 | +// grant extends the `fs` scope to the picked folder's whole subtree; |
| 9 | +// - a Chromium browser, through the File System Access API's |
| 10 | +// `showDirectoryPicker()`, which hands back a handle the page may walk. |
| 11 | +// |
| 12 | +// Firefox and Safari implement neither, so `zarrDirectoryPickerSupported()` |
| 13 | +// reports the feature as unavailable there rather than opening a dialog that |
| 14 | +// cannot deliver. |
| 15 | + |
| 16 | +import { open } from "@tauri-apps/plugin-dialog"; |
| 17 | +import { readFile } from "@tauri-apps/plugin-fs"; |
| 18 | +import type { ZarrDirectoryReader } from "@geolibre/plugins"; |
| 19 | +import { isTauri } from "./is-tauri"; |
| 20 | + |
| 21 | +/** The subset of `FileSystemDirectoryHandle` this module uses. */ |
| 22 | +interface DirectoryHandle { |
| 23 | + name: string; |
| 24 | + kind: "directory"; |
| 25 | + getDirectoryHandle(name: string): Promise<DirectoryHandle>; |
| 26 | + getFileHandle(name: string): Promise<{ getFile(): Promise<Blob> }>; |
| 27 | +} |
| 28 | + |
| 29 | +interface DirectoryPickerWindow extends Window { |
| 30 | + showDirectoryPicker?: (options?: { mode?: "read" | "readwrite" }) => Promise<DirectoryHandle>; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Whether this build can open a Zarr store from a local folder: always on |
| 35 | + * desktop, and in a browser only where the File System Access API exists. |
| 36 | + * |
| 37 | + * @returns True when {@link pickZarrDirectory} can present a folder dialog. |
| 38 | + */ |
| 39 | +export function zarrDirectoryPickerSupported(): boolean { |
| 40 | + if (isTauri()) return true; |
| 41 | + return typeof (window as DirectoryPickerWindow).showDirectoryPicker === "function"; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Open the folder dialog and return read access to the chosen folder. |
| 46 | + * |
| 47 | + * @returns A reader over the picked folder, or null when the dialog was |
| 48 | + * dismissed or no folder picker is available. |
| 49 | + */ |
| 50 | +export async function pickZarrDirectory(): Promise<ZarrDirectoryReader | null> { |
| 51 | + if (isTauri()) { |
| 52 | + const selected = await open({ directory: true, multiple: false, recursive: true }); |
| 53 | + return typeof selected === "string" ? createTauriDirectoryReader(selected) : null; |
| 54 | + } |
| 55 | + |
| 56 | + const picker = (window as DirectoryPickerWindow).showDirectoryPicker; |
| 57 | + if (!picker) return null; |
| 58 | + try { |
| 59 | + const handle = await picker.call(window, { mode: "read" }); |
| 60 | + return handle ? createHandleDirectoryReader(handle) : null; |
| 61 | + } catch (error) { |
| 62 | + // Dismissing the dialog rejects with AbortError; that is not a failure. |
| 63 | + if (error instanceof DOMException && error.name === "AbortError") return null; |
| 64 | + throw error; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** The last segment of a local path, used as the store's display name. */ |
| 69 | +function directoryName(path: string): string { |
| 70 | + return ( |
| 71 | + path |
| 72 | + .replace(/[/\\]+$/, "") |
| 73 | + .split(/[/\\]/) |
| 74 | + .pop() || path |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +/** Read a picked folder through Tauri's `fs` plugin. */ |
| 79 | +function createTauriDirectoryReader(root: string): ZarrDirectoryReader { |
| 80 | + // Join with the folder's own separator style so a Windows path stays |
| 81 | + // all-backslash; store keys always arrive with `/`. |
| 82 | + const separator = root.includes("\\") ? "\\" : "/"; |
| 83 | + const base = /[/\\]$/.test(root) ? root.slice(0, -1) : root; |
| 84 | + const resolve = (path: string) => |
| 85 | + path ? `${base}${separator}${path.split("/").join(separator)}` : base; |
| 86 | + |
| 87 | + return { |
| 88 | + name: directoryName(root), |
| 89 | + async readFile(path: string) { |
| 90 | + try { |
| 91 | + return await readFile(resolve(path)); |
| 92 | + } catch { |
| 93 | + // A Zarr store writes no file for an all-fill chunk, so a missing key |
| 94 | + // is ordinary rather than an error worth surfacing. |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + }, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +/** Read a picked folder through a File System Access directory handle. */ |
| 102 | +function createHandleDirectoryReader(root: DirectoryHandle): ZarrDirectoryReader { |
| 103 | + const resolveDirectory = async (path: string): Promise<DirectoryHandle | null> => { |
| 104 | + let handle = root; |
| 105 | + for (const segment of path.split("/").filter(Boolean)) { |
| 106 | + handle = await handle.getDirectoryHandle(segment); |
| 107 | + } |
| 108 | + return handle; |
| 109 | + }; |
| 110 | + |
| 111 | + return { |
| 112 | + name: root.name || "zarr", |
| 113 | + async readFile(path: string) { |
| 114 | + const segments = path.split("/").filter(Boolean); |
| 115 | + const fileName = segments.pop(); |
| 116 | + if (!fileName) return undefined; |
| 117 | + try { |
| 118 | + const directory = await resolveDirectory(segments.join("/")); |
| 119 | + if (!directory) return undefined; |
| 120 | + const file = await directory.getFileHandle(fileName); |
| 121 | + return new Uint8Array(await (await file.getFile()).arrayBuffer()); |
| 122 | + } catch { |
| 123 | + // As above: an absent key is how a store expresses an empty chunk. |
| 124 | + return undefined; |
| 125 | + } |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
0 commit comments