-
-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FileSystem API to read/write files and use it (#3977)
* Add FileSystem API to read/write files and use it * Fixed issues as per review
- Loading branch information
1 parent
3fe659e
commit 4823832
Showing
11 changed files
with
340 additions
and
563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
class FileSystem { | ||
|
||
_createFile(fileHandle) { | ||
return { | ||
name: fileHandle.name, | ||
_fileHandle: fileHandle, | ||
}; | ||
} | ||
|
||
async pickSaveFile(suggestedName, description, extension) { | ||
const fileHandle = await window.showSaveFilePicker({ | ||
suggestedName: suggestedName, | ||
types: [{ | ||
description: description, | ||
accept: { | ||
"application/unknown": extension, | ||
}, | ||
}], | ||
}); | ||
|
||
const file = this._createFile(fileHandle); | ||
|
||
if (await this.verifyPermission(file, true)) { | ||
return file; | ||
} | ||
} | ||
|
||
async pickOpenFile(description, extension) { | ||
const fileHandle = await window.showOpenFilePicker({ | ||
multiple: false, | ||
types: [{ | ||
description: description, | ||
accept: { | ||
"application/unknown": extension, | ||
}, | ||
}], | ||
}); | ||
|
||
const file = this._createFile(fileHandle[0]); | ||
|
||
if (await this.verifyPermission(file, false)) { | ||
return file; | ||
} | ||
} | ||
|
||
async verifyPermission(file, withWrite) { | ||
const fileHandle = file._fileHandle; | ||
|
||
const opts = {}; | ||
opts.mode = withWrite ? "readwrite" : "read"; | ||
|
||
if ((await fileHandle.queryPermission(opts)) === "granted") { | ||
console.log("The user has %s permissions for the file: %s", opts.mode, fileHandle.name); | ||
return true; | ||
} | ||
|
||
if ((await fileHandle.requestPermission(opts)) === "granted") { | ||
console.log("Request %s permissions for the file: %s", opts.mode, fileHandle.name); | ||
return true; | ||
} | ||
|
||
console.error("The user has no permission for file: ", fileHandle.name); | ||
throw new Error("The user has no %s permission for file: %s", opts.mode, fileHandle.name); | ||
} | ||
|
||
async writeFile(file, contents) { | ||
const fileHandle = file._fileHandle; | ||
|
||
const writable = await fileHandle.createWritable(); | ||
await writable.write(contents); | ||
await writable.close(); | ||
} | ||
|
||
|
||
async readFile(file) { | ||
const fileHandle = file._fileHandle; | ||
|
||
const fileReader = await fileHandle.getFile(); | ||
return await fileReader.text(); | ||
} | ||
|
||
async readFileAsBlob(file) { | ||
const fileHandle = file._fileHandle; | ||
|
||
return await fileHandle.getFile(); | ||
} | ||
|
||
async openFile(file) { | ||
const fileHandle = file._fileHandle; | ||
|
||
return await fileHandle.createWritable(); | ||
} | ||
|
||
async writeChunck(writable, chunk) { | ||
return await writable.write(chunk); | ||
} | ||
|
||
async closeFile(writable) { | ||
return await writable.close(); | ||
} | ||
} | ||
|
||
export default new FileSystem(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.