GoogleChromeLabs / browser-fs-access

File System Access API with legacy fallback in the browser
https://googlechromelabs.github.io/browser-fs-access/demo/
Apache License 2.0
1.38k stars 84 forks source link

directoryOpen doesnt include empty folders to represent the tree structure #96

Closed NotASingleSingelton closed 2 years ago

NotASingleSingelton commented 2 years ago

Hello,

currently there is an issue with directoryOpen and the returning FileWithDirectoryAndFileHandle[] which does not contain information about empty folders which I would require for a projection of the whole tree structure.

Is there an way which I don't see? Would it be possible to make this possible like adding a array which represents the tree structure or something?

tomayac commented 2 years ago

You can do this, but note that it won't work on browsers that don't support the File System Access API. Here's a snippet:

      const blobs = await directoryOpen({ recursive:true });
      const processed = [];
      blobs.forEach(async (blob) => {
        if (!blob.directoryHandle) {
          return;
        } 
        if (processed.includes(blob.directoryHandle)) {
          return;
        }
        processed.push(blob.directoryHandle);
        for await (const entry of blob.directoryHandle.values()) {
          if (entry.kind === 'directory') {
            console.log(entry.kind, entry.name);
          }
        }  
      });

You can see this in action at https://wary-kaput-cupboard.glitch.me/ (click the Open Directory button and check the console. This lists empty folders. You can keep track of the nesting if you're interested.