cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgreb...@apache.org
Subject [1/2] cordova-plugin-file git commit: CB-10419 cordova-plugin-file 4.0.0 error with browserify workflow
Date Tue, 26 Jan 2016 13:19:28 GMT
Repository: cordova-plugin-file
Updated Branches:
  refs/heads/master 563dcaf7b -> 79956e4c3


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/79956e4c/www/browser/Preparing.js
----------------------------------------------------------------------
diff --git a/www/browser/Preparing.js b/www/browser/Preparing.js
index f302ee9..2f94c70 100644
--- a/www/browser/Preparing.js
+++ b/www/browser/Preparing.js
@@ -19,168 +19,170 @@
  *
 */
 
-/*global require*/
-
-//Only Chrome uses this file.
-if (!require('./isChrome')()) {
-    return;
-}
-
-var channel = require('cordova/channel');
-var FileError = require('./FileError');
-var PERSISTENT_FS_QUOTA = 5 * 1024 * 1024;
-var filePluginIsReadyEvent = new Event('filePluginIsReady');
-
-var entryFunctionsCreated = false;
-var quotaWasRequested = false;
-var eventWasThrown = false;
-
-if (!window.requestFileSystem) {
-    window.requestFileSystem = function(type, size, win, fail) {
-        if (fail) {
-            fail("Not supported");
-        }
-    };
-} else {
-    window.requestFileSystem(window.TEMPORARY, 1, createFileEntryFunctions, function() {});
-}
+(function () {
+    /*global require*/
 
-if (!window.resolveLocalFileSystemURL) {
-    window.resolveLocalFileSystemURL = function(url, win, fail) {
-        if(fail) {
-            fail("Not supported");
-        }
-    };
-}
-
-// Resolves a filesystem entry by its path - which is passed either in standard (filesystem:file://)
or
-// Cordova-specific (cdvfile://) universal way.
-// Aligns with specification: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#widl-LocalFileSystem-resolveLocalFileSystemURL
-var nativeResolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
-window.resolveLocalFileSystemURL = function(url, win, fail) {
-    /* If url starts with `cdvfile` then we need convert it to Chrome real url first:
-      cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
*/
-    if (url.trim().substr(0,7) === "cdvfile") {
-        /* Quirk:
-        Plugin supports cdvfile://localhost (local resources) only.
-        I.e. external resources are not supported via cdvfile. */
-        if (url.indexOf("cdvfile://localhost") !== -1) {
-            // Browser supports temporary and persistent only
-            var indexPersistent = url.indexOf('persistent');
-            var indexTemporary = url.indexOf('temporary');
-
-            /* Chrome urls start with 'filesystem:' prefix. See quirk:
-               toURL function in Chrome returns filesystem:-prefixed path depending on application
host.
-               For example, filesystem:file:///persistent/somefile.txt,
-               filesystem:http://localhost:8080/persistent/somefile.txt. */
-            var prefix = 'filesystem:file:///';
-            if (location.protocol !== 'file:') {
-                prefix = 'filesystem:' + location.origin + '/';
-            }
+    //Only Chrome uses this file.
+    if (!require('./isChrome')()) {
+        return;
+    }
 
-            var result;
-            if (indexPersistent !== -1) {
-                // cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
-                // or filesystem:http://localhost:8080/persistent/path/to/file
-                result =  prefix + 'persistent' + url.substr(indexPersistent + 10);
-                nativeResolveLocalFileSystemURL(result, win, fail);
-                return;
-            }
+    var channel = require('cordova/channel');
+    var FileError = require('./FileError');
+    var PERSISTENT_FS_QUOTA = 5 * 1024 * 1024;
+    var filePluginIsReadyEvent = new Event('filePluginIsReady');
 
-            if (indexTemporary !== -1) {
-                // cdvfile://localhost/temporary/path/to/file -> filesystem:file://temporary/path/to/file
-                // or filesystem:http://localhost:8080/temporary/path/to/file
-                result = prefix + 'temporary' + url.substr(indexTemporary + 9);
-                nativeResolveLocalFileSystemURL(result, win, fail);
-                return;
-            }
-        }
+    var entryFunctionsCreated = false;
+    var quotaWasRequested = false;
+    var eventWasThrown = false;
 
-        // cdvfile other than local file resource is not supported
-        fail && fail(new FileError(FileError.ENCODING_ERR));
-    } else {
-        nativeResolveLocalFileSystemURL(url, win, fail);
-    }
-};
-
-function createFileEntryFunctions(fs) {
-    fs.root.getFile('todelete_658674_833_4_cdv', {create: true}, function(fileEntry) {
-        var fileEntryType = Object.getPrototypeOf(fileEntry);
-        var entryType = Object.getPrototypeOf(fileEntryType);
-
-        // Save the original method
-        var origToURL = entryType.toURL;
-        entryType.toURL = function () {
-            var origURL = origToURL.call(this);
-            if (this.isDirectory && origURL.substr(-1) !== '/') {
-                return origURL + '/';
+    if (!window.requestFileSystem) {
+        window.requestFileSystem = function(type, size, win, fail) {
+            if (fail) {
+                fail("Not supported");
             }
-            return origURL;
         };
+    } else {
+        window.requestFileSystem(window.TEMPORARY, 1, createFileEntryFunctions, function()
{});
+    }
 
-        entryType.toNativeURL = function () {
-            console.warn("DEPRECATED: Update your code to use 'toURL'");
-            return this.toURL();
+    if (!window.resolveLocalFileSystemURL) {
+        window.resolveLocalFileSystemURL = function(url, win, fail) {
+            if(fail) {
+                fail("Not supported");
+            }
         };
+    }
 
-        entryType.toInternalURL = function() {
-            if (this.toURL().indexOf("persistent") > -1) {
-                return "cdvfile://localhost/persistent" + this.fullPath;
+    // Resolves a filesystem entry by its path - which is passed either in standard (filesystem:file://)
or
+    // Cordova-specific (cdvfile://) universal way.
+    // Aligns with specification: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#widl-LocalFileSystem-resolveLocalFileSystemURL
+    var nativeResolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
+    window.resolveLocalFileSystemURL = function(url, win, fail) {
+        /* If url starts with `cdvfile` then we need convert it to Chrome real url first:
+          cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
*/
+        if (url.trim().substr(0,7) === "cdvfile") {
+            /* Quirk:
+            Plugin supports cdvfile://localhost (local resources) only.
+            I.e. external resources are not supported via cdvfile. */
+            if (url.indexOf("cdvfile://localhost") !== -1) {
+                // Browser supports temporary and persistent only
+                var indexPersistent = url.indexOf('persistent');
+                var indexTemporary = url.indexOf('temporary');
+
+                /* Chrome urls start with 'filesystem:' prefix. See quirk:
+                   toURL function in Chrome returns filesystem:-prefixed path depending on
application host.
+                   For example, filesystem:file:///persistent/somefile.txt,
+                   filesystem:http://localhost:8080/persistent/somefile.txt. */
+                var prefix = 'filesystem:file:///';
+                if (location.protocol !== 'file:') {
+                    prefix = 'filesystem:' + location.origin + '/';
+                }
+
+                var result;
+                if (indexPersistent !== -1) {
+                    // cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
+                    // or filesystem:http://localhost:8080/persistent/path/to/file
+                    result =  prefix + 'persistent' + url.substr(indexPersistent + 10);
+                    nativeResolveLocalFileSystemURL(result, win, fail);
+                    return;
+                }
+
+                if (indexTemporary !== -1) {
+                    // cdvfile://localhost/temporary/path/to/file -> filesystem:file://temporary/path/to/file
+                    // or filesystem:http://localhost:8080/temporary/path/to/file
+                    result = prefix + 'temporary' + url.substr(indexTemporary + 9);
+                    nativeResolveLocalFileSystemURL(result, win, fail);
+                    return;
+                }
             }
 
-            if (this.toURL().indexOf("temporary") > -1) {
-                return "cdvfile://localhost/temporary" + this.fullPath;
-            }
-        };
+            // cdvfile other than local file resource is not supported
+            fail && fail(new FileError(FileError.ENCODING_ERR));
+        } else {
+            nativeResolveLocalFileSystemURL(url, win, fail);
+        }
+    };
 
-        entryType.setMetadata = function(win, fail /*, metadata*/) {
-            fail && fail("Not supported");
-        };
+    function createFileEntryFunctions(fs) {
+        fs.root.getFile('todelete_658674_833_4_cdv', {create: true}, function(fileEntry)
{
+            var fileEntryType = Object.getPrototypeOf(fileEntry);
+            var entryType = Object.getPrototypeOf(fileEntryType);
+
+            // Save the original method
+            var origToURL = entryType.toURL;
+            entryType.toURL = function () {
+                var origURL = origToURL.call(this);
+                if (this.isDirectory && origURL.substr(-1) !== '/') {
+                    return origURL + '/';
+                }
+                return origURL;
+            };
 
-        fileEntry.createWriter(function(writer) {
-            var originalWrite = writer.write;
-            var writerProto = Object.getPrototypeOf(writer);
-            writerProto.write = function(blob) {
-                if(blob instanceof Blob) {
-                    originalWrite.apply(this, [blob]);
-                } else {
-                    var realBlob = new Blob([blob]);
-                    originalWrite.apply(this, [realBlob]);
-               }
+            entryType.toNativeURL = function () {
+                console.warn("DEPRECATED: Update your code to use 'toURL'");
+                return this.toURL();
             };
 
-            fileEntry.remove(function(){ entryFunctionsCreated = true; }, function(){ /*
empty callback */ });
-      });
-    });
-}
+            entryType.toInternalURL = function() {
+                if (this.toURL().indexOf("persistent") > -1) {
+                    return "cdvfile://localhost/persistent" + this.fullPath;
+                }
 
-window.initPersistentFileSystem = function(size, win, fail) {
-    if (navigator.webkitPersistentStorage) {
-        navigator.webkitPersistentStorage.requestQuota(size, win, fail);
-        return;
+                if (this.toURL().indexOf("temporary") > -1) {
+                    return "cdvfile://localhost/temporary" + this.fullPath;
+                }
+            };
+
+            entryType.setMetadata = function(win, fail /*, metadata*/) {
+                fail && fail("Not supported");
+            };
+
+            fileEntry.createWriter(function(writer) {
+                var originalWrite = writer.write;
+                var writerProto = Object.getPrototypeOf(writer);
+                writerProto.write = function(blob) {
+                    if(blob instanceof Blob) {
+                        originalWrite.apply(this, [blob]);
+                    } else {
+                        var realBlob = new Blob([blob]);
+                        originalWrite.apply(this, [realBlob]);
+                   }
+                };
+
+                fileEntry.remove(function(){ entryFunctionsCreated = true; }, function(){
/* empty callback */ });
+          });
+        });
     }
 
-    fail("This browser does not support this function");
-};
+    window.initPersistentFileSystem = function(size, win, fail) {
+        if (navigator.webkitPersistentStorage) {
+            navigator.webkitPersistentStorage.requestQuota(size, win, fail);
+            return;
+        }
 
-window.isFilePluginReadyRaised = function () { return eventWasThrown; };
+        fail("This browser does not support this function");
+    };
 
-window.initPersistentFileSystem(PERSISTENT_FS_QUOTA, function() {
-    console.log('Persistent fs quota granted');
-    quotaWasRequested = true;
-}, function(e){
-    console.log('Error occured while trying to request Persistent fs quota: ' + JSON.stringify(e));
-});
+    window.isFilePluginReadyRaised = function () { return eventWasThrown; };
 
-channel.onCordovaReady.subscribe(function () {
-    function dispatchEventIfReady() {
-        if (entryFunctionsCreated && quotaWasRequested) {
-            window.dispatchEvent(filePluginIsReadyEvent);
-            eventWasThrown = true;
-        } else {
-            setTimeout(dispatchEventIfReady, 100);
+    window.initPersistentFileSystem(PERSISTENT_FS_QUOTA, function() {
+        console.log('Persistent fs quota granted');
+        quotaWasRequested = true;
+    }, function(e){
+        console.log('Error occured while trying to request Persistent fs quota: ' + JSON.stringify(e));
+    });
+
+    channel.onCordovaReady.subscribe(function () {
+        function dispatchEventIfReady() {
+            if (entryFunctionsCreated && quotaWasRequested) {
+                window.dispatchEvent(filePluginIsReadyEvent);
+                eventWasThrown = true;
+            } else {
+                setTimeout(dispatchEventIfReady, 100);
+            }
         }
-    }
 
-    dispatchEventIfReady();
-}, false);
+        dispatchEventIfReady();
+    }, false);
+})();

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/79956e4c/www/requestFileSystem.js
----------------------------------------------------------------------
diff --git a/www/requestFileSystem.js b/www/requestFileSystem.js
index 098047c..1c6c5f4 100644
--- a/www/requestFileSystem.js
+++ b/www/requestFileSystem.js
@@ -18,61 +18,62 @@
  * under the License.
  *
 */
-
-//For browser platform: not all browsers use this file.
-function checkBrowser() {
-    if (cordova.platformId === "browser" && require('./isChrome')()) {
-        module.exports = window.requestFileSystem || window.webkitRequestFileSystem;
-        return true;
+(function() {
+    //For browser platform: not all browsers use this file.
+    function checkBrowser() {
+        if (cordova.platformId === "browser" && require('./isChrome')()) {
+            module.exports = window.requestFileSystem || window.webkitRequestFileSystem;
+            return true;
+        }
+        return false;
+    }
+    if (checkBrowser()) {
+        return;
     }
-    return false;
-}
-if (checkBrowser()) {
-    return;
-}
 
-var argscheck = require('cordova/argscheck'),
-    FileError = require('./FileError'),
-    FileSystem = require('./FileSystem'),
-    exec = require('cordova/exec');
-var fileSystems = require('./fileSystems');
+    var argscheck = require('cordova/argscheck'),
+        FileError = require('./FileError'),
+        FileSystem = require('./FileSystem'),
+        exec = require('cordova/exec');
+    var fileSystems = require('./fileSystems');
 
-/**
- * Request a file system in which to store application data.
- * @param type  local file system type
- * @param size  indicates how much storage space, in bytes, the application expects to need
- * @param successCallback  invoked with a FileSystem object
- * @param errorCallback  invoked if error occurs retrieving file system
- */
-var requestFileSystem = function(type, size, successCallback, errorCallback) {
-    argscheck.checkArgs('nnFF', 'requestFileSystem', arguments);
-    var fail = function(code) {
-        errorCallback && errorCallback(new FileError(code));
-    };
+    /**
+     * Request a file system in which to store application data.
+     * @param type  local file system type
+     * @param size  indicates how much storage space, in bytes, the application expects to
need
+     * @param successCallback  invoked with a FileSystem object
+     * @param errorCallback  invoked if error occurs retrieving file system
+     */
+    var requestFileSystem = function(type, size, successCallback, errorCallback) {
+        argscheck.checkArgs('nnFF', 'requestFileSystem', arguments);
+        var fail = function(code) {
+            errorCallback && errorCallback(new FileError(code));
+        };
 
-    if (type < 0) {
-        fail(FileError.SYNTAX_ERR);
-    } else {
-        // if successful, return a FileSystem object
-        var success = function(file_system) {
-            if (file_system) {
-                if (successCallback) {
-                    fileSystems.getFs(file_system.name, function(fs) {
-                        // This should happen only on platforms that haven't implemented
requestAllFileSystems (windows)
-                        if (!fs) {
-                            fs = new FileSystem(file_system.name, file_system.root);
-                        }
-                        successCallback(fs);
-                    });
+        if (type < 0) {
+            fail(FileError.SYNTAX_ERR);
+        } else {
+            // if successful, return a FileSystem object
+            var success = function(file_system) {
+                if (file_system) {
+                    if (successCallback) {
+                        fileSystems.getFs(file_system.name, function(fs) {
+                            // This should happen only on platforms that haven't implemented
requestAllFileSystems (windows)
+                            if (!fs) {
+                                fs = new FileSystem(file_system.name, file_system.root);
+                            }
+                            successCallback(fs);
+                        });
+                    }
                 }
-            }
-            else {
-                // no FileSystem object returned
-                fail(FileError.NOT_FOUND_ERR);
-            }
-        };
-        exec(success, fail, "File", "requestFileSystem", [type, size]);
-    }
-};
+                else {
+                    // no FileSystem object returned
+                    fail(FileError.NOT_FOUND_ERR);
+                }
+            };
+            exec(success, fail, "File", "requestFileSystem", [type, size]);
+        }
+    };
 
-module.exports = requestFileSystem;
+    module.exports = requestFileSystem;
+})();

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/79956e4c/www/resolveLocalFileSystemURI.js
----------------------------------------------------------------------
diff --git a/www/resolveLocalFileSystemURI.js b/www/resolveLocalFileSystemURI.js
index 633c1d2..4761222 100644
--- a/www/resolveLocalFileSystemURI.js
+++ b/www/resolveLocalFileSystemURI.js
@@ -18,72 +18,73 @@
  * under the License.
  *
 */
-
-//For browser platform: not all browsers use overrided `resolveLocalFileSystemURL`.
-function checkBrowser() {
-    if (cordova.platformId === "browser" && require('./isChrome')()) {
-        module.exports.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
-        return true;
+(function() {
+    //For browser platform: not all browsers use overrided `resolveLocalFileSystemURL`.
+    function checkBrowser() {
+        if (cordova.platformId === "browser" && require('./isChrome')()) {
+            module.exports.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
window.webkitResolveLocalFileSystemURL;
+            return true;
+        }
+        return false;
     }
-    return false;
-}
-if (checkBrowser()) {
-    return;
-}
-
-var argscheck = require('cordova/argscheck'),
-    DirectoryEntry = require('./DirectoryEntry'),
-    FileEntry = require('./FileEntry'),
-    FileError = require('./FileError'),
-    exec = require('cordova/exec');
-var fileSystems = require('./fileSystems');
-
-/**
- * Look up file system Entry referred to by local URI.
- * @param {DOMString} uri  URI referring to a local file or directory
- * @param successCallback  invoked with Entry object corresponding to URI
- * @param errorCallback    invoked if error occurs retrieving file system entry
- */
-module.exports.resolveLocalFileSystemURL = module.exports.resolveLocalFileSystemURL || function(uri,
successCallback, errorCallback) {
-    argscheck.checkArgs('sFF', 'resolveLocalFileSystemURI', arguments);
-    // error callback
-    var fail = function(error) {
-        errorCallback && errorCallback(new FileError(error));
-    };
-    // sanity check for 'not:valid:filename' or '/not:valid:filename'
-    // file.spec.12 window.resolveLocalFileSystemURI should error (ENCODING_ERR) when resolving
invalid URI with leading /.
-    if(!uri || uri.split(":").length > 2) {
-        setTimeout( function() {
-            fail(FileError.ENCODING_ERR);
-        },0);
+    if (checkBrowser()) {
         return;
     }
-    // if successful, return either a file or directory entry
-    var success = function(entry) {
-        if (entry) {
-            if (successCallback) {
-                // create appropriate Entry object
-                var fsName = entry.filesystemName || (entry.filesystem && entry.filesystem.name)
|| (entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary');
-                fileSystems.getFs(fsName, function(fs) {
-                    // This should happen only on platforms that haven't implemented requestAllFileSystems
(windows)
-                    if (!fs) {
-                        fs = new FileSystem(fsName, {name:"", fullPath:"/"});
-                    }
-                    var result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath,
fs, entry.nativeURL) : new FileEntry(entry.name, entry.fullPath, fs, entry.nativeURL);
-                    successCallback(result);
-                });
-            }
-        }
-        else {
-            // no Entry object returned
-            fail(FileError.NOT_FOUND_ERR);
+
+    var argscheck = require('cordova/argscheck'),
+        DirectoryEntry = require('./DirectoryEntry'),
+        FileEntry = require('./FileEntry'),
+        FileError = require('./FileError'),
+        exec = require('cordova/exec');
+    var fileSystems = require('./fileSystems');
+
+    /**
+     * Look up file system Entry referred to by local URI.
+     * @param {DOMString} uri  URI referring to a local file or directory
+     * @param successCallback  invoked with Entry object corresponding to URI
+     * @param errorCallback    invoked if error occurs retrieving file system entry
+     */
+    module.exports.resolveLocalFileSystemURL = module.exports.resolveLocalFileSystemURL ||
function(uri, successCallback, errorCallback) {
+        argscheck.checkArgs('sFF', 'resolveLocalFileSystemURI', arguments);
+        // error callback
+        var fail = function(error) {
+            errorCallback && errorCallback(new FileError(error));
+        };
+        // sanity check for 'not:valid:filename' or '/not:valid:filename'
+        // file.spec.12 window.resolveLocalFileSystemURI should error (ENCODING_ERR) when
resolving invalid URI with leading /.
+        if(!uri || uri.split(":").length > 2) {
+            setTimeout( function() {
+                fail(FileError.ENCODING_ERR);
+            },0);
+            return;
         }
-    };
+        // if successful, return either a file or directory entry
+        var success = function(entry) {
+            if (entry) {
+                if (successCallback) {
+                    // create appropriate Entry object
+                    var fsName = entry.filesystemName || (entry.filesystem && entry.filesystem.name)
|| (entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary');
+                    fileSystems.getFs(fsName, function(fs) {
+                        // This should happen only on platforms that haven't implemented
requestAllFileSystems (windows)
+                        if (!fs) {
+                            fs = new FileSystem(fsName, {name:"", fullPath:"/"});
+                        }
+                        var result = (entry.isDirectory) ? new DirectoryEntry(entry.name,
entry.fullPath, fs, entry.nativeURL) : new FileEntry(entry.name, entry.fullPath, fs, entry.nativeURL);
+                        successCallback(result);
+                    });
+                }
+            }
+            else {
+                // no Entry object returned
+                fail(FileError.NOT_FOUND_ERR);
+            }
+        };
 
-    exec(success, fail, "File", "resolveLocalFileSystemURI", [uri]);
-};
+        exec(success, fail, "File", "resolveLocalFileSystemURI", [uri]);
+    };
 
-module.exports.resolveLocalFileSystemURI = function() {
-    console.log("resolveLocalFileSystemURI is deprecated. Please call resolveLocalFileSystemURL
instead.");
-    module.exports.resolveLocalFileSystemURL.apply(this, arguments);
-};
+    module.exports.resolveLocalFileSystemURI = function() {
+        console.log("resolveLocalFileSystemURI is deprecated. Please call resolveLocalFileSystemURL
instead.");
+        module.exports.resolveLocalFileSystemURL.apply(this, arguments);
+    };
+})();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message