Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

1
LINES

< > BotCompany Repo | #1018985 // LargeLocalStorage.min.js

Document

(function(a){function b(Q){function a(a,b,c){this._handlers=a,this._next=b,this._end=c,this._i=0}function b(a,b,c){for(var d=0;b>d;++d){var e=a[d];if(e.name===c||e.handler===c)return d}return-1}function d(a){return a.next.apply(a,Array.prototype.slice.call(arguments,1))}function e(a,b){a.forEach(function(a){b[a]||(b[a]=d)})}function f(b){function c(b){this.pipe={_handlers:[],_contextCtor:a,_nextMethods:f,end:d,_pipedMethodNames:b}}var d={},e=function(){return d},f={},h=new c(b);for(var i in g)h.pipe[i]=g[i];return b.forEach(function(a){d[a]=e,f[a]=new Function("var handler = this._nextHandler();handler.__pipectx = this.__pipectx;return handler."+a+".apply(handler, arguments);"),h[a]=new Function("var ctx = new this.pipe._contextCtor(this.pipe._handlers, this.pipe._nextMethods."+a+", this.pipe.end);return ctx.next.apply(ctx, arguments);")}),h}a.prototype={next:function(){return this.__pipectx=this,this._next.apply(this,arguments)},_nextHandler:function(){if(this._i>=this._handlers.length)return this._end;var a=this._handlers[this._i].handler;return this._i+=1,a},length:function(){return this._handlers.length}};var g={addFirst:function(a,b){e(this._pipedMethodNames,b),this._handlers.unshift({name:a,handler:b})},addLast:function(a,b){e(this._pipedMethodNames,b),this._handlers.push({name:a,handler:b})},addAfter:function(a,c,d){e(this._pipedMethodNames,d);var f=this._handlers,g=f.length,h=b(f,g,a);h>=0&&f.splice(h+1,0,{name:c,handler:d})},addBefore:function(a,c,d){e(this._pipedMethodNames,d);var f=this._handlers,g=f.length,h=b(f,g,a);h>=0&&f.splice(h,0,{name:c,handler:d})},replace:function(a,c,d){e(this._pipedMethodNames,d);var f=this._handlers,g=f.length,h=b(f,g,a);h>=0&&f.splice(h,1,{name:c,handler:d})},removeFirst:function(){return this._handlers.shift()},removeLast:function(){return this._handlers.pop()},remove:function(a){var c=this._handlers,d=c.length,e=b(c,d,a);e>=0&&c.splice(e,1)},getHandler:function(a){var c=b(this._handlers,this._handlers.length,a);return c>=0?this._handlers[c].handler:null}};f.isPipeline=function(a){return a instanceof Pipeline};var h=function(){function a(b,c,d,e){b(c[d.length],function(f){d.push(f),d.length==c.length?e.resolve(d):a(b,c,d,e)},function(a){e.reject(a)})}return{convertToBase64:function(a,b){var c=new FileReader;c.onload=function(a){b(a.target.result)},c.onerror=function(){},c.onabort=function(){},c.readAsDataURL(a)},dataURLToBlob:function(a){var b=";base64,";if(-1==a.indexOf(b)){var c=a.split(","),d=c[0].split(":")[1],e=c[1];return new Blob([e],{type:d})}for(var c=a.split(b),d=c[0].split(":")[1],e=window.atob(c[1]),f=e.length,g=new Uint8Array(f),h=0;f>h;++h)g[h]=e.charCodeAt(h);return new Blob([g.buffer],{type:d})},splitAttachmentPath:function(a){var b=a.split("/");return 1==b.length&&b.unshift("__nodoc__"),b},mapAsync:function(b,c){var d=Q.defer();return c.then(function(c){a(b,c,[],d)},function(a){d.reject(a)}),d.promise},countdown:function(a,b){var c=[];return function(){for(var d=0;d<arguments.length;++d)c.push(arguments[d]);a-=1,0==a&&b.apply(this,c)}}}}(),i=window.requestFileSystem||window.webkitRequestFileSystem,j=navigator.persistentStorage||navigator.webkitPersistentStorage,k=function(Q){function a(a,b){return function(d){1==d.code?a.resolve(c):b?b.reject(d):a.reject(d)}}function b(a,b){a=a.replace(/\//g,"--");var c=a+"-attachments";return{dir:c,path:c+"/"+b}}function d(a,b){var c=Q.defer();return e(a,b,c),c.promise}function e(a,b,c){a.readEntries(function(d){0==d.length?c.resolve(b):(b=b.concat(d),e(a,b,c))},function(a){c.reject(a)})}function f(a,b,c){this._fs=a,this._capacity=b,this._prefix=c,this.type="FileSystemAPI"}return f.prototype={getContents:function(b,c){var d=Q.defer();return b=this._prefix+b,this._fs.root.getFile(b,{},function(e){e.file(function(a){var e=new FileReader;e.onloadend=function(a){var e,f=a.target.result;if(c&&c.json)try{f=JSON.parse(f)}catch(a){e=new Error("unable to parse JSON for "+b)}e?d.reject(e):d.resolve(f)},e.readAsText(a)},a(d))},a(d)),d.promise},setContents:function(b,c,d){var e=Q.defer();return d&&d.json&&(c=JSON.stringify(c)),b=this._prefix+b,this._fs.root.getFile(b,{create:!0},function(b){b.createWriter(function(b){var d;b.onwriteend=function(){b.onwriteend=function(){e.resolve()},b.truncate(d.size)},b.onerror=a(e),d=c instanceof Blob?c:new Blob([c],{type:"text/plain"}),b.write(d)},a(e))},a(e)),e.promise},ls:function(a){var b=!1;a?a=this._prefix+a+"-attachments":(a=this._prefix,b=!0);var c=Q.defer();return this._fs.root.getDirectory(a,{create:!1},function(a){var b=a.createReader();d(b,[]).then(function(a){var b=[];a.forEach(function(a){a.isDirectory||b.push(a.name)}),c.resolve(b)})},function(a){c.reject(a)}),c.promise},clear:function(){var a=Q.defer(),b=!1,c=function(c){b=!0,a.reject(c)};return this._fs.root.getDirectory(this._prefix,{},function(d){var e=d.createReader();e.readEntries(function(d){var e=h.countdown(d.length,function(){b||a.resolve()});d.forEach(function(a){a.isDirectory?a.removeRecursively(e,c):a.remove(e,c)}),0==d.length&&a.resolve()},c)},c),a.promise},rm:function(b){var c=Q.defer(),d=Q.defer();b=this._prefix+b;var e=b+"-attachments";return this._fs.root.getFile(b,{create:!1},function(a){a.remove(function(){c.promise.then(d.resolve)},function(a){d.reject(a)})},a(d)),this._fs.root.getDirectory(e,{},function(a){a.removeRecursively(function(){c.resolve()},function(a){d.reject(a)})},a(c,d)),d.promise},getAttachment:function(d,e){var f=this._prefix+b(d,e).path,g=Q.defer();return this._fs.root.getFile(f,{},function(b){b.file(function(a){0==a.size?g.resolve(c):g.resolve(a)},a(g))},function(a){1==a.code?g.resolve(c):g.reject(a)}),g.promise},getAttachmentURL:function(a,c){var d=this._prefix+b(a,c).path,e=Q.defer(),f="filesystem:"+window.location.protocol+"//"+window.location.host+"/persistent/"+d;return e.resolve(f),e.promise},getAllAttachments:function(a){var b=Q.defer(),c=this._prefix+a+"-attachments";return this._fs.root.getDirectory(c,{},function(c){var e=c.createReader();b.resolve(h.mapAsync(function(b,c,d){b.file(function(d){c({data:d,docKey:a,attachKey:b.name})},d)},d(e,[])))},function(){b.resolve([])}),b.promise},getAllAttachmentURLs:function(a){var b=Q.defer(),c=this._prefix+a+"-attachments";return this._fs.root.getDirectory(c,{},function(c){var e=c.createReader();d(e,[]).then(function(c){b.resolve(c.map(function(b){return{url:b.toURL(),docKey:a,attachKey:b.name}}))})},function(a){b.reject(a)}),b.promise},revokeAttachmentURL:function(){},setAttachment:function(c,d,e){var f=b(c,d),g=Q.defer(),h=this;return this._fs.root.getDirectory(this._prefix+f.dir,{create:!0},function(){g.resolve(h.setContents(f.path,e))},a(g)),g.promise},rmAttachment:function(c,d){var e=b(c,d).path,f=Q.defer();return this._fs.root.getFile(this._prefix+e,{create:!1},function(b){b.remove(function(){f.resolve()},a(f))},a(f)),f.promise},getCapacity:function(){return this._capacity}},{init:function(a){var b=Q.defer();if(!i)return b.reject("No FS API"),b.promise;var c=a.name+"/";return j.requestQuota(a.size,function(d){i(window.PERSISTENT,d,function(e){e.root.getDirectory(a.name,{create:!0},function(){b.resolve(new f(e,d,c))},function(a){console.error(a),b.reject(a)})},function(a){console.error(a),b.reject(a)})},function(a){console.error(a),b.reject(a)}),b.promise},isAvailable:function(){return null!=i}}}(Q),l=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.OIndexedDB||window.msIndexedDB,m=window.IDBTransaction||window.webkitIDBTransaction||window.OIDBTransaction||window.msIDBTransaction,n=function(Q){function a(a){this._db=a,this.type="IndexedDB";var b=this._db.transaction(["attachments"],"readwrite");this._supportsBlobs=!0;try{b.objectStore("attachments").put(Blob(["sdf"],{type:"text/plain"}),"featurecheck")}catch(c){this._supportsBlobs=!1}}var b=window.URL||window.webkitURL,d=h.convertToBase64,e=h.dataURLToBlob;return a.prototype={getContents:function(a){var b=Q.defer(),c=this._db.transaction(["files"],"readonly"),d=c.objectStore("files").get(a);return d.onsuccess=function(a){b.resolve(a.target.result)},d.onerror=function(a){b.reject(a)},b.promise},setContents:function(a,b){var c=Q.defer(),d=this._db.transaction(["files"],"readwrite"),e=d.objectStore("files").put(b,a);return e.onsuccess=function(a){c.resolve(a)},e.onerror=function(a){c.reject(a)},c.promise},rm:function(a){var b=Q.defer(),c=Q.defer(),d=this._db.transaction(["files","attachments"],"readwrite"),e=d.objectStore("files").delete(a);e.onsuccess=function(){b.promise.then(function(){c.resolve()})},e.onerror=function(a){b.promise.catch(function(){c.reject(a)})};var f=d.objectStore("attachments"),g=f.index("fname"),h=g.openCursor(IDBKeyRange.only(a));return h.onsuccess=function(a){var c=a.target.result;c?(c.delete(),c.continue()):b.resolve()},h.onerror=function(a){b.reject(a)},c.promise},getAttachment:function(a,b){var d=Q.defer(),f=this._db.transaction(["attachments"],"readonly"),g=f.objectStore("attachments").get(a+"/"+b),h=this;return g.onsuccess=function(a){if(!a.target.result)return d.resolve(c),void 0;var b=a.target.result.data;h._supportsBlobs||(b=e(b)),d.resolve(b)},g.onerror=function(a){d.reject(a)},d.promise},ls:function(a){var b=Q.defer();if(a)var c="attachments";else var c="files";var d=this._db.transaction([c],"readonly"),e=d.objectStore(c).openCursor(),f=[];return e.onsuccess=function(c){var d=c.target.result;d?(f.push(a?d.key.split("/")[1]:d.key),d.continue()):b.resolve(f)},e.onerror=function(a){b.reject(a)},b.promise},clear:function(){var a=Q.defer(),b=Q.defer(),c=this._db.transaction(["attachments","files"],"readwrite"),d=c.objectStore("attachments").clear(),e=c.objectStore("files").clear();return d.onsuccess=function(){a.promise.then(b.resolve)},e.onsuccess=function(){a.resolve()},d.onerror=function(a){b.reject(a)},e.onerror=function(a){b.reject(a)},b.promise},getAllAttachments:function(a){var b=Q.defer(),c=this,d=this._db.transaction(["attachments"],"readonly"),f=d.objectStore("attachments").index("fname"),g=f.openCursor(IDBKeyRange.only(a)),h=[];return g.onsuccess=function(d){var f=d.target.result;if(f){var g;g=c._supportsBlobs?f.value.data:e(f.value.data),h.push({data:g,docKey:a,attachKey:f.primaryKey.split("/")[1]}),f.continue()}else b.resolve(h)},g.onerror=function(a){b.reject(a)},b.promise},getAllAttachmentURLs:function(a){var c=Q.defer();return this.getAllAttachments(a).then(function(a){var d=a.map(function(a){return a.url=b.createObjectURL(a.data),delete a.data,a});c.resolve(d)},function(a){c.reject(a)}),c.promise},getAttachmentURL:function(a,c){var d=Q.defer();return this.getAttachment(a,c).then(function(a){d.resolve(b.createObjectURL(a))},function(a){d.reject(a)}),d.promise},revokeAttachmentURL:function(a){b.revokeObjectURL(a)},setAttachment:function(a,b,c){function e(c){var d={path:a+"/"+b,fname:a,data:c},e=this._db.transaction(["attachments"],"readwrite"),g=e.objectStore("attachments").put(d);g.onsuccess=function(a){f.resolve(a)},g.onerror=function(a){f.reject(a)}}var f=Q.defer();if(c instanceof Blob&&!this._supportsBlobs){var g=this;d(c,function(a){e.call(g,a)})}else e.call(this,c);return f.promise},rmAttachment:function(a,b){var c=Q.defer(),d=this._db.transaction(["attachments"],"readwrite"),e=d.objectStore("attachments").delete(a+"/"+b);return e.onsuccess=function(a){c.resolve(a)},e.onerror=function(a){c.reject(a)},c.promise}},{init:function(b){function c(a){a.createObjectStore("files");var b=a.createObjectStore("attachments",{keyPath:"path"});b.createIndex("fname","fname",{unique:!1})}var d=Q.defer(),e=2;if(!l||!m)return d.reject("No IndexedDB"),d.promise;var f=l.open(b.name,e);return f.onerror=function(a){d.reject(a)},f.onsuccess=function(){var b=f.result;if(b.onerror=function(a){console.log(a)},b.setVersion)if(b.version!=e){var g=b.setVersion(e);g.onsuccess=function(){c(b),d.resolve()}}else d.resolve(new a(b));else d.resolve(new a(b))},f.onupgradeneeded=function(a){c(a.target.result)},d.promise},isAvailable:function(){return null!=l&&null!=m}}}(Q),o=function(Q){return{init:function(){return Q({type:"LocalStorage"})}}}(Q),p=window.openDatabase,q=function(Q){function a(a){this._db=a,this.type="WebSQL"}var b=window.URL||window.webkitURL,d=h.convertToBase64,e=h.dataURLToBlob;return a.prototype={getContents:function(a,b){var d=Q.defer();return this._db.transaction(function(e){e.executeSql("SELECT value FROM files WHERE fname = ?",[a],function(a,e){if(0==e.rows.length)d.resolve(c);else{var f=e.rows.item(0).value;b&&b.json&&(f=JSON.parse(f)),d.resolve(f)}})},function(a){consol.log(a),d.reject(a)}),d.promise},setContents:function(a,b,c){var d=Q.defer();return c&&c.json&&(b=JSON.stringify(b)),this._db.transaction(function(c){c.executeSql("INSERT OR REPLACE INTO files (fname, value) VALUES(?, ?)",[a,b])},function(a){console.log(a),d.reject(a)},function(){d.resolve()}),d.promise},rm:function(a){var b=Q.defer();return this._db.transaction(function(b){b.executeSql("DELETE FROM files WHERE fname = ?",[a]),b.executeSql("DELETE FROM attachments WHERE fname = ?",[a])},function(a){console.log(a),b.reject(a)},function(){b.resolve()}),b.promise},getAttachment:function(a,b){var d=Q.defer();return this._db.transaction(function(f){f.executeSql("SELECT value FROM attachments WHERE fname = ? AND akey = ?",[a,b],function(a,b){0==b.rows.length?d.resolve(c):d.resolve(e(b.rows.item(0).value))})},function(a){d.reject(a)}),d.promise},getAttachmentURL:function(a,c){var d=Q.defer();return this.getAttachment(a,c).then(function(a){d.resolve(b.createObjectURL(a))},function(){d.reject()}),d.promise},ls:function(a){var b,c,d=Q.defer();return a?(b="SELECT akey FROM attachments WHERE fname = ?",c="akey"):(b="SELECT fname FROM files",c="fname"),this._db.transaction(function(e){e.executeSql(b,a?[a]:[],function(a,b){for(var e=[],f=0;f<b.rows.length;++f)e.push(b.rows.item(f)[c]);d.resolve(e)},function(a){d.reject(a)})}),d.promise},clear:function(){var a=Q.defer(),b=Q.defer();return this._db.transaction(function(c){c.executeSql("DELETE FROM files",function(){a.resolve()}),c.executeSql("DELETE FROM attachments",function(){b.resolve()})},function(c){a.reject(c),b.reject(c)}),Q.all([a,b])},getAllAttachments:function(a){var b=Q.defer();return this._db.transaction(function(c){c.executeSql("SELECT value, akey FROM attachments WHERE fname = ?",[a],function(c,d){for(var f=[],g=0;g<d.rows.length;++g){var h=d.rows.item(g);f.push({docKey:a,attachKey:h.akey,data:e(h.value)})}b.resolve(f)})},function(a){b.reject(a)}),b.promise},getAllAttachmentURLs:function(a){var c=Q.defer();return this.getAllAttachments(a).then(function(a){var d=a.map(function(a){return a.url=b.createObjectURL(a.data),delete a.data,a});c.resolve(d)},function(a){c.reject(a)}),c.promise},revokeAttachmentURL:function(a){b.revokeObjectURL(a)},setAttachment:function(a,b,c){var e=Q.defer(),f=this;return d(c,function(c){f._db.transaction(function(d){d.executeSql("INSERT OR REPLACE INTO attachments (fname, akey, value) VALUES(?, ?, ?)",[a,b,c])},function(a){e.reject(a)},function(){e.resolve()})}),e.promise},rmAttachment:function(a,b){var c=Q.defer();return this._db.transaction(function(c){c.executeSql("DELETE FROM attachments WHERE fname = ? AND akey = ?",[a,b])},function(a){c.reject(a)},function(){c.resolve()}),c.promise}},{init:function(b){var c=Q.defer();if(!p)return c.reject("No WebSQL"),c.promise;var d=p(b.name,"1.0","large local storage",b.size);return d.transaction(function(a){a.executeSql("CREATE TABLE IF NOT EXISTS files (fname unique, value)"),a.executeSql("CREATE TABLE IF NOT EXISTS attachments (fname, akey, value)"),a.executeSql("CREATE INDEX IF NOT EXISTS fname_index ON attachments (fname)"),a.executeSql("CREATE INDEX IF NOT EXISTS akey_index ON attachments (akey)"),a.executeSql("CREATE UNIQUE INDEX IF NOT EXISTS uniq_attach ON attachments (fname, akey)")},function(a){c.reject(a)},function(){c.resolve(new a(d))}),c.promise},isAvailable:function(){return null!=p}}}(Q),r=function(Q){function a(a,b){for(var d in b)a[d]===c&&(a[d]=b[d]);return a}function b(b){return b||(b={}),b=a(b,m),b.forceProvider?l[b.forceProvider].init(b):k.init(b).then(function(a){return Q(a)},function(){return n.init(b)}).then(function(a){return Q(a)},function(){return q.init(b)}).then(function(a){return Q(a)},function(){return console.error("Unable to create any storage implementations.  Using LocalStorage"),o.init(b)})}function d(a){var b={};return Object.keys(a).forEach(function(c){b[c]=a[c]}),b}function e(a,c,e,f){var e=j[c.name]&&j[c.name].lastStorageImpl;c.migrate&&(e!=f&&e in l?(c=d(c),c.forceProvider=e,b(c).then(function(b){c.migrate(null,b,a,c)},function(a){c.migrate(a)})):c.migrationComplete&&c.migrationComplete())}function g(a){var c=Q.defer();this.initialized=c.promise;var d=f(["ready","ls","rm","clear","getContents","setContents","getAttachment","setAttachment","getAttachmentURL","getAllAttachments","getAllAttachmentURLs","revokeAttachmentURL","rmAttachment","getCapacity","initialized"]);d.pipe.addLast("lls",this),d.initialized=this.initialized;var g=this;return b(a).then(function(b){g._impl=b,e(d,a,g._impl.type),j[a.name]=j[a.name]||{},j[a.name].lastStorageImpl=b.type,c.resolve(d)}).catch(function(a){console.log(a),c.reject("No storage provider found")}),d}function h(a,b,c){var d=[];return b.forEach(function(b){d.push(c.setAttachment(a,b.attachKey,b.data))}),Q.all(d)}function i(a,b,c){var d=[];return a.forEach(function(a){d.push(b.getContents(a).then(function(b){return c.setContents(a,b)}))}),a.forEach(function(a){d.push(b.getAllAttachments(a).then(function(b){return h(a,b,c)}))}),Q.all(d)}var j=localStorage.getItem("LargeLocalStorage-meta");j=j?JSON.parse(j):{},window.addEventListener("beforeunload",function(){localStorage.setItem("LargeLocalStorage-meta",JSON.stringify(j))});var l={FileSystemAPI:k,IndexedDB:n,WebSQL:q},m={size:10485760,name:"lls"};g.prototype={ready:function(){return null!=this._impl},ls:function(a){return this._checkAvailability(),this._impl.ls(a)},rm:function(a){return this._checkAvailability(),this._impl.rm(a)},clear:function(){return this._checkAvailability(),this._impl.clear()},getContents:function(a,b){return this._checkAvailability(),this._impl.getContents(a,b)},setContents:function(a,b,c){return this._checkAvailability(),this._impl.setContents(a,b,c)},getAttachment:function(a,b){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.getAttachment(a,b)},setAttachment:function(a,b,c){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.setAttachment(a,b,c)},getAttachmentURL:function(a,b){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.getAttachmentURL(a,b)},getAllAttachments:function(a){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.getAllAttachments(a)},getAllAttachmentURLs:function(a){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.getAllAttachmentURLs(a)},revokeAttachmentURL:function(a){return this._checkAvailability(),this._impl.revokeAttachmentURL(a)},rmAttachment:function(a,b){return a||(a="__emptydoc__"),this._checkAvailability(),this._impl.rmAttachment(a,b)},getCapacity:function(){return this._checkAvailability(),this._impl.getCapacity?this._impl.getCapacity():-1},_checkAvailability:function(){if(!this._impl)throw{msg:"No storage implementation is available yet.  The user most likely has not granted you app access to FileSystemAPI or IndexedDB",code:"NO_IMPLEMENTATION"}}},g.contrib={},g.copyOldData=function(a,b,c,d){if(a)throw a;b.ls().then(function(a){return i(a,b,c)}).then(function(){d.migrationComplete&&d.migrationComplete()},function(a){d.migrationComplete(a)})},g._sessionMeta=j;var p=[];return Object.keys(l).forEach(function(a){l[a].isAvailable()&&p.push(a)}),g.availableProviders=p,g}(Q);return r}var c={}.a;"function"==typeof define&&define.amd?define(["Q"],b):a.LargeLocalStorage=b.call(a,Q)}).call(this,this);

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1018985
Snippet name: LargeLocalStorage.min.js
Eternal ID of this version: #1018985/1
Text MD5: 0927e61a1b970dbb2b4a7ec27821663d
Author: stefan
Category: javax / web
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-10-19 16:25:36
Source code size: 19788 bytes / 1 line
Pitched / IR pitched: No / No
Views / Downloads: 345 / 98
Referenced in: [show references]