WICG / file-system-access

Expose the file system on the user’s device, so Web apps can interoperate with the user’s native applications.
https://wicg.github.io/file-system-access/
Other
667 stars 66 forks source link

How to check if FileSystemDirectoryHandle points to an existing directory #352

Open jozefchutka opened 2 years ago

jozefchutka commented 2 years ago

Lets say I am building an IDE and want to have an option to open recently used folders which also persists after a page refresh. References to FileSystemDirectoryHandle-s, obtained by showDirectoryPicker() are stored in an indexeddb, but real filesystem directory might have been deleted (at some point in time after referencing). What is the proper API to check if it points to an existing directory?

I was thinking of using getDirectoryHandle but I do not have parent reference, nor does it accept empty name argument.

I can try-catch for await (const entry of directory.values()), but this has some downsides (requires permissions granted, consider page refresh) and for that reason its not convinient ux to run through a list of handles (multiple confirms). I wonder if there is something more appropriate.

For the moment I am using

async getError(handle:FileSystemDirectoryHandle) {
    if(await handle.queryPermission({mode:"readwrite"}) !== 'granted'
        && await handle.requestPermission({mode:"readwrite"}) !== 'granted')
        return ERROR_NO_PERMISSION;

    try {
        for await (const _ of handle.values())
            return;
        return;
    } catch(error) {
        return error instanceof DOMException && error.code == DOMException.NOT_FOUND_ERR 
            ? ERROR_DOES_NOT_EXIST
            : ERROR_UNKNOWN;
    }
}