UNPKG

dropzone-file-parser

Version:

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

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