Feature/attachment with native fs (#1078)

* feat: upload image with native fs

* improve: life of loading assets image file with nfs

Co-authored-by: Tienson Qin <tiensonqin@gmail.com>
This commit is contained in:
Charlie
2021-01-12 21:25:37 +08:00
committed by GitHub
parent 7a6ce189fc
commit 4b2dde2228
13 changed files with 209 additions and 92 deletions

View File

@@ -74,58 +74,58 @@ export var getSelectionText = function () {
}
}
return '';
return ''
}
// Modified from https://github.com/GoogleChromeLabs/browser-nativefs
// because shadow-cljs doesn't handle this babel transform
export var getFiles = async function (dirHandle, recursive, cb, path = dirHandle.name) {
const dirs = [];
const files = [];
const dirs = []
const files = []
for await (const entry of dirHandle.values()) {
const nestedPath = `${path}/${entry.name}`;
const nestedPath = `${path}/${entry.name}`
if (entry.kind === 'file') {
cb(nestedPath, entry);
cb(nestedPath, entry)
files.push(
entry.getFile().then((file) => {
Object.defineProperty(file, 'webkitRelativePath', {
configurable: true,
enumerable: true,
get: () => nestedPath,
});
Object.defineProperty(file, 'handle', {
configurable: true,
enumerable: true,
get: () => entry,
});
return file;
}
Object.defineProperty(file, 'webkitRelativePath', {
configurable: true,
enumerable: true,
get: () => nestedPath,
})
Object.defineProperty(file, 'handle', {
configurable: true,
enumerable: true,
get: () => entry,
})
return file
}
)
);
)
} else if (entry.kind === 'directory' && recursive) {
cb(nestedPath, entry);
dirs.push(getFiles(entry, recursive, cb, nestedPath));
cb(nestedPath, entry)
dirs.push(getFiles(entry, recursive, cb, nestedPath))
}
}
return [(await Promise.all(dirs)), (await Promise.all(files))];
};
return [(await Promise.all(dirs)), (await Promise.all(files))]
}
export var verifyPermission = async function (handle, readWrite) {
const options = {};
const options = {}
if (readWrite) {
options.mode = 'readwrite';
options.mode = 'readwrite'
}
// Check if permission was already granted.
if ((await handle.queryPermission(options)) === 'granted') {
return;
return
}
// Request permission. If the user grants permission, just return.
if ((await handle.requestPermission(options)) === 'granted') {
return;
return
}
// The user didn't grant permission, throw an error.
throw new Error("Permission is not granted");
throw new Error('Permission is not granted')
}
// NOTE: Need externs to prevent `options.recursive` been munged
@@ -139,20 +139,25 @@ export var openDirectory = async function (options = {}, cb) {
export var writeFile = async function (fileHandle, contents) {
// 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();
};
const writable = await fileHandle.createWritable()
if (contents instanceof ReadableStream) {
await contents.pipeTo(writable)
} else {
// 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()
}
}
export var nfsSupported = function () {
if ('chooseFileSystemEntries' in self) {
return 'chooseFileSystemEntries';
return 'chooseFileSystemEntries'
} else if ('showOpenFilePicker' in self) {
return 'showOpenFilePicker';
return 'showOpenFilePicker'
}
return false;
return false
}
const inputTypes = [