I am working on determining when a directory tree has been completely traversed after being dropped onto a page. My goal is to identify the point at which the next process can begin once the entire folder and its sub-directories have been processed by the drop event code.
For instance, in the following code snippet, I aim to send the files via JSON to a server after the folder has been processed. Please note that the Ajax call is not included here.
Here is the recursive directory tree traversal function:
async function processEnt(ent,n,fls){
n = n==undefined? 0:n;
if( ent.isFile ){
fls.files.push(ent.name)
}
else if (ent.isDirectory){
fls.dirs[ent.name]={ "files":[], "dirs":{},"done":false }
var r = ent.createReader()
await r.readEntries(
async function f(ents){
if(ents.length >0){
for(var e of ents){
await processEnt(e,n+1,fls.dirs[ent.name])
}
await r.readEntries(f)
}
}
)
}
console.log(n +" level done")
}
The drop event handler:
async function onDrop(evt){
evt.preventDefault()
evt.stopPropagation()
M.files = {
"files":[],
"dirs":{}
}
for(item of evt.dataTransfer.items){
await processEnt( item.webkitGetAsEntry(),0,M.files )
}
console.log("on drop done")
}