-
Notifications
You must be signed in to change notification settings - Fork 757
/
idb.filesystem.min.js
21 lines (20 loc) · 8.88 KB
/
idb.filesystem.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*! (c) 2017 Copyright (c) 2016 Eric Bidelman. All rights reserved.
* @version v0.0.8 (Apache) */
(function(f){function p(a){var b=a.code,c=a.name;Object.defineProperty(this,"code",{set:function(a){b=a},get:function(){return b}});Object.defineProperty(this,"name",{set:function(a){c=a},get:function(){return c}})}function w(a,b){var c=b;"/"!=b[0]&&(c=a+"/"+b);a=c.split("/");b=[];for(c=0;c<a.length;++c){var d=a[c];if(".."===d){if(!b.length)throw Error("Invalid path");b.pop()}else"."!==d&&""!==d&&b.push(d)}return c="/"+b.join("/")}function x(a){var b=null;this.size=a.size||0;this.name=a.name||"";
this.type=a.type||"";this.lastModifiedDate=a.lastModifiedDate||null;Object.defineProperty(this,"blob_",{enumerable:!0,get:function(){return b},set:function(a){b=a;this.size=b.size;this.name=b.name;this.type=b.type;this.lastModifiedDate=b.lastModifiedDate}.bind(this)})}function C(a){if(!a)throw Error("Expected fileEntry argument to write.");var b=0,c=a.file_?a.file_.blob_:null;Object.defineProperty(this,"position",{get:function(){return b}});Object.defineProperty(this,"length",{get:function(){return c?
c.size:0}});this.seek=function(a){b=a;b>this.length&&(b=this.length);0>b&&(b+=this.length);0>b&&(b=0)};this.truncate=function(a){c=c?a<this.length?c.slice(0,a):new Blob([c,new Uint8Array(a-this.length)],{type:c.type}):new Blob([]);b=0;this.write(c)};this.write=function(d){if(!d)throw Error("Expected blob argument to write.");if(this.onwritestart)this.onwritestart();if(c){var e=c.slice(0,b),f=c.slice(b+d.size),h=b-e.size;0>h&&(h=0);c=new Blob([e,new Uint8Array(h),d,f],{type:c.type})}else c=new Blob([d],
{type:d.type});e=function(e){a.file_.blob_=e;a.file_.lastModifiedDate=d.lastModifiedDate||new Date;g.put(a,function(e){r||(a.file_.blob_=c,a.file_.lastModifiedDate=d.lastModifiedDate||null);b+=d.size;if(this.onwriteend)this.onwriteend()}.bind(this),this.onerror)}.bind(this);r?e(c):D(c,e)}}function E(a){var b=!1;this.readEntries=function(c,d){if(!c)throw Error("Expected successCallback argument.");b?c([]):g.getAllEntries(a.fullPath,function(a){b=!0;c(a)},d)}}function y(a,b){this.modificationTime_=
a||null;this.size_=b||0}function u(){}function k(a){this.file_=null;Object.defineProperty(this,"isFile",{enumerable:!0,get:function(){return!0}});Object.defineProperty(this,"isDirectory",{enumerable:!0,get:function(){return!1}});a&&(this.file_=a.file_,this.name=a.name,this.fullPath=a.fullPath,this.filesystem=a.filesystem,"string"===typeof this.file_.blob_&&(this.file_.blob_=B(this.file_.blob_)))}function h(a){Object.defineProperty(this,"isFile",{enumerable:!0,get:function(){return!1}});Object.defineProperty(this,
"isDirectory",{enumerable:!0,get:function(){return!0}});a&&(this.name=a.name,this.fullPath=a.fullPath,this.filesystem=a.filesystem)}function F(a,b){v=a==f.TEMPORARY?"Temporary":"Persistent";this.name=(location.protocol+location.host).replace(/:/g,"_")+":"+v;this.root=new h;this.root.fullPath="/";this.root.filesystem=this;this.root.name=""}function l(a){switch(a.target.errorCode){case 12:console.log("Error - Attempt to open db with a lower version than the current one.");break;default:console.log("errorCode: "+
a.target.errorCode)}console.log(a,a.code,a.message)}if(!f.requestFileSystem&&!f.webkitRequestFileSystem){var q=f.indexedDB||f.mozIndexedDB||f.msIndexedDB;if(q){var r=!0;new function(){q.deleteDatabase("blob-support").onsuccess=function(){var a=q.open("blob-support",1);a.onerror=function(){r=!1};a.onsuccess=function(){var b=a.result;try{var c=new Blob(["test"],{type:"text/plain"});b.transaction("store","readwrite").objectStore("store").put(c,"key");r=!0}catch(d){r=!1}finally{b.close(),q.deleteDatabase("blob-support")}};
a.onupgradeneeded=function(){a.result.createObjectStore("store")}}};var B=function(a){if(-1==a.indexOf(";base64,")){var b=a.split(",");a=b[0].split(":")[1];b=decodeURIComponent(b[1]);return new Blob([b],{type:a})}b=a.split(";base64,");a=b[0].split(":")[1];b=window.atob(b[1]);for(var c=b.length,d=new Uint8Array(c),e=0;e<c;++e)d[e]=b.charCodeAt(e);return new Blob([d],{type:a})},D=function(a,b){var c=new FileReader;c.readAsDataURL(a);c.onloadend=function(){b(c.result)}};f.PERSISTENT||(f.TEMPORARY=0,
f.PERSISTENT=1);void 0===f.FileError&&(window.FileError=function(){},FileError.prototype.prototype=Error.prototype);FileError.INVALID_MODIFICATION_ERR||(FileError.INVALID_MODIFICATION_ERR=9,FileError.NOT_FOUND_ERR=1);p.prototype=FileError.prototype;p.prototype.toString=Error.prototype.toString;var m=new p({code:FileError.INVALID_MODIFICATION_ERR,name:"INVALID_MODIFICATION_ERR"}),z=new p({code:1E3,name:"Not implemented"}),t=new p({code:FileError.NOT_FOUND_ERR,name:"Not found"}),n=null,v="temporary",
g={db:null},A=String.fromCharCode(48);x.prototype.constructor=x;y.prototype={get modificationTime(){return this.modificationTime_},get size(){return this.size_}};u.prototype={name:null,fullPath:null,filesystem:null,copyTo:function(){throw z;},getMetadata:function(a,b){if(!a)throw Error("Expected successCallback argument.");try{this.isFile?a(new y(this.file_.lastModifiedDate,this.file_.size)):b(new p({code:1001,name:"getMetadata() not implemented for DirectoryEntry"}))}catch(c){b&&b(c)}},getParent:function(){throw z;
},moveTo:function(){throw z;},remove:function(a,b){if(!a)throw Error("Expected successCallback argument.");g["delete"](this.fullPath,function(){a()},b)},toURL:function(){return"filesystem:"+(location.protocol+"//"+location.host)+"/"+v.toLowerCase()+this.fullPath}};k.prototype=new u;k.prototype.constructor=k;k.prototype.createWriter=function(a){a(new C(this))};k.prototype.file=function(a,b){if(!a)throw Error("Expected successCallback argument.");if(null==this.file_)if(b)b(t);else throw t;else b=null==
this.file_.blob_?this.file_:this.file_.blob_,b.lastModifiedDate=this.file_.lastModifiedDate,a(b)};h.prototype=new u;h.prototype.constructor=h;h.prototype.createReader=function(){return new E(this)};h.prototype.getDirectory=function(a,b,c,d){a=w(this.fullPath,a);g.get(a,function(e){b||(b={});!0===b.create&&!0===b.exclusive&&e?d&&d(m):!0!==b.create||e?!0===b.create&&e?e.isDirectory?c(new h(e)):d&&d(m):b.create&&!1!==b.create||e?b.create&&!1!==b.create||!e||!e.isFile?c(new h(e)):d&&d(m):"/"==a?(e=new h,
e.name="",e.fullPath="/",e.filesystem=n,c(e)):d&&d(t):(e=new h,e.name=a.split("/").pop(),e.fullPath=a,e.filesystem=n,g.put(e,c,d))},d)};h.prototype.getFile=function(a,b,c,d){a=w(this.fullPath,a);g.get(a,function(e){b||(b={});!0===b.create&&!0===b.exclusive&&e?d&&d(m):!0!==b.create||e?!0===b.create&&e?e.isFile?c(new k(e)):d&&d(m):b.create&&!1!==b.create||e?b.create&&!1!==b.create||!e||!e.isDirectory?c(new k(e)):d&&d(m):d&&d(t):(e=new k,e.name=a.split("/").pop(),e.fullPath=a,e.filesystem=n,e.file_=
new x({size:0,name:e.name,lastModifiedDate:new Date}),g.put(e,c,d))},d)};h.prototype.removeRecursively=function(a,b){if(!a)throw Error("Expected successCallback argument.");this.remove(a,b)};g.open=function(a,b,c){var d=this;a=q.open(a.replace(":","_"));a.onerror=c||l;a.onupgradeneeded=function(a){d.db=a.target.result;d.db.onerror=l;d.db.objectStoreNames.contains("entries")||d.db.createObjectStore("entries")};a.onsuccess=function(a){d.db=a.target.result;d.db.onerror=l;b(a)};a.onblocked=c||l};g.close=
function(){this.db.close();this.db=null};g.drop=function(a,b){if(this.db){var c=q.deleteDatabase(this.db.name);c.onsuccess=function(b){a(b)};c.onerror=b||l;g.close()}};g.get=function(a,b,c){if(this.db){var d=this.db.transaction(["entries"],"readonly");a=IDBKeyRange.bound(a,a+A,!1,!0);var e=d.objectStore("entries").get(a);d.onabort=c||l;d.oncomplete=function(a){b(e.result)}}};g.getAllEntries=function(a,b,c){if(this.db){var d=[],e=null;"/"!=a&&(e=IDBKeyRange.bound(a+"/",a+A,!1,!0));var f=this.db.transaction(["entries"],
"readonly");f.onabort=c||l;f.oncomplete=function(c){d=d.filter(function(b){var c=b.fullPath.split("/").length,d=a.split("/").length;if("/"==a&&c<d+1||"/"!=a&&c==d+1)return b});b(d)};f.objectStore("entries").openCursor(e).onsuccess=function(a){if(a=a.target.result){var b=a.value;d.push(b.isFile?new k(b):new h(b));a["continue"]()}}}};g["delete"]=function(a,b,c){if(this.db){var d=this.db.transaction(["entries"],"readwrite");d.oncomplete=b;d.onabort=c||l;a=IDBKeyRange.bound(a,a+A,!1,!0);d.objectStore("entries")["delete"](a)}};
g.put=function(a,b,c){if(this.db){var d=this.db.transaction(["entries"],"readwrite");d.onabort=c||l;d.oncomplete=function(c){b(a)};d.objectStore("entries").put(a,a.fullPath)}};f.addEventListener("beforeunload",function(a){g.db&&g.db.close()},!1);f.requestFileSystem=function(a,b,c,d){a!=f.TEMPORARY&&a!=f.PERSISTENT&&d?d(m):(n=new F(a,b),g.open(n.name,function(a){c(n)},d))};f.resolveLocalFileSystemURL=function(a,b,c){var d="filesystem:"+(location.protocol+"//"+location.host)+"/"+v.toLowerCase();a=a.replace(d,
"");"/"===a.substr(-1)&&(a=a.slice(0,-1));a?g.get(a,function(a){if(a){if(a.isFile)return b(new k(a));if(a.isDirectory)return b(new h(a))}else c&&c(t)},c):b(n.root)};f===window&&f.RUNNING_TESTS&&(f.Entry=u,f.FileEntry=k,f.DirectoryEntry=h,f.resolveToFullPath_=w,f.Metadata=y,f.Base64ToBlob=B)}}})(self);