-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathfs-helpers.js
148 lines (140 loc) · 4.25 KB
/
fs-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/* exported getFileHandle, getNewFileHandle, readFile, verifyPermission,
writeFile */
/**
* Open a handle to an existing file on the local file system.
*
* @return {!Promise<FileSystemFileHandle>} Handle to the existing file.
*/
function getFileHandle() {
// For Chrome 86 and later...
if ('showOpenFilePicker' in window) {
return window.showOpenFilePicker().then((handles) => handles[0]);
}
// For Chrome 85 and earlier...
return window.chooseFileSystemEntries();
}
/**
* Create a handle to a new (text) file on the local file system.
*
* @return {!Promise<FileSystemFileHandle>} Handle to the new file.
*/
function getNewFileHandle() {
// For Chrome 86 and later...
if ('showSaveFilePicker' in window) {
const opts = {
types: [{
description: 'Text file',
accept: {'text/plain': ['.txt']},
}],
};
return window.showSaveFilePicker(opts);
}
// For Chrome 85 and earlier...
const opts = {
type: 'save-file',
accepts: [{
description: 'Text file',
extensions: ['txt'],
mimeTypes: ['text/plain'],
}],
};
return window.chooseFileSystemEntries(opts);
}
/**
* Reads the raw text from a file.
*
* @param {File} file
* @return {!Promise<string>} A promise that resolves to the parsed string.
*/
function readFile(file) {
// If the new .text() reader is available, use it.
if (file.text) {
return file.text();
}
// Otherwise use the traditional file reading technique.
return _readFileLegacy(file);
}
/**
* Reads the raw text from a file.
*
* @private
* @param {File} file
* @return {Promise<string>} A promise that resolves to the parsed string.
*/
function _readFileLegacy(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.addEventListener('loadend', (e) => {
const text = e.srcElement.result;
resolve(text);
});
reader.readAsText(file);
});
}
/**
* Writes the contents to disk.
*
* @param {FileSystemFileHandle} fileHandle File handle to write to.
* @param {string} contents Contents to write.
*/
async function writeFile(fileHandle, contents) {
// Support for Chrome 82 and earlier.
if (fileHandle.createWriter) {
// Create a writer (request permission if necessary).
const writer = await fileHandle.createWriter();
// Write the full length of the contents
await writer.write(0, contents);
// Close the file and write the contents to disk
await writer.close();
return;
}
// For Chrome 83 and later.
// Create a FileSystemWritableFileStream to write to.
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(contents);
// Close the file and write the contents to disk.
await writable.close();
}
/**
* Verify the user has granted permission to read or write to the file, if
* permission hasn't been granted, request permission.
*
* @param {FileSystemFileHandle} fileHandle File handle to check.
* @param {boolean} withWrite True if write permission should be checked.
* @return {boolean} True if the user has granted read/write permission.
*/
async function verifyPermission(fileHandle, withWrite) {
const opts = {};
if (withWrite) {
opts.writable = true;
// For Chrome 86 and later...
opts.mode = 'readwrite';
}
// Check if we already have permission, if so, return true.
if (await fileHandle.queryPermission(opts) === 'granted') {
return true;
}
// Request permission to the file, if the user grants permission, return true.
if (await fileHandle.requestPermission(opts) === 'granted') {
return true;
}
// The user did nt grant permission, return false.
return false;
}