The queryPermission()
method of the FileSystemHandle
interface queries the current permission state of the current handle.
queryPermission(fileSystemHandlePermissionDescriptor)
PermissionStatus.state
which is one of 'granted'
, 'denied'
or 'prompt'
.
If this returns "prompt" the website will have to call requestPermission() before any operations on the handle can be done. If this returns "denied" any operations will reject. Usually handles returned by the local file system handle factories will initially return "granted" for their read permission state. However, other than through the user revoking permission, a handle retrieved from IndexedDB is also likely to return "prompt".
The following asynchronous function returns true if user has granted read or readwrite permissions to the file handle. Permission is requested if not.
async function verifyPermission(fileHandle, withWrite) {
const opts = {};
if (withWrite) {
opts.mode = "readwrite";
}
if ((await fileHandle.queryPermission(opts)) === "granted") {
return true;
}
if ((await fileHandle.requestPermission(opts)) === "granted") {
return true;
}
return false;
}