diff --git a/src/idb.filesystem.js b/src/idb.filesystem.js index 2140d5b..453778a 100755 --- a/src/idb.filesystem.js +++ b/src/idb.filesystem.js @@ -1,18 +1,18 @@ -/** +/** * Copyright 2013 - Eric Bidelman - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - + * @fileoverview * A polyfill implementation of the HTML5 Filesystem API which sits on top of * IndexedDB as storage layer. Files and folders are stored as FileEntry and @@ -133,7 +133,7 @@ function resolveToFullPath_(cwdFullPath, path) { // Add back in leading slash. if (fullPath[0] != DIR_SEPARATOR) { fullPath = DIR_SEPARATOR + fullPath; - } + } // Replace './' by current dir. ('./one/./two' -> one/two) fullPath = fullPath.replace(/\.\//g, DIR_SEPARATOR); @@ -148,7 +148,7 @@ function resolveToFullPath_(cwdFullPath, path) { if (fullPath[fullPath.length - 1] == DIR_SEPARATOR && fullPath != DIR_SEPARATOR) { fullPath = fullPath.substring(0, fullPath.length - 1); - } + } return fullPath; } @@ -197,7 +197,7 @@ function MyFile(opts) { }.bind(this) }); } -MyFile.prototype.constructor = MyFile; +MyFile.prototype.constructor = MyFile; //MyFile.prototype.slice = Blob.prototype.slice; /** @@ -344,7 +344,7 @@ function DirectoryReader(dirEntry) { }; /** - * Interface supplies information about the state of a file or directory. + * Interface supplies information about the state of a file or directory. * * Modeled from: * dev.w3.org/2009/dap/file-system/file-dir-sys.html#idl-def-Metadata @@ -429,7 +429,7 @@ Entry.prototype = { * Modeled from: * dev.w3.org/2009/dap/file-system/pub/FileSystem/#the-fileentry-interface * - * @param {FileEntry} opt_fileEntry Optional FileEntry to initialize this + * @param {FileEntry} opt_fileEntry Optional FileEntry to initialize this * object from. * @constructor * @extends {Entry} @@ -525,7 +525,7 @@ function DirectoryEntry(opt_folderEntry) { } } DirectoryEntry.prototype = new Entry(); -DirectoryEntry.prototype.constructor = DirectoryEntry; +DirectoryEntry.prototype.constructor = DirectoryEntry; DirectoryEntry.prototype.createReader = function() { return new DirectoryReader(this); }; @@ -555,7 +555,7 @@ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, dirEntry.name = path.split(DIR_SEPARATOR).pop(); // Just need filename. dirEntry.fullPath = path; dirEntry.filesystem = fs_; - + idb_.put(dirEntry, successCallback, opt_errorCallback); } else if (options.create === true && folderEntry) { @@ -598,7 +598,7 @@ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, // IDB won't' save methods, so we need re-create DirectoryEntry. successCallback(new DirectoryEntry(folderEntry)); - } + } }, opt_errorCallback); }; @@ -664,7 +664,7 @@ DirectoryEntry.prototype.getFile = function(path, options, successCallback, // IDB won't' save methods, so we need re-create the FileEntry. successCallback(new FileEntry(fileEntry)); - } + } }, opt_errorCallback); }; @@ -724,7 +724,19 @@ idb_.open = function(dbName, successCallback, opt_errorCallback) { var self = this; // TODO: FF 12.0a1 isn't liking a db name with : in it. - var request = indexedDB.open(dbName.replace(':', '_')/*, 1 /*version*/); + dbName = dbName.replace(':', '_') + + var request; + + try { + // By default we use optimistic storage implement in FF26 + // Bug : https://bugzilla.mozilla.org/show_bug.cgi?id=785884 + request = indexedDB.open(dbName, { version: 1, storage: "temporary" }); + } catch(e){ + // open syntax has changed (from numeric to object). If the browser + // throw an exception, it doesn't accept this optimistic. + request = indexedDB.open(dbName); + } request.onerror = opt_errorCallback || onError; @@ -733,7 +745,7 @@ idb_.open = function(dbName, successCallback, opt_errorCallback) { // console.log('onupgradeneeded: oldVersion:' + e.oldVersion, // 'newVersion:' + e.newVersion); - + self.db = e.target.result; self.db.onerror = onError; @@ -747,7 +759,7 @@ idb_.open = function(dbName, successCallback, opt_errorCallback) { self.db.onerror = onError; successCallback(e); }; - + request.onblocked = opt_errorCallback || onError; }; @@ -820,7 +832,7 @@ idb_.getAllEntries = function(fullPath, successCallback, opt_errorCallback) { results = results.filter(function(val) { var valPartsLen = val.fullPath.split(DIR_SEPARATOR).length; var fullPathPartsLen = fullPath.split(DIR_SEPARATOR).length; - + if (fullPath == DIR_SEPARATOR && valPartsLen < fullPathPartsLen + 1) { // Hack to filter out entries in the root folder. This is inefficient // because reading the entires of fs.root (e.g. '/') returns ALL @@ -895,7 +907,7 @@ function onError(e) { } // Clean up. -// TODO: decide if this is the best place for this. +// TODO: decide if this is the best place for this. exports.addEventListener('beforeunload', function(e) { idb_.db.close(); }, false); diff --git a/src/idb.filesystem.min.js b/src/idb.filesystem.min.js index 4c3a84f..f5c210f 100644 --- a/src/idb.filesystem.min.js +++ b/src/idb.filesystem.min.js @@ -1,16 +1,16 @@ -(function(f){function m(a){var c=a.code,b=a.name;Object.defineProperty(this,"code",{set:function(a){c=a},get:function(){return c}});Object.defineProperty(this,"name",{set:function(a){b=a},get:function(){return b}})}function r(a,c){var b=c;c[0]!=g&&(b=a,b=a!=g?b+(g+c):b+c);for(var b=b.split(g),d=0;dthis.length)c=this.length;0>c&&(c+=this.length);0>c&&(c=0)};this.truncate=function(a){b=b?af&&(f=0);b=new Blob([e,new Uint8Array(f),d,g],{type:b.type})}else b=new Blob([d],{type:d.type});a.file_.blob_=b;a.file_.lastModifiedDate=d.lastModifiedDate||null;h.put(a,function(){c+=d.size;if(this.onwriteend)this.onwriteend()}.bind(this),this.onerror)}}function y(a){var c=!1;this.readEntries=function(b,d){if(!b)throw Error("Expected successCallback argument.");c?b([]):h.getAllEntries(a.fullPath,function(a){c=!0;b(a)},d)}}function t(a,c){this.modificationTime_=a||null;this.size_=c|| +f=c-e.size;0>f&&(f=0);b=new Blob([e,new Uint8Array(f),d,g],{type:b.type})}else b=new Blob([d],{type:d.type});a.file_.blob_=b;a.file_.lastModifiedDate=d.lastModifiedDate||null;h.put(a,function(){c+=d.size;if(this.onwriteend)this.onwriteend()}.bind(this),this.onerror)}}function y(a){var c=!1;this.readEntries=function(b,d){if(!b)throw Error("Expected successCallback argument.");c?b([]):h.getAllEntries(a.fullPath,function(a){c=!0;b(a)},d)}}function u(a,c){this.modificationTime_=a||null;this.size_=c|| 0}function o(){}function j(a){this.file_=null;Object.defineProperty(this,"isFile",{enumerable:!0,get:function(){return!0}});Object.defineProperty(this,"isDirectory",{enumerable:!0,get:function(){return!1}});if(a)this.file_=a.file_,this.name=a.name,this.fullPath=a.fullPath,this.filesystem=a.filesystem}function i(a){Object.defineProperty(this,"isFile",{enumerable:!0,get:function(){return!1}});Object.defineProperty(this,"isDirectory",{enumerable:!0,get:function(){return!0}});if(a)this.name=a.name,this.fullPath= -a.fullPath,this.filesystem=a.filesystem}function z(a){u=a==f.TEMPORARY?"Temporary":"Persistent";this.name=(location.protocol+location.host).replace(/:/g,"_")+":"+u;this.root=new i;this.root.fullPath=g;this.root.filesystem=this;this.root.name=""}function k(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 v= -f.indexedDB||f.mozIndexedDB||f.msIndexedDB;if(v){f.TEMPORARY=0;f.PERSISTENT=1;if(void 0===f.FileError)window.FileError=function(){},FileError.prototype.prototype=Error.prototype;FileError.INVALID_MODIFICATION_ERR=9;FileError.NOT_FOUND_ERR=1;m.prototype=FileError.prototype;m.prototype.toString=Error.prototype.toString;var l=new m({code:FileError.INVALID_MODIFICATION_ERR,name:"INVALID_MODIFICATION_ERR"}),p=new m({code:1E3,name:"Not implemented"}),q=new m({code:FileError.NOT_FOUND_ERR,name:"Not found"}), -n=null,u="temporary",h={db:null},g="/",w=String.fromCharCode(g.charCodeAt(0)+1);s.prototype.constructor=s;t.prototype={get modificationTime(){return this.modificationTime_},get size(){return this.size_}};o.prototype={name:null,fullPath:null,filesystem:null,copyTo:function(){throw p;},getMetadata:function(a,c){if(!a)throw Error("Expected successCallback argument.");try{this.isFile?a(new t(this.file_.lastModifiedDate,this.file_.size)):c(new m({code:1001,name:"getMetadata() not implemented for DirectoryEntry"}))}catch(b){c&& -c(b)}},getParent:function(){throw p;},moveTo:function(){throw p;},remove:function(a,c){if(!a)throw Error("Expected successCallback argument.");h["delete"](this.fullPath,function(){a()},c)},toURL:function(){return"filesystem:"+(location.protocol+"//"+location.host)+g+u.toLowerCase()+this.fullPath}};j.prototype=new o;j.prototype.constructor=j;j.prototype.createWriter=function(a){a(new x(this))};j.prototype.file=function(a,c){if(!a)throw Error("Expected successCallback argument.");if(null==this.file_)if(c)c(q); -else throw q;else{var b=null==this.file_.blob_?this.file_:this.file_.blob_;b.lastModifiedDate=this.file_.lastModifiedDate;a(b)}};i.prototype=new o;i.prototype.constructor=i;i.prototype.createReader=function(){return new y(this)};i.prototype.getDirectory=function(a,c,b,d){a=r(this.fullPath,a);h.get(a,function(e){c||(c={});!0===c.create&&!0===c.exclusive&&e?d&&d(l):!0===c.create&&!e?(e=new i,e.name=a.split(g).pop(),e.fullPath=a,e.filesystem=n,h.put(e,b,d)):!0===c.create&&e?e.isDirectory?b(new i(e)): -d&&d(l):(!c.create||!1===c.create)&&!e?a==g?(e=new i,e.name="",e.fullPath=g,e.filesystem=n,b(e)):d&&d(q):(!c.create||!1===c.create)&&e&&e.isFile?d&&d(l):b(new i(e))},d)};i.prototype.getFile=function(a,c,b,d){a=r(this.fullPath,a);h.get(a,function(e){c||(c={});!0===c.create&&!0===c.exclusive&&e?d&&d(l):!0===c.create&&!e?(e=new j,e.name=a.split(g).pop(),e.fullPath=a,e.filesystem=n,e.file_=new s({size:0,name:e.name,lastModifiedDate:new Date}),h.put(e,b,d)):!0===c.create&&e?e.isFile?b(new j(e)):d&&d(l): -(!c.create||!1===c.create)&&!e?d&&d(q):(!c.create||!1===c.create)&&e&&e.isDirectory?d&&d(l):b(new j(e))},d)};i.prototype.removeRecursively=function(a,c){if(!a)throw Error("Expected successCallback argument.");this.remove(a,c)};h.open=function(a,c,b){var d=this,a=v.open(a.replace(":","_"));a.onerror=b||k;a.onupgradeneeded=function(a){d.db=a.target.result;d.db.onerror=k;d.db.objectStoreNames.contains("entries")||d.db.createObjectStore("entries")};a.onsuccess=function(a){d.db=a.target.result;d.db.onerror= -k;c(a)};a.onblocked=b||k};h.close=function(){this.db.close();this.db=null};h.drop=function(a,c){if(this.db){var b=v.deleteDatabase(this.db.name);b.onsuccess=function(b){a(b)};b.onerror=c||k;h.close()}};h.get=function(a,c,b){if(this.db){var d=this.db.transaction(["entries"],"readonly"),a=IDBKeyRange.bound(a,a+w,!1,!0),e=d.objectStore("entries").get(a);d.onabort=b||k;d.oncomplete=function(){c(e.result)}}};h.getAllEntries=function(a,c,b){if(this.db){var d=[],e=null;a!=g&&(e=IDBKeyRange.bound(a+g,a+w, -!1,!0));var f=this.db.transaction(["entries"],"readonly");f.onabort=b||k;f.oncomplete=function(){d=d.filter(function(b){var c=b.fullPath.split(g).length,d=a.split(g).length;if(a==g&&c