From 8aeba2c0ab226a891d7b22994a984144952a28ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Mon, 22 Feb 2021 03:15:54 +0100 Subject: [PATCH] bug fix --- example/test.js | 47 +++++++++++++++++--------------- src/getOriginPrivateDirectory.js | 8 +++--- src/util.js | 4 +-- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/example/test.js b/example/test.js index 509486b..403def9 100644 --- a/example/test.js +++ b/example/test.js @@ -890,7 +890,7 @@ form_showDirectoryPicker.onsubmit = evt => { evt.preventDefault() const opts = Object.fromEntries([...new FormData(evt.target)]) opts._preferPolyfill = !!opts._preferPolyfill - showDirectoryPicker(opts).then(console.log, console.error) + showDirectoryPicker(opts).then(showFileStructur, console.error) } form_showOpenFilePicker.onsubmit = evt => { evt.preventDefault() @@ -950,8 +950,8 @@ function arrayEqual(a1, a2) { } async function cleanupSandboxedFileSystem (root) { - for await (let entry of root.entries()) { - await root.removeEntry(entry.name, { recursive: entry.kind === 'directory' }) + for await (let [name, entry] of root) { + await root.removeEntry(name, { recursive: entry.kind === 'directory' }) } } @@ -967,19 +967,17 @@ async function getFileContents (handle) { async function getDirectoryEntryCount (handle) { let result = 0 - for await (let entry of handle.entries()) { - result++ - } + for await (let entry of handle) result++ return result } async function getSortedDirectoryEntries(handle) { let result = []; - for await (let entry of handle.entries()) { + for await (let [name, entry] of handle) { if (entry.kind === 'directory') - result.push(entry.name + '/') + result.push(name + '/') else - result.push(entry.name) + result.push(name) } result.sort() return result @@ -1020,31 +1018,35 @@ init().catch(console.error) globalThis.ondragover = evt => evt.preventDefault() globalThis.ondrop = async evt => { evt.preventDefault() + const root = await getOriginPrivateDirectory(evt.dataTransfer) + showFileStructur(root) +} +async function showFileStructur(root) { let result = [] let cwd = '' // return result.sort() + const readonly = document.querySelector('[form=form_showOpenFilePicker][name="_preferPolyfill"]').checked try { - const root = await getOriginPrivateDirectory(evt.dataTransfer) - assert(await getDirectoryEntryCount(root) > 0) - assert(await root.requestPermission({ writable: true }) === 'denied') + readonly && assert(await getDirectoryEntryCount(root) > 0) + readonly && assert(await root.requestPermission({ writable: true }) === 'denied') const dirs = [root] for (let dir of dirs) { cwd += dir.name + '/' - for await (let entry of dir.entries()) { + for await (let [name, handle] of dir) { // Everything should be read only - assert(await entry.requestPermission({ writable: true }) === 'denied') - assert(await entry.requestPermission({ readable: true }) === 'granted') - if (entry.kind === 'file') { - result.push(cwd + entry.name) - err = await entry.createWritable().catch(e=>e) - assert(err.name === 'NotAllowedError') + readonly && assert(await handle.requestPermission({ writable: true }) === 'denied') + readonly && assert(await handle.requestPermission({ readable: true }) === 'granted') + if (handle.kind === 'file') { + result.push(cwd + handle.name) + readonly && (err = await handle.createWritable().catch(e=>e)) + readonly && assert(err.name === 'NotAllowedError') } else { - result.push(cwd + entry.name + '/') - assert(entry.kind === 'directory') - dirs.push(entry) + result.push(cwd + handle.name + '/') + assert(handle.kind === 'directory') + dirs.push(handle) } } } @@ -1052,6 +1054,7 @@ globalThis.ondrop = async evt => { console.log(result) alert('assertion succeed\n' + result) } catch (err) { + console.log(err) alert('assertion failed - see console') } } diff --git a/src/getOriginPrivateDirectory.js b/src/getOriginPrivateDirectory.js index fb40767..6c6d138 100644 --- a/src/getOriginPrivateDirectory.js +++ b/src/getOriginPrivateDirectory.js @@ -6,14 +6,14 @@ import FileSystemDirectoryHandle from './FileSystemDirectoryHandle.js' */ async function getOriginPrivateDirectory (driver, options = {}) { if (typeof DataTransfer === 'function' && driver instanceof DataTransfer) { - const entries = [driver.items].map(item => + const entries = [...driver.items].map(item => { // @ts-ignore - item.webkitGetAsEntry() - ) + return item.webkitGetAsEntry() + }) return import('./util.js').then(m => m.fromDataTransfer(entries)) } if (!driver) { - return globalThis.getOriginPrivateDirectory() + return globalThis.navigator?.storage?.getDirectory() || globalThis.getOriginPrivateDirectory() } let module = await driver const sandbox = module.default ? await module.default(options) : module(options) diff --git a/src/util.js b/src/util.js index 78dcc21..e87a2bd 100644 --- a/src/util.js +++ b/src/util.js @@ -16,7 +16,7 @@ export async function fromDataTransfer (entries) { ]) const folder = new memory.FolderHandle('', false) - folder.entries = entries.map(entry => entry.isFile + folder._entries = entries.map(entry => entry.isFile ? new sandbox.FileHandle(entry, false) : new sandbox.FolderHandle(entry, false) ) @@ -43,7 +43,7 @@ export async function fromInput (input) { if (!dir._entries[path]) dir._entries[path] = new FolderHandle(path, false) return dir._entries[path] }, root) - dir.entries[name] = new FileHandle(file.name, file, false) + dir._entries[name] = new FileHandle(file.name, file, false) }) return new FileSystemDirectoryHandle(root) } else {