openmedialibrary_reader/epub.js/libs/fileStorage.min.js

3 lines
11 KiB
JavaScript

/*! fileStorage - v0.1.0 - 2013-03-28 */var fileStorage = fileStorage || {};
fileStorage.core=fileStorage.core||{},fileStorage.core.dataURLToBlob=function(e){var t=";base64,";if(e.indexOf(t)==-1){var n=e.split(","),r=n[0].split(":")[1],i=n[1];return new Blob([i],{type:r})}var n=e.split(t),r=n[0].split(":")[1],i=window.atob(n[1]),s=i.length,o=new Uint8Array(s);for(var u=0;u<s;++u)o[u]=i.charCodeAt(u);return new Blob([o],{type:r})},fileStorage.core.loadFile=function(e,t,n){var r=new XMLHttpRequest;return this.succeeded=function(e){t&&t(e)},this.failed=function(e){console.log("Error:",e)},this.start=function(){var t=this;r.open("GET",e,!0),r.responseType="blob",r.onload=function(e){this.status==200&&t.succeeded(this.response)},r.onerror=function(e){t.failed(this.status)},r.send()},{start:this.start,succeeded:this.succeeded,failed:this.failed}},fileStorage.Queue=function(e,t){this._q=[],this._tasks={},this.idCount=0,this.concurrency=0,this.workers=[],this.available=[],typeof e=="string"&&(this.workerStr=e,this.addWorkers(t||1)),typeof e=="function"&&(this.workerFunction=e,this.addFakeWorkers(t||1))},fileStorage.Queue.prototype.addWorkers=function(e){var t=this.concurrency,n=t+e;for(var r=t;r<e;r++){var i=new Worker(this.workerStr);this.workers.push(i),this.available.push(r)}this.concurrency=e},fileStorage.Queue.prototype.addFakeWorkers=function(e){var t=this.concurrency,n=t+e;for(var r=t;r<e;r++){var i=new fileStorage.FakeWorker(this.workerFunction);this.workers.push(i),this.available.push(r)}this.concurrency=e},fileStorage.Queue.prototype.add=function(e,t,n){var r=this.idCount;return this._tasks[r]={msg:e,callback:t||function(){}},n?(this._q.unshift(r),this.running||this.run()):this._q.push(r),this.idCount++,r},fileStorage.Queue.prototype.addGroup=function(e,t){var n=this,r=e.length,i=function(){r--,r<=0&&t()};return e.forEach(function(e){n.add(e,i)}),this.running||this.run(),i},fileStorage.Queue.prototype.run=function(e){if(this.running)return;this.running=!0;while(this.available.length){var t=this.next();if(!t)break}},fileStorage.Queue.prototype.find=function(e){},fileStorage.Queue.prototype.next=function(){var e=this,t=this._q.shift(),n,r,i;return typeof t=="undefined"?(this.running=!1,!1):(n=this._tasks[t],r=this.available.pop(),i=this.workers[r],i.postMessage(n.msg),i.onmessage=function(i){var s=i.data;n.callback(s),delete e._tasks[t],e.available.push(r),e.next()},i)},fileStorage.Queue.prototype.empty=function(){this._q=[],this._tasks={}},fileStorage.FakeWorker=function(e){this.func=e},fileStorage.FakeWorker.prototype.postMessage=function(e){setTimeout(function(){this.func(e,this.onmessage)}.bind(this),1)},fileStorage.FakeWorker.prototype.onmessage=function(e){},fileStorage.FakeWorker.prototype.close=function(e){},fileStorage.storage=function(e){return this._supported={},this._storageType=!1,this._store=!1,this.determineStorageMethod(e),this},fileStorage.storage.prototype.storageMethod=function(e){console.log("storageMethod",e),!e||typeof fileStorage.store[e]=="undefined"?this._storageType="none":this._storageType=e,this._store=new fileStorage.store[this._storageType],this._store.failed=this._error},fileStorage.storage.prototype.determineStorageMethod=function(e){var t=["filesystem","indexeddb","websql","ram"],n="none";this.checkSupport();if(e&&(e=="none"||this._supported[e]))n=e;else for(var r=-1,i=t.length;++r<i;)if(this._supported[t[r]]){n=t[r];break}this.storageMethod(n)},fileStorage.storage.prototype.get=function(e,t){return this._store.get(e,t)},fileStorage.storage.prototype.batch=function(e,t){return this._store.batch(e,t)},fileStorage.storage.prototype.getURL=function(e){return this._store.getURL(e)},fileStorage.storage.prototype.save=function(e,t,n){return this._store.save(e,t,n)},fileStorage.storage.prototype._error=function(e){console.log("error",e)},fileStorage.storage.prototype.getStorageType=function(){return this._storageType},fileStorage.storage.prototype.checkSupport=function(){var e="filesystem indexeddb websql ram".split(" "),t="RequestFileSystem IndexedDB openDatabase URL".split(" ");for(var n=-1,r=e.length;++n<r;){var i=e[n],s=t[n];this._supported[i]=this.testSupport(s)}},fileStorage.storage.prototype.testSupport=function(e){prefixes=["webkit","moz","o","ms"];for(var t=-1,n=prefixes.length;++t<n;)if(window[prefixes[t]+e])return!0;return e in window},fileStorage.store=fileStorage.store||{},fileStorage.store.filesystem=function(){function o(e){if(r){e(r);return}n(s,i,function(t){r=t,e(t)},v)}function u(e){}function a(e,n){t.addGroup(e,n)}function f(n,r){if(typeof e[n]!="undefined")return e[n];l(n,function(e){var i;e?(i=d(n,e),typeof r!="undefined"&&r(i)):t.add(n,function(e){l(e,function(t){e=d(n,t),typeof r!="undefined"&&r(e)})},!0)})}function l(e,t){var n,r;o(function(n){n.root.getFile(e,{},function(e){t(e)},function(){t(!1)})})}function c(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=v,n.start()}function h(e,t,n){o(function(r){var i=e.split("/").slice(0,-1);p(r.root,i),r.root.getFile(e,{create:!0},function(e){e.createWriter(function(e){e.onwriteend=function(e){n&&n(e)},e.onerror=function(e){v(err)},e.write(t)})},v)})}function p(e,t){if(t[0]=="."||t[0]=="")t=t.slice(1);e.getDirectory(t[0],{create:!0},function(e){t.length&&p(e,t.slice(1))},v)}function d(t,n){var r;return typeof e[t]!="undefined"?e[t]:(r=n.toURL(),e[t]=r,r)}function v(e){typeof this.failed=="undefined"?console.log("Error: ",m(e)):this.failed(e)}function m(e){switch(e.code){case FileError.QUOTA_EXCEEDED_ERR:return"QUOTA_EXCEEDED_ERR";case FileError.NOT_FOUND_ERR:return"NOT_FOUND_ERR";case FileError.SECURITY_ERR:return"SECURITY_ERR";case FileError.INVALID_MODIFICATION_ERR:return"INVALID_MODIFICATION_ERR";case FileError.INVALID_STATE_ERR:return"INVALID_STATE_ERR";case FileError.TYPE_MISMATCH_ERR:return"TYPE_MISMATCH_ERR";default:return"Unknown Error:"+e.code}}var e={},t=new fileStorage.Queue((fileStorage.filePath||"")+"loader_filesystem.js",6),n=window.requestFileSystem||window.webkitRequestFileSystem,r;const i=5242880,s=TEMPORARY;return{get:f,preload:u,batch:a,getURL:d,save:h}},fileStorage.store=fileStorage.store||{},fileStorage.store.indexeddb=function(){function u(e,t){var n={data:null},r=e;p(e,function(e){d(r,e),t(n)})}function a(e){var t;if(i){e(i);return}t=indexedDB.open(o),t.onsuccess=function(n){i=t.result,i.onerror=function(e){console.log("Database error: "+e.target.errorCode)},e&&e(i)},t.onerror=function(e){},t.onupgradeneeded=function(e){var t=e.target.result,n=t.createObjectStore("files",{keyPath:"path"})}}function f(e){}function l(e,t){n.addGroup(e,t)}function c(e,r){if(typeof t[e]!="undefined"){r(t[e]);return}h(e,function(t){var i;t?(i=v(e,t),typeof r!="undefined"&&r(i)):n.add(e,function(t){i=v(e,t),typeof r!="undefined"&&r(i)},!0)})}function h(e,t){var n,r;a(function(i){r=i.transaction(["files"]).objectStore("files"),n=r.get(e),n.onerror=function(e){console.log("error:",e),t(!1)},n.onsuccess=function(r){var i=n.result.file;i?t(i):console.log("File not found",e)}})}function p(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=m,n.start()}function d(e,t){var n={path:e,file:t},r;a(function(e){var t=e.transaction(["files"],"readwrite"),i=t.objectStore("files");r=i.put(n),r.onerror=function(e){console.log("failed: "+e.target.errorCode)},r.onsuccess=function(e){}})}function v(e,n){var r;if(typeof t[e]!="undefined"){callback(t[e]);return}return r=s.createObjectURL(n),t[e]=r,r}function m(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new fileStorage.Queue(u,6),r=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,i,s=window.URL;const o="fileStorage_db";return{get:c,preload:f,batch:l,getURL:v,save:d}},fileStorage.store=fileStorage.store||{},fileStorage.store.none=function(){function r(e,t){var n={data:null},r=u(e);r?(n.data=r,t(n)):a(e,function(e){n.data=e,t(n)})}function i(e){var t=u(e);t||n.add(e)}function s(e,t){n.addGroup(e,t)}function o(e,t){var r=u(e),i;r?typeof t!="undefined"&&t(r):n.add(e,function(n){typeof t!="undefined"&&t(e)},!0)}function u(t){var n=e[t];return typeof n!="undefined"?n:!1}function a(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(n){f(e,n),typeof t!="undefined"&&t(n)},n.failed=l,n.start()}function f(t,n){if(e[t])return;e[t]=t}function l(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new fileStorage.Queue(r,6);return{get:o,preload:i,batch:s}},fileStorage.store=fileStorage.store||{},fileStorage.store.ram=function(){function i(e,t){var n={data:null},r=a(e);r?(n.data=r,t(n)):f(e,function(e){n.data=e,t(n)})}function s(e){var t=a(e);t||n.add(e)}function o(e,t){n.addGroup(e,t)}function u(e,t){var r=a(e),i;r?(i=c(e,r),typeof t!="undefined"&&t(i)):n.add(e,function(n){i=c(e,n),typeof t!="undefined"&&t(i)},!0)}function a(t){var n=e[t];return typeof n!="undefined"?n:!1}function f(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(n){l(e,n),typeof t!="undefined"&&t(n)},n.failed=h,n.start()}function l(t,n){if(e[t])return;e[t]=n}function c(e,n){var i;return typeof t[e]!="undefined"?t[e]:(i=r.createObjectURL(n),t[e]=i,i)}function h(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new fileStorage.Queue(i,6),r=window.URL||window.webkitURL;return{get:u,preload:s,batch:o}},fileStorage.store=fileStorage.store||{},fileStorage.store.websql=function(){function f(e,t){var n={data:null},r=e;v(e,function(e){m(r,e),t(n)})}function l(e){if(n){e(n);return}n=openDatabase(i,s,o,u);if(!n){console.error("Database error");return}n.transaction(function(t){t.executeSql("CREATE TABLE IF NOT EXISTS "+a+" (path TEXT PRIMARY KEY ASC UNIQUE, file BLOB, type TEXT)"),e&&e(n)})}function c(e){}function h(e,n){t.addGroup(e,n)}function p(n,r){if(typeof e[n]!="undefined"){r(e[n]);return}d(n,function(e){var i;e?(i=g(n,e),typeof r!="undefined"&&r(i)):t.add(n,function(e){i=g(n,e),typeof r!="undefined"&&r(i)},!0)})}function d(e,t){var n={};console.log("path",e),n.onError=function(e,n){console.log("get Error",n),t(!1)},n.onSuccess=function(e,n){var r;n.rows.length&&(r=n.rows.item(0),t(r.file))},l(function(t){t.transaction(function(t){t.executeSql("SELECT * FROM "+a+" WHERE path='"+e+"' LIMIT 1",[],n.onSuccess,n.onError)})})}function v(e,t){var n=new fileStorage.core.loadFile(e,!1,"arraybuffer");n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=y,n.start()}function m(e,t){var n={},r=new FileReader,i;t instanceof Blob||console.log("Not blob"),r.onload=function(r){i=r.target.result,l(function(r){r.transaction(function(r){r.executeSql("REPLACE INTO "+a+" (path, file, type) VALUES (?,?,?)",[e,i,t.type],n.onSuccess,n.onError)})})},r.onerror=function(e){console.log("err",e)},r.readAsDataURL(t),n.onError=function(e,t){console.log("failed: ",t)},n.onSuccess=function(t){console.log("saved",e)}}function g(t,n){var i,s;if(typeof e[t]!="undefined"){callback(e[t]);return}return s=fileStorage.core.dataURLToBlob(n),i=r.createObjectURL(s),e[t]=i,i}function y(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t=new fileStorage.Queue(f,6),n,r=window.URL||window.webkitURL;const i="fileStoragejs_db",s="1",o="cache for files",u=5242880,a="files";return{get:p,preload:c,batch:h,getURL:g,save:m}};
//# sourceMappingURL=fileStorage.min.js.map