Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getDirectory() and other assorted changes related to OPFS #22738

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions files/en-us/web/api/file_system_access_api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,46 @@ This API allows interaction with files on a user's local device, or on a user-ac

Most of the interaction with files and directories is accomplished through handles. A parent {{domxref('FileSystemHandle')}} class helps define two child classes: {{domxref('FileSystemFileHandle')}} and {{domxref('FileSystemDirectoryHandle')}}, for files and directories respectively.

The handles represent a file or directory on the user's system. You can first gain access to them by showing the user a file or directory picker. The methods which allow this are {{domxref('window.showOpenFilePicker')}} and {{domxref('window.showDirectoryPicker')}}. Once these are called, the file picker presents itself and the user selects either a file or directory. Once this happens successfully, a handle is returned. You can also gain access to file handles via the {{domxref('DataTransferItem.getAsFileSystemHandle()')}} method of the {{domxref('HTML Drag and Drop API')}}.
The handles represent a file or directory on the user's system. You can first gain access to them by showing the user a file or directory picker using methods such as {{domxref('window.showOpenFilePicker()')}} and {{domxref('window.showDirectoryPicker()')}}. Once these are called, the file picker presents itself and the user selects either a file or directory. Once this happens successfully, a handle is returned.

The interface {{domxref('FileSystemSyncAccessHandle')}} is also available to define a high-performance handle for _synchronous_ read/write operations (the other handle types are asynchronous). The synchronous nature of this class brings performance advantages intended for use in contexts where asynchronous operations come with high overhead (e.g., [WebAssembly](/en-US/docs/WebAssembly)). Note that it is only usable inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API), for files within the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system). {{domxref('FileSystemSyncAccessHandle')}} is accessed via the {{domxref('FileSystemFileHandle.createSyncAccessHandle', 'createSyncAccessHandle()')}} method.
You can also gain access to file handles via:

