UNPKG

dropzone-file-parser

Version:

A package that helps to parse the file and folders dropped in a dropzone

2 lines (1 loc) 2.33 kB
function i(i,e,t,r){return new(t||(t=Promise))((function(s,n){function o(i){try{h(r.next(i))}catch(i){n(i)}}function d(i){try{h(r.throw(i))}catch(i){n(i)}}function h(i){var e;i.done?s(i.value):(e=i.value,e instanceof t?e:new t((function(i){i(e)}))).then(o,d)}h((r=r.apply(i,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class e{constructor(i,e){this.name=i.name||"root",this.isFile=i.isFile||!1,this.id=i.id||"",this.parent=e,this.size=0,this.fileCount=0,this.sizeUpdatePromise=this.initialize(i)}initialize(e){return i(this,void 0,void 0,(function*(){return e.isFile?new Promise(((i,t)=>{e.file((e=>{this.file=e,this.size=e.size,this.parent&&this.parent.updateSize(e.size),i()}),t)})):(this.children=[],this.fileCount=0,Promise.resolve())}))}static create(t,r){return i(this,void 0,void 0,(function*(){const i=new e(t,r);return yield i.sizeUpdatePromise,i}))}addChild(i){this.children||(this.children=[]),this.children.push(i),i.isFile&&this.updateFileCount()}updateSize(i){this.size=this.size+i,this.parent&&this.parent.updateSize(i)}updateFileCount(){this.fileCount++,this.parent&&this.parent.updateFileCount()}getFiles(){var i,e;return this.isFile?[this]:null!==(e=null===(i=this.children)||void 0===i?void 0:i.reduce(((i,e)=>i.concat(e.getFiles())),[]))&&void 0!==e?e:[]}}class t{constructor(i){this.root=new e({name:"root",id:i},null),this.fileCount=0}addNode(t){return i(this,void 0,void 0,(function*(){const i=yield e.create(t,this.root);return this.root.addChild(i),i}))}processNode(e){return i(this,void 0,void 0,(function*(){const i=yield this.addNode(e);return e.isDirectory?this.parseDirectory(e,i):(this.fileCount++,Promise.resolve())}))}parseDirectory(t,r){return new Promise(((s,n)=>{const o=t.createReader(),d=()=>{o.readEntries((t=>{if(0===t.length)s();else{const s=t.map((t=>i(this,void 0,void 0,(function*(){const i=yield e.create(t,r);return r.addChild(i),t.isDirectory?this.parseDirectory(t,i):(this.fileCount++,Promise.resolve())}))));Promise.all(s).then((()=>d())).catch(n)}}),n)};d()}))}getFiles(){return this.root.getFiles()}}const r=(e,r)=>i(void 0,void 0,void 0,(function*(){const i=new t(r),s=Array.from(e).map((e=>{const t=e.webkitGetAsEntry();if(t)return i.processNode(t)})).filter((i=>void 0!==i));return yield Promise.all(s),i}));export{r as constructDirectoryNodeList};