Skip to content

best way to save what you love

web/storage: more stringent opfs check

jjd6e2f3cb12ec
diff --git a/web/src/lib/storage/index.ts b/web/src/lib/storage/index.ts
index 8687eb3c..0d2c3b0d 100644
--- a/web/src/lib/storage/index.ts
+++ b/web/src/lib/storage/index.ts
@@ -2,12 +2,12 @@ import type { AbstractStorage } from "./storage";
import { MemoryStorage } from "./memory";
import { OPFSStorage } from "./opfs";
-export function init(expectedSize?: number): Promise<AbstractStorage> {
- if (OPFSStorage.isAvailable()) {
+export async function init(expectedSize?: number): Promise<AbstractStorage> {
+ if (await OPFSStorage.isAvailable()) {
return OPFSStorage.init();
}
- if (MemoryStorage.isAvailable()) {
+ if (await MemoryStorage.isAvailable()) {
return MemoryStorage.init(expectedSize || 0);
}
diff --git a/web/src/lib/storage/memory.ts b/web/src/lib/storage/memory.ts
index 2652c8cb..45bebc3b 100644
--- a/web/src/lib/storage/memory.ts
+++ b/web/src/lib/storage/memory.ts
@@ -85,7 +85,7 @@ export class MemoryStorage extends AbstractStorage {
this.#chunks = [];
}
- static isAvailable() {
+ static async isAvailable() {
return true;
}
}
diff --git a/web/src/lib/storage/opfs.ts b/web/src/lib/storage/opfs.ts
index 484ae174..96a1e1f8 100644
--- a/web/src/lib/storage/opfs.ts
+++ b/web/src/lib/storage/opfs.ts
@@ -7,6 +7,8 @@ export class OPFSStorage extends AbstractStorage {
#handle;
#io;
+ static #isAvailable?: boolean;
+
constructor(root: FileSystemDirectoryHandle, handle: FileSystemFileHandle, reader: FileSystemSyncAccessHandle) {
super();
this.#root = root;
@@ -38,16 +40,33 @@ export class OPFSStorage extends AbstractStorage {
await this.#root.removeEntry(this.#handle.name);
}
- static isAvailable() {
+ static async #computeIsAvailable() {
if (typeof navigator === 'undefined')
return false;
- return 'storage' in navigator && 'getDirectory' in navigator.storage;
+ if ('storage' in navigator && 'getDirectory' in navigator.storage) {
+ try {
+ await navigator.storage.getDirectory();
+ return true;
+ } catch {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ static async isAvailable() {
+ if (this.#isAvailable === undefined) {
+ this.#isAvailable = await this.#computeIsAvailable();
+ }
+
+ return this.#isAvailable;
}
}
export const removeFromFileStorage = async (filename: string) => {
- if (OPFSStorage.isAvailable()) {
+ if (await OPFSStorage.isAvailable()) {
const root = await navigator.storage.getDirectory();
try {
@@ -60,7 +79,7 @@ export const removeFromFileStorage = async (filename: string) => {
}
export const clearFileStorage = async () => {
- if (OPFSStorage.isAvailable()) {
+ if (await OPFSStorage.isAvailable()) {
const root = await navigator.storage.getDirectory();
try {
await root.removeEntry(COBALT_PROCESSING_DIR, { recursive: true });
diff --git a/web/src/lib/storage/storage.ts b/web/src/lib/storage/storage.ts
index 5b50eceb..5ffc71db 100644
--- a/web/src/lib/storage/storage.ts
+++ b/web/src/lib/storage/storage.ts
@@ -3,7 +3,7 @@ export abstract class AbstractStorage {
throw "init() call on abstract implementation";
}
- static isAvailable(): boolean {
+ static async isAvailable(): Promise<boolean> {
return false;
}