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
669 stars 66 forks source link

Allow us to directly construct the `FileSystemFileHandle` if we already know the path #301

Closed swyxio closed 3 years ago

swyxio commented 3 years ago

hi folks thanks for working on this great API!

right now the only way to get a FileSystemFileHandle is by using a showOpenFilePicker. however we may ALREADY know the filepath and just want to read and write to it without opening a picker. as far as I can tell there's no way to do that.

what i'd ideally want is something like:

let fileHandle = new FileSystemFileHandle(knownFilePath)

// // the below operations would Just Work™
// read
const fileData = await fileHandle.getFile();    
let contents = await fileData.text()

// write
const writable = await fileHandle.createWritable();
await writable.write(contents + 'epsilon');
await writable.close();

use case: https://github.com/sw-yx/svelte-filesystem-demo

mkruisselbrink commented 3 years ago

Allowing websites to request access to specific file paths is much more dangerous than only allowing websites access to file paths users explicitly picked, as such we're not currently planning to implement a feature like this.

jimmywarting commented 3 years ago

i think he kind of want a cd or recursive mkdir method on the FileSystemDirectoryHandle itself

Say that you already have access to the root directory and you have saved it into IndexeDB and you know for sure that you will also have access to all the subfolder of something that you have already given permission to.

So it would be something like

const handle = await showDirectoryPicker()
handle.getDirectoryHandle('./some/deep/subpath/within/the/handle')

I guess it would not be anything harmful about that. It's possible to create like a recursive function of it...

James-E-A commented 5 months ago

we may ALREADY know the filepath and just want to read and write to it without opening a picker.

  1. If you already have a handle open to a parent directory, use the methods on that handle to get the handle you want on the file you want.
  2. If you want to access a file on the user's real, OS-local filesystem, and you don't currently have a handle on that file or any parent directory, then you simply must call to one of the showXxxYyyPicker functions to get user consent and the corresponding handle.

If you're trying to do stuff across multiple browser restarts with some persistent file (for example loading and saving a user-created document, or seeking around in a custom database format), but it doesn't have to be literally exposed to other applications via the OS-local filesystem, why not just use the Origin Private Filesystem, which is (yet another) persistent local storage API that you can access without pestering the user?