Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Feb 22, 2021
1 parent 9d0af55 commit 8aeba2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
47 changes: 25 additions & 22 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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' })
}
}

Expand All @@ -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
Expand Down Expand Up @@ -1020,38 +1018,43 @@ 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)
}
}
}
result = JSON.stringify(result.sort(), null, 2)
console.log(result)
alert('assertion succeed\n' + result)
} catch (err) {
console.log(err)
alert('assertion failed - see console')
}
}
8 changes: 4 additions & 4 deletions src/getOriginPrivateDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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 {
Expand Down

0 comments on commit 8aeba2c

Please sign in to comment.