-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfso.min.js
27 lines (27 loc) · 13.2 KB
/
fso.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Copyright (c) 2014 Keith Horwood | The MIT License (MIT) | https://raw.github.com/keithwhor/FSO.js/master/LICENSE */
(function(){function l(a,c,b,d){a=parseInt(a);a=isNaN(a)?1073741824:Math.ceil(Math.max(0,a));this._queue=[];this.__state=m.INITIALIZING;this.fs=null;this.availableBytes=0;this.persistent=!!c;this.rootURL="filesystem:"+window.location.origin+"/"+(this.persistent?"persistent/":"temporary/");this._callbacks={success:null,error:null};"function"===typeof b&&(this._callbacks.success=b);"function"===typeof d&&(this._callbacks.__error__=d);if(b=window.requestFileSystem||window.webkitRequestFileSystem){d=
window.PERSISTENT||1;var e=window.TEMPORARY||0;b(this.persistent?d:e,a,this.__init__.bind(this,a,c),this.__error__.bind(this))}else this.__error__({name:"constructor"},"This browser does not support the FileSystem API")}function p(a){a instanceof l||this.__error__({name:"constructor"},"FSOQueue must be instantiated with valid FSO");this.parent=a;this._callbacksSet=!1;this._callbacks={success:null,error:null};this.__line=null;this.__committed=!1;this.parent.__state===m.INITIALIZING?this.__state=m.INITIALIZING:
(this.__state=m.READY,this.fs=this.parent.fs,this.currentDirectory=this.fs.root);this._queue=[]}var y=new Blob([new ArrayBuffer(2097152)]),z=new Blob([new ArrayBuffer(0)]),m={INITIALIZING:0,READY:1,EXECUTING:2,COMPLETE:4,ERROR:9},t=function(a,c,b){var d=0,e=[z];c=Math.abs(parseInt(c)||0);c%=2097152;if(a)if("string"===typeof a){b=a;a=[];a.length=b.length;for(var f=0,g=a.length;f<g;f++)a[f]=b.charCodeAt(f);a=(new Uint8Array(a)).buffer;b=a.byteLength}else if(a instanceof Array)a=(new UInt8Array(a)).buffer,
b=a.byteLength;else if(a instanceof ArrayBuffer)b=a.byteLength;else if(a.buffer&&a.buffer instanceof ArrayBuffer)a=a.buffer,b=a.byteLength;else throw"function"===typeof b&&b("Data incorrectly formatted"),Error("Data incorrectly formatted");else return[new Blob([new ArrayBuffer(c)])];if(0<c+b){e=[];e.length=Math.ceil((b+c)/2097152);f=Math.min(2097152-c,b);e[d++]=new Blob([new ArrayBuffer(c),new Int8Array(a,0,f)]);b-=f;for(c=f;2097152<b;)e[d++]=new Blob([new Int8Array(a,c,2097152)]),c+=2097152,b-=2097152;
e[d]=new Blob([new Int8Array(a,c,b)])}return e},A=function(a){0!==a.indexOf("/")&&(a="/"+a);return a},u={prettySize:function(a){var c=Math.log(a)/Math.log(1024)|0;return Math.round(100*a/Math.pow(1024,c))/100+" k M G T P E".split(" ")[c]+"B"},prettyDirectory:function(a,c){c|=0;var b=[],d;d=[];d.length=2*c+1;d=""+d.join(" ");d+=(a.isFile?" -":" ")+" "+(a.isFile?a.name:"["+a.name+"]")+(a.isFile?" ("+u.prettySize(a.size)+")":"");b.push(d);if(a.isDirectory){a.children.sort(function(a,b){return a.isFile===
b.isFile?a.name>b.name:a.isFile<b.isFile});d=0;for(var e=a.children.length;d<e;d++)b=b.concat(u.prettyDirectory(a.children[d],c+1))}return 0<c?b:b.join(String.fromCharCode(10))}},v={getBytes:function(a){var c=this.__error__.bind(this,{name:"getBytes"}),b=this.__execute__.bind(this,a),d=this.parent;this.parent.persistent?navigator.webkitPersistentStorage.queryUsageAndQuota(function(a,c){d.availableBytes=c;b(a,c)},c):navigator.webkitTemporaryStorage.queryUsageAndQuota(function(a,c){d.availableBytes=
c;b(a,c)},c)},info:function(a,c){var b=this.__error__.bind(this,{name:"info",path:a}),d=this.__execute__.bind(this,c),e,f=this.fs,g=this.parent.toURL.bind(this.parent),k=function(a){a={name:this.name,fullPath:this.fullPath,url:g(this.fullPath),isDirectory:this.isDirectory,isFile:this.isFile,size:a.size,modified:a.modificationTime};d(a)};e=function(){f.root.getFile(a,{},function(a){a.getMetadata(k.bind(a),b)},b)};(function(){f.root.getDirectory(a,{},function(a){a.getMetadata(k.bind(a),b)},function(a){"TypeMismatchError"===
a.name?e():b(a)})})()},read:function(a,c){var b=this.__error__.bind(this,{name:"read",path:a}),d=this.__execute__.bind(this,c);this.fs.root.getFile(a,{},function(a){a.file(function(a){var b=new FileReader;b.onloadend=function(){d(this.result)};b.readAsText(a)},b)},b)},readBuffer:function(a,c){var b=this.__error__.bind(this,{name:"read",path:a}),d=this.__execute__.bind(this,c);this.fs.root.getFile(a,{},function(a){a.file(function(a){var b=new FileReader;b.onloadend=function(){d(this.result)};b.readAsArrayBuffer(a)},
b)},b)},put:function(a,c,b,d){var e=this.__error__.bind(this,{name:"put",path:c}),f=this.__execute__.bind(this,d);"/"!==c.substr(-1)&&(c+="/");b||(b=null);a instanceof File||e("Invalid file");this.fs.root.getFile(c+(b?b:a.name),{create:!0,exclusive:!1},function(b){b.createWriter(function(b){b.onerror=e;b.truncate(0);b.onwriteend=function(){b.seek(0);b.write(a);b.onwriteend=f}},e)},e)},write:function(a,c,b){var d=this.__error__.bind(this,{name:"write",path:a}),e=this.__execute__.bind(this,b);this.fs.root.getFile(a,
{create:!0,exclusive:!1},function(a){a.createWriter(function(a){a.onerror=d;a.truncate(0);a.onwriteend=function(){var b=t(c,0,d),f=b.length-1,r=0,n=[function(){a.seek(2097152*r);a.write(b[r++],d)},e],q=function(){n[f-r>>>31]()};a.onwriteend=q;q()}},d)},d)},insert:function(a,c,b,d){var e=this.__error__.bind(this,{name:"insert",path:a}),f=this.__execute__.bind(this,d);b=Number(b);b=isNaN(b)?0:Math.max(0,b);this.fs.root.getFile(a,{create:!0,exclusive:!1},function(a){a.createWriter(function(a){a.onerror=
e;var d=a.length||0,g,n=Math.floor((b-d)/2097152);if(n){var d=1,q=[y],s=n;for(g=[];0<s;)s%(2*d)===d&&(g=g.concat(q),s-=d),q=q.concat(q),d*=2;g=g.concat(t(c,b-2097152*n,e))}else g=t(c,Math.max(0,b-d),e);var m=g.length-1,l=0,p=[function(){a.seek(b+2097152*l);a.write(g[l++],e)},f],n=function(){p[m-l>>>31]()};a.onwriteend=n;n()},e)},e)},append:function(a,c,b){var d=this.__error__.bind(this,{name:"append",path:a}),e=this.__execute__.bind(this,b);this.fs.root.getFile(a,{create:!0,exclusive:!1},function(a){a.createWriter(function(a){a.onerror=
d;var b=a.length,f=t(c,0,d),r=f.length-1,n=0,q=[function(){a.seek(b+2097152*n);a.write(f[n++],d)},e],s=function(){q[r-n>>>31]()};a.onwriteend=s;s()},d)},d)},rm:function(a,c){var b=this.__error__.bind(this,{name:"rm",path:a}),d=this.__execute__.bind(this,c),e,f=this.fs;e=function(){f.root.getFile(a,{},function(a){a.remove(d.bind(null,!0),b)},function(a){"NotFoundError"===a.name?d():b(a)})};(function(){f.root.getDirectory(a,{},function(a){a.remove(d,function(a){"InvalidModificationError"===a.name?b("Directory non-empty, please use .rmdir for recursive directory removal"):
b(a)})},function(a){"NotFoundError"===a.name?d():"TypeMismatchError"===a.name?e():b(a)})})()},mkdir:function(a,c){var b=this.__error__.bind(this,{name:"mkdir",path:a}),d=this.__execute__.bind(this,c),e=a.split("/"),f=[],g,k=this.fs;g=function(){k.root.getDirectory(e.join("/"),{create:!0},function(){f.length?(e.push(f.pop()),g()):d()},function(a){"NotFoundError"===a.name?(f.push(e.pop()),g()):b(a)})};g()},rmdir:function(a,c){var b=this.__error__.bind(this,{name:"rmdir",path:a}),d=this.__execute__.bind(this,
c),e=this.fs;""===a||"/"===a?e.root.createReader().readEntries(function(a){var c=0,e=a.length;if(e)for(var h=function(){c++;c===e&&d()},r=0;r<e;r++){var n=a[r];n.isDirectory?n.removeRecursively(h,b):n.remove(h,b)}else d()},b):e.root.getDirectory(a,{},function(a){a.removeRecursively(d,b)},function(a){"NotFoundError"===a.name?d():"TypeMismatchError"===a.name?b("Not a directory entry"):b(a)})},rename:function(a,c,b){var d=a.lastIndexOf("/",a.length-2),e=null;0<d&&(e=a.substr(0,d));var f=this.__error__.bind(this,
{name:"rename",path:a}),g=this.__error__.bind(this,{name:"rename to",path:c}),k=this.__execute__.bind(this,b),h=this.fs,r=function(){h.root.getDirectory(a,{},function(b){e?h.root.getDirectory(e,{},function(d){b.moveTo(d,c,k.bind(null,a,c),function(a){"InvalidModificationError"===a.name?g("File "+c+" already exists"):g(a)})},g):b.moveTo(h.root,c,k.bind(null,a,c),function(a){"InvalidModificationError"===a.name?g("File "+c+" already exists"):g(a)})},f)};h.root.getFile(a,{},function(b){e?h.root.getDirectory(e,
{},function(d){b.moveTo(d,c,k.bind(null,a,c),function(a){"InvalidModificationError"===a.name?g("File "+c+" already exists"):g(a)})},g):b.moveTo(h.root,c,k.bind(null,a,c),function(a){"InvalidModificationError"===a.name?g("File "+c+" already exists"):g(a)})},function(a){"TypeMismatchError"===a.name?r():f(a)})},move:function(a,c,b,d){var e=this.__error__.bind(this,{name:"move",path:a}),f=this.__error__.bind(this,{name:"move to",path:c}),g=this.__execute__.bind(this,d),k=this.fs;b||(b=null);var h=function(){k.root.getDirectory(a,
{},function(d){k.root.getDirectory(c,{},function(c){d.moveTo(c,b,g.bind(null,a,name),function(a){"InvalidModificationError"===a.name?f("File already exists"):f(a)})},f)},e)};k.root.getFile(a,{},function(d){k.root.getDirectory(c,{},function(c){d.moveTo(c,b,g.bind(null,a,name),function(a){"InvalidModificationError"===a.name?f("File already exists"):f(a)})},f)},function(a){"TypeMismatchError"===a.name?h():e(a)})},copy:function(a,c,b,d){var e=this.__error__.bind(this,{name:"copy",path:a}),f=this.__error__.bind(this,
{name:"copy to",path:c}),g=this.__execute__.bind(this,d),k=this.fs;b||(b=null);var h=function(){k.root.getDirectory(a,{},function(d){k.root.getDirectory(c,{},function(e){d.copyTo(e,b,g.bind(null,a,c),function(a){"InvalidModificationError"===a.name?f("File already exists"):f(a)})},f)},e)};k.root.getFile(a,{},function(d){k.root.getDirectory(c,{},function(e){d.copyTo(e,b,g.bind(null,a,c),function(a){"InvalidModificationError"===a.name?f("File already exists"):f(a)})},f)},function(a){"TypeMismatchError"===
a.name?h():e(a)})},list:function(a,c,b){var d=this.__error__.bind(this,{name:"list",path:a}),e=this.__execute__.bind(this,b),f=this.fs,g=this.parent.toURL.bind(this.parent);null!==c&&(c=Math.max(0,c|0));var k={},h={},m=function(a){delete k[a];Object.keys(k).length||e(h)},n=function(a,b,e){if(!(null!==c&&e>c)){var f=a.fullPath;k[f]=!0;var h=0;a.createReader().readEntries(function(a){var c=a.length;if(c)for(var k=function(a,b){a.getMetadata(function(d){b.size=d.size;b.modified=d.modificationTime;h++;
b.isDirectory&&(b.children=[],n(a,b.children,e+1));h===c&&m(f)},d)},q=0;q<c;q++){var l=a[q],p={name:l.name,fullPath:l.fullPath,url:g(l.fullPath),isDirectory:l.isDirectory,isFile:l.isFile};k(l,p);b.push(p)}else m(f)},d)}};""===a||"/"===a?(h.name="/",h.fullPath="/",h.children=[],h.isFile=!1,h.isDirectory=!0,f.root.getMetadata(function(a){h.size=a.size;h.modified=a.modificationTime;n(f.root,h.children,0)},d)):f.root.getDirectory(a,{},function(a){h.name=a.name;h.fullPath=a.fullPath;h.children=[];h.isFile=
!1;h.isDirectory=!0;a.getMetadata(function(b){h.size=b.size;h.modified=b.modificationTime;n(a,h.children,0)},d)},function(a){"TypeMismatchError"===a.name?d("Not a directory entry"):d(a)})}};l.prototype.__error__=function(a,c){var b;if("string"===typeof c)b=c;else switch(c.name){case "NotFoundError":b="File or directory does not exist";break;default:b=c.message||c.name}a.object=this.constructor.name;a.message=b;this.__state=m.ERROR;b=this._callbacks.__error__;if("function"===typeof b)b.call(this.parent,
a);else throw Error(a.object+"."+a.name+("undefined"!==typeof a.path?" ["+A(a.path)+"]":"")+": "+a.message+(this.__line?" ("+this.__line+")":""));};l.prototype.__requestStorage__=function(a){this.availableBytes=a;this.__state=m.READY;this.__ready__()};l.prototype.__init__=function(a,c,b){this.fs=b;this.rootURL=this.fs.root.toURL();c?navigator.webkitPersistentStorage.requestQuota(a,this.__requestStorage__.bind(this,a),this.__error__.bind(this)):navigator.webkitTemporaryStorage.requestQuota(a,this.__requestStorage__.bind(this,
a),this.__error__.bind(this))};l.prototype.toURL=function(a){0===a.indexOf("/")&&(a=a.substr(1));return this.rootURL+a};l.prototype.__ready__=function(){if(this.__state===m.READY&&(this._callbacks.success&&this._callbacks.success.call(this),this._queue)){for(var a=0,c=this._queue.length;a<c;a++){var b=this._queue[a];b instanceof p&&b.__ready__()}delete this._queue}};l.prototype.createQueue=function(){var a=new p(this);this.__state===m.INITIALIZING?this._queue.push(a):a.__ready__();return a};p.prototype.__error__=
l.prototype.__error__;p.prototype.__ready__=function(){this.__state===m.INITIALIZING&&(this.fs=this.parent.fs,this.currentDirectory=this.fs.root,this.__state=m.READY,this.__committed&&(this.__state=m.EXECUTING,this.__execute__()))};p.prototype.__prepare__=function(a,c){if("function"!==typeof v[a])throw Error("Invalid command: "+a);var b=Error().stack.split("\n")[3],b=b.slice(b.indexOf("at ")+3,b.length);!0===this.__committed&&(this.__line=b,this.__error__({name:a},"Queue is already executing or awaiting execution"));
this._queue.push([a,c,b])};p.prototype.__execute__=function(a){if(this.__state===m.EXECUTING)if("function"===typeof a&&a.apply(this.parent,[].slice.call(arguments,1)),this._queue.length){var c=this._queue.shift();this.__line=c[2];v[c[0]].apply(this,c[1])}else c=this._callbacks.success,"function"===typeof c&&c.call(this.parent),this.__state=m.COMPLETE};p.prototype.execute=function(a,c){if(!0===this.__committed){var b=Error().stack.split("\n")[2];this.__line=b=b.slice(b.indexOf("at ")+3,b.length);this.__error__({name:"execute"},
"Queue is already executing or awaiting execution")}this.__committed=!0;this._callbacksSet||("function"===typeof a&&(this._callbacks.success=a),"function"===typeof c&&(this._callbacks.__error__=c),this._callbacksSet=!0);this.__state===m.READY&&(this.__state=m.EXECUTING,this.__execute__())};for(var x=Object.keys(v),B=function(a){p.prototype[a]=function(){this.__prepare__(a,[].slice.call(arguments));return this}},w=0,C=x.length;w<C;w++)B(x[w]);window.FSO=l;window.FSOUtil=u})();