1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00
epub.js/remove/js/libs/fileStorage.min.js
2013-06-04 22:56:02 -07:00

1 line
No EOL
11 KiB
JavaScript

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 e(e){if(d){e(d);return}p(m,v,function(t){d=t,e(t)},f)}function t(e){}function n(e,t){h.addGroup(e,t)}function r(e,t){if(typeof c[e]!="undefined")return c[e];i(e,function(n){var r;n?(r=a(e,n),typeof t!="undefined"&&t(r)):h.add(e,function(n){i(n,function(r){n=a(e,r),typeof t!="undefined"&&t(n)})},!0)})}function i(t,n){var r,i;e(function(e){e.root.getFile(t,{},function(e){n(e)},function(){n(!1)})})}function s(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=f,n.start()}function o(t,n,r){e(function(e){var i=t.split("/").slice(0,-1);u(e.root,i),e.root.getFile(t,{create:!0},function(e){e.createWriter(function(e){e.onwriteend=function(e){r&&r(e)},e.onerror=function(e){f(err)},e.write(n)})},f)})}function u(e,t){if(t[0]=="."||t[0]=="")t=t.slice(1);e.getDirectory(t[0],{create:!0},function(e){t.length&&u(e,t.slice(1))},f)}function a(e,t){var n;return typeof c[e]!="undefined"?c[e]:(n=t.toURL(),c[e]=n,n)}function f(e){typeof this.failed=="undefined"?console.log("Error: ",l(e)):this.failed(e)}function l(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 c={},h=new fileStorage.Queue((fileStorage.filePath||"")+"loader_filesystem.js",6),p=window.requestFileSystem||window.webkitRequestFileSystem,d;const v=5242880,m=TEMPORARY;return{get:r,preload:t,batch:n,getURL:a,save:o}},fileStorage.store=fileStorage.store||{},fileStorage.store.indexeddb=function(){function e(e,t){var n={data:null},r=e;o(e,function(e){u(r,e),t(n)})}function t(e){var t;if(d){e(d);return}t=indexedDB.open(m),t.onsuccess=function(n){d=t.result,d.onerror=function(e){console.log("Database error: "+e.target.errorCode)},e&&e(d)},t.onerror=function(e){},t.onupgradeneeded=function(e){var t=e.target.result,n=t.createObjectStore("files",{keyPath:"path"})}}function n(e){}function r(e,t){h.addGroup(e,t)}function i(e,t){if(typeof c[e]!="undefined"){t(c[e]);return}s(e,function(n){var r;n?(r=a(e,n),typeof t!="undefined"&&t(r)):h.add(e,function(n){r=a(e,n),typeof t!="undefined"&&t(r)},!0)})}function s(e,n){var r,i;t(function(t){i=t.transaction(["files"]).objectStore("files"),r=i.get(e),r.onerror=function(e){console.log("error:",e),n(!1)},r.onsuccess=function(t){var i=r.result.file;i?n(i):console.log("File not found",e)}})}function o(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=f,n.start()}function u(e,n){var r={path:e,file:n},i;t(function(e){var t=e.transaction(["files"],"readwrite"),n=t.objectStore("files");i=n.put(r),i.onerror=function(e){console.log("failed: "+e.target.errorCode)},i.onsuccess=function(e){}})}function a(e,t){var n;if(typeof c[e]!="undefined"){callback(c[e]);return}return n=v.createObjectURL(t),c[e]=n,n}function f(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var l={},c={},h=new fileStorage.Queue(e,6),p=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,d,v=window.URL;const m="fileStorage_db";return{get:i,preload:n,batch:r,getURL:a,save:u}},fileStorage.store=fileStorage.store||{},fileStorage.store.none=function(){function e(e,t){var n={data:null},r=i(e);r?(n.data=r,t(n)):s(e,function(e){n.data=e,t(n)})}function t(e){var t=i(e);t||l.add(e)}function n(e,t){l.addGroup(e,t)}function r(e,t){var n=i(e),r;n?typeof t!="undefined"&&t(n):l.add(e,function(n){typeof t!="undefined"&&t(e)},!0)}function i(e){var t=a[e];return typeof t!="undefined"?t:!1}function s(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(n){o(e,n),typeof t!="undefined"&&t(n)},n.failed=u,n.start()}function o(e,t){if(a[e])return;a[e]=e}function u(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var a={},f={},l=new fileStorage.Queue(e,6);return{get:r,preload:t,batch:n}},fileStorage.store=fileStorage.store||{},fileStorage.store.ram=function(){function e(e,t){var n={data:null},r=i(e);r?(n.data=r,t(n)):s(e,function(e){n.data=e,t(n)})}function t(e){var t=i(e);t||c.add(e)}function n(e,t){c.addGroup(e,t)}function r(e,t){var n=i(e),r;n?(r=u(e,n),typeof t!="undefined"&&t(r)):c.add(e,function(n){r=u(e,n),typeof t!="undefined"&&t(r)},!0)}function i(e){var t=f[e];return typeof t!="undefined"?t:!1}function s(e,t){var n=new fileStorage.core.loadFile(e);n.succeeded=function(n){o(e,n),typeof t!="undefined"&&t(n)},n.failed=a,n.start()}function o(e,t){if(f[e])return;f[e]=t}function u(e,t){var n;return typeof l[e]!="undefined"?l[e]:(n=h.createObjectURL(t),l[e]=n,n)}function a(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var f={},l={},c=new fileStorage.Queue(e,6),h=window.URL||window.webkitURL;return{get:r,preload:t,batch:n}},fileStorage.store=fileStorage.store||{},fileStorage.store.websql=function(){function e(e,t){var n={data:null},r=e;o(e,function(e){u(r,e),t(n)})}function t(e){if(h){e(h);return}h=openDatabase(d,v,m,g);if(!h){console.error("Database error");return}h.transaction(function(t){t.executeSql("CREATE TABLE IF NOT EXISTS "+y+" (path TEXT PRIMARY KEY ASC UNIQUE, file BLOB, type TEXT)"),e&&e(h)})}function n(e){}function r(e,t){c.addGroup(e,t)}function i(e,t){if(typeof l[e]!="undefined"){t(l[e]);return}s(e,function(n){var r;n?(r=a(e,n),typeof t!="undefined"&&t(r)):c.add(e,function(n){r=a(e,n),typeof t!="undefined"&&t(r)},!0)})}function s(e,n){var r={};console.log("path",e),r.onError=function(e,t){console.log("get Error",t),n(!1)},r.onSuccess=function(e,t){var r;t.rows.length&&(r=t.rows.item(0),n(r.file))},t(function(t){t.transaction(function(t){t.executeSql("SELECT * FROM "+y+" WHERE path='"+e+"' LIMIT 1",[],r.onSuccess,r.onError)})})}function o(e,t){var n=new fileStorage.core.loadFile(e,!1,"arraybuffer");n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=f,n.start()}function u(e,n){var r={},i=new FileReader,s;n instanceof Blob||console.log("Not blob"),i.onload=function(i){s=i.target.result,t(function(t){t.transaction(function(t){t.executeSql("REPLACE INTO "+y+" (path, file, type) VALUES (?,?,?)",[e,s,n.type],r.onSuccess,r.onError)})})},i.onerror=function(e){console.log("err",e)},i.readAsDataURL(n),r.onError=function(e,t){console.log("failed: ",t)},r.onSuccess=function(t){console.log("saved",e)}}function a(e,t){var n,r;if(typeof l[e]!="undefined"){callback(l[e]);return}return r=fileStorage.core.dataURLToBlob(t),n=p.createObjectURL(r),l[e]=n,n}function f(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var l={},c=new fileStorage.Queue(e,6),h,p=window.URL||window.webkitURL;const d="fileStoragejs_db",v="1",m="cache for files",g=5242880,y="files";return{get:i,preload:n,batch:r,getURL:a,save:u}};