- The {{domxref('DataTransferItem.getAsFileSystemHandle()')}} method of the {{domxref('HTML Drag and Drop API', 'HTML Drag and Drop API', '', 'nocode')}}.
- The [File Handling API](https://developer.chrome.com/en/articles/file-handling/).

Each handle provides its own functionality and there are a few differences depending on which one you are using (see the [interfaces](#interfaces) section for specific details). You then can access file data, or information (including children) of the directory selected. This API opens up potential functionality the web has been lacking. Still, security has been of utmost concern when designing the API, and access to file/directory data is disallowed unless the user specifically permits it.

> **Note:** The different exceptions that can be thrown when using the features of this API are listed on relevant pages as defined in the spec. However, the situation is made more complex by the interaction of the API and the underlying operating system. A proposal has been made to [list the error mappings in the spec](https://github.com/whatwg/fs/issues/57), which includes useful related information.

> **Note:** Objects based on {{domxref("FileSystemHandle")}} can also be serialized into an {{domxref("IndexedDB API", "IndexedDB", "", "nocode")}} database instance, or transferred via {{domxref("window.postMessage", "postMessage()")}}.

### Origin private file system

The [origin private file system (OPFS)](https://fs.spec.whatwg.org/#origin-private-file-system) is a storage endpoint private to the origin of the page, providing optional access to a special kind of file that is highly optimized for performance, for example, by offering in-place and exclusive write access to a file's content.

Storing data in the OPFS is similar to storing data in any other browser-provided storage mechanism that's private to the origin of the page (for example the {{domxref("IndexedDB API", "IndexedDB API", "", "nocode")}}). This means that files in the OPFS differ from files selected using a picker in the following ways:

- Permission prompts are not required to access files in the OPFS.
- Clearing data for the site deletes the OPFS.
- The OFPS is subject to browser quota restrictions.

Files can be manipulated inside the OPFS via a three-step process:

1. The {{domxref("StorageManager.getDirectory()")}} method returns a reference to a {{domxref("FileSystemDirectoryHandle")}} object allowing access to a directory and its contents — this represents the root of the OPFS.
2. The {{domxref("FileSystemDirectoryHandle.getFileHandle()")}} method is invoked to return a {{domxref('FileSystemFileHandle')}} object representing a handle to a specific file in the directory.
3. The {{domxref('FileSystemFileHandle.createSyncAccessHandle', 'createSyncAccessHandle()')}} method is invoked on that file handle, and returns a {{domxref('FileSystemSyncAccessHandle')}} object that can be used to read and write to the file. This is a high-performance handle for _synchronous_ read/write operations (the other handle types are asynchronous). The synchronous nature of this class brings performance advantages intended for use in contexts where asynchronous operations come with high overhead (for example, [WebAssembly](/en-US/docs/WebAssembly)). Note that it is only usable inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API).

While browsers typically implement this by persisting the contents of the OPFS to disk somewhere, it is not intended that the contents be easily user-accessible. While the browser might make it seem that there are files, they might be stored in a database or any other data structure. You cannot expect to find the created files matched one-to-one somewhere on the hard disk.

> **Note:** Writes performed using {{domxref('FileSystemSyncAccessHandle.write()')}} are in-place, meaning that changes are written to the actual underlying file at the same time as they are written to the writer. This is not the case with other writing mechanisms available in this API (e.g. {{domxref('FileSystemFileHandle.createWritable()')}}), where changes are not committed to disk until the writing stream is closed.

Each handle provides its own functionality and there are a few differences depending on which one you are using (see the [interfaces](#interfaces) section for specific details). You then can access file data, or information (including children) of the directory selected.
### Saving files

There is also "save" functionality:

- In the case of the asynchronous handles, use the {{domxref('FileSystemWritableFileStream')}} interface. Once the data you'd like to save is in a format of {{domxref('Blob')}}, {{jsxref("String")}} object, string literal or {{jsxref('ArrayBuffer', 'buffer')}}, you can open a stream and save the data to a file. This can be the existing file or a new file.
- In the case of the synchronous {{domxref('FileSystemSyncAccessHandle')}}, you write changes to a file using the {{domxref('FileSystemSyncAccessHandle.write', 'write()')}} method. You can optionally also call {{domxref('FileSystemSyncAccessHandle.flush', 'flush()')}} if you need the changes committed to disk at a specific time (otherwise you can leave the underlying operating system to handle this when it sees fit, which should be OK in most cases).

This API opens up potential functionality the web has been lacking. Still, security has been of utmost concern when designing the API, and access to file/directory data is disallowed unless the user specifically permits it.

> **Note:** The different exceptions that can be thrown when using the features of this API are listed on relevant pages as defined in the spec. However, the situation is made more complex by the interaction of the API and the underlying operating system. A proposal has been made to [list the error mappings in the spec](https://github.com/whatwg/fs/issues/57), which includes useful related information.

## Interfaces

- {{domxref("FileSystemHandle")}}
Expand All @@ -53,7 +76,7 @@ This API opens up potential functionality the web has been lacking. Still, secur
- {{domxref("FileSystemDirectoryHandle")}}
- : provides a handle to a file system directory.
- {{domxref("FileSystemSyncAccessHandle")}}
- : Provides a synchronous handle to a file system entry, which operates in-place on a single file on disk. The synchronous nature of the file reads and writes allows for higher performance for critical methods in contexts where asynchronous operations come with high overhead, e.g., [WebAssembly](/en-US/docs/WebAssembly). This class is only accessible inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system).
- : Provides a synchronous handle to a file system entry, which operates in-place on a single file on disk. The synchronous nature of the file reads and writes allows for higher performance for critical methods in contexts where asynchronous operations come with high overhead, e.g., [WebAssembly](/en-US/docs/WebAssembly). This class is only accessible inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](#origin_private_file_system).
- {{domxref("FileSystemWritableFileStream")}}
- : is a {{domxref('WritableStream')}} object with additional convenience methods, which operates on a single file on disk.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.entries
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`entries()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns an array of a given object's
own enumerable property `[key, value]` pairs, in the same order as that
provided by a {{jsxref('for...in')}} loop (the difference being that a for-in loop
provided by a [`for...in`](/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop (the difference being that a for-in loop
enumerates properties in the prototype chain as well).

## Syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.getDirectoryHandle
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`getDirectoryHandle()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.getFileHandle
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`getFileHandle()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns a
Expand Down
6 changes: 4 additions & 2 deletions files/en-us/web/api/filesystemdirectoryhandle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ tags:
browser-compat: api.FileSystemDirectoryHandle
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`FileSystemDirectoryHandle`** interface of the {{domxref('File System Access API')}} provides a handle to a file system directory. The interface is accessed via the {{domxref('window.showDirectoryPicker()')}} method.
The **`FileSystemDirectoryHandle`** interface of the {{domxref('File System Access API')}} provides a handle to a file system directory.

The interface can be accessed via the {{domxref('window.showDirectoryPicker()')}} and {{domxref('StorageManager.getDirectory()')}} methods.

{{InheritanceDiagram}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.keys
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`keys()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns a new _array iterator_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.removeEntry
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`removeEntry()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface attempts to remove an entry if the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.resolve
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`resolve()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns an {{jsxref('Array')}} of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:
browser-compat: api.FileSystemDirectoryHandle.values
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`values()`** method of the
{{domxref("FileSystemDirectoryHandle")}} interface returns a new _array iterator_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ browser-compat: api.FileSystemFileHandle.createSyncAccessHandle
The **`createSyncAccessHandle()`** method of the
{{domxref("FileSystemFileHandle")}} interface returns a {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandle')}} object
that can be used to synchronously read from and write to a file. The synchronous nature of this method brings performance advantages,
but it is only usable inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system).
but it is only usable inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system).

Creating a {{domxref('FileSystemSyncAccessHandle')}} takes an exclusive lock on the file associated with the file handle. This prevents the creation of further {{domxref('FileSystemSyncAccessHandle')}}s or {{domxref('FileSystemWritableFileStream')}}s for the file until the existing access handle is closed.

Expand All @@ -39,7 +39,7 @@ A {{jsxref('Promise')}} which resolves to a {{domxref('FileSystemSyncAccessHandl
### Exceptions

- `InvalidStateError` {{domxref("DOMException")}}
- : Thrown if the {{domxref('FileSystemSyncAccessHandle')}} object does not represent a file in the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system).
- : Thrown if the {{domxref('FileSystemSyncAccessHandle')}} object does not represent a file in the [origin private file system](/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system).
- `NoModificationAllowedError` {{domxref("DOMException")}}
- : Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
- `NotAllowedError` {{domxref("DOMException")}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tags:
browser-compat: api.FileSystemHandle.isSameEntry
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`isSameEntry()`** method of the
{{domxref("FileSystemHandle")}} interface compares two {{domxref("FileSystemHandle",
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/filesystemhandle/kind/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tags:
browser-compat: api.FileSystemHandle.kind
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`kind`** read-only property of the
{{domxref("FileSystemHandle")}} interface returns the type of entry. This is
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/filesystemhandle/name/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:
browser-compat: api.FileSystemHandle.name
---

{{securecontext_header}}{{DefaultAPISidebar("File System Access API")}}
{{securecontext_header}}{{APIRef("File System Access API")}}

The **`name`** read-only property of the
{{domxref("FileSystemHandle")}} interface returns the name of the entry represented by
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/filesystemsyncaccesshandle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ browser-compat: api.FileSystemSyncAccessHandle

The **`FileSystemSyncAccessHandle`** interface of the {{domxref("File System Access API", "File System Access API", "", "nocode")}} represents a synchronous handle to a file system entry. The synchronous nature of the file reads and writes allows for higher performance for critical methods in contexts where asynchronous operations come with high overhead, e.g., [WebAssembly](/en-US/docs/WebAssembly).

This class is only accessible inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](https://fs.spec.whatwg.org/#origin-private-file-system).
This class is only accessible inside dedicated [Web Workers](/en-US/docs/Web/API/Web_Workers_API) for files within the [origin private file system](/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system).

The interface is accessed through the {{domxref('FileSystemFileHandle.createSyncAccessHandle()')}} method.

Expand Down
Loading