cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rkn...@apache.org
Subject cordova-plugin-file git commit: CB-10577: Android resolveLocalFileSystemURL should detect directory vs file. This closes #167
Date Tue, 08 Mar 2016 22:50:02 GMT
Repository: cordova-plugin-file
Updated Branches:
  refs/heads/master 68e40437f -> 3e09b31fd


CB-10577: Android resolveLocalFileSystemURL should detect directory vs file. This closes #167


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/3e09b31f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/3e09b31f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/3e09b31f

Branch: refs/heads/master
Commit: 3e09b31fd7530e02a2f70ea3d7b8d53cd99941cc
Parents: 68e4043
Author: Jason Ginchereau <jasongin@microsoft.com>
Authored: Fri Mar 4 12:02:10 2016 -0800
Committer: riknoll <richard.b.knoll@gmail.com>
Committed: Tue Mar 8 14:47:58 2016 -0800

----------------------------------------------------------------------
 src/android/FileUtils.java       | 32 ++++++++++++++++----------
 src/android/LocalFilesystem.java |  2 +-
 tests/tests.js                   | 42 ++++++++++++++++++++++++++++++++++-
 3 files changed, 62 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3e09b31f/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index e9c937f..aa81fa0 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -659,27 +659,35 @@ public class FileUtils extends CordovaPlugin {
      * @throws JSONException
      */
     private JSONObject resolveLocalFileSystemURI(String uriString) throws IOException, JSONException
{
-    	if (uriString == null) {
-    		throw new MalformedURLException("Unrecognized filesystem URL");
-    	}
-    	Uri uri = Uri.parse(uriString);
+        if (uriString == null) {
+            throw new MalformedURLException("Unrecognized filesystem URL");
+        }
+        Uri uri = Uri.parse(uriString);
+        boolean isNativeUri = false;
 
         LocalFilesystemURL inputURL = LocalFilesystemURL.parse(uri);
         if (inputURL == null) {
-    		/* Check for file://, content:// urls */
-    		inputURL = resolveNativeUri(uri);
-    	}
+            /* Check for file://, content:// urls */
+            inputURL = resolveNativeUri(uri);
+            isNativeUri = true;
+        }
 
         try {
-        	Filesystem fs = this.filesystemForURL(inputURL);
-        	if (fs == null) {
-        		throw new MalformedURLException("No installed handlers for this URL");
-        	}
+            Filesystem fs = this.filesystemForURL(inputURL);
+            if (fs == null) {
+                throw new MalformedURLException("No installed handlers for this URL");
+            }
             if (fs.exists(inputURL)) {
+                if (!isNativeUri) {
+                    // If not already resolved as native URI, resolve to a native URI and
back to
+                    // fix the terminating slash based on whether the entry is a directory
or file.
+                    inputURL = fs.toLocalUri(fs.toNativeUri(inputURL));
+                }
+
                 return fs.getEntryForLocalURL(inputURL);
             }
         } catch (IllegalArgumentException e) {
-        	throw new MalformedURLException("Unrecognized filesystem URL");
+            throw new MalformedURLException("Unrecognized filesystem URL");
         }
         throw new FileNotFoundException();
     }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3e09b31f/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 153d8e3..9325fab 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -93,7 +93,7 @@ public class LocalFilesystem extends Filesystem {
         if (!subPath.isEmpty()) {
             b.appendEncodedPath(subPath);
         }
-        if (f.isDirectory() || inputURL.getPath().endsWith("/")) {
+        if (f.isDirectory()) {
             // Add trailing / for directories.
             b.appendEncodedPath("");
         }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3e09b31f/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 29279de..3cdca3e 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -301,6 +301,8 @@ exports.defineAutoTests = function () {
                     var fileName = 'file.spec.9';
                     var win = function (fileEntry) {
                         expect(fileEntry).toBeDefined();
+                        expect(fileEntry.isFile).toBe(true);
+                        expect(fileEntry.isDirectory).toBe(false);
                         expect(fileEntry.name).toCanonicallyMatch(fileName);
                         expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use
cdvfile URL');
                         expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end
with a slash');
@@ -311,10 +313,29 @@ exports.defineAutoTests = function () {
                         window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null,
done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
                     }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName),
failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
                 });
+                it("file.spec.9.1 should resolve a file even with a terminating slash", function
(done) {
+                    var fileName = 'file.spec.9.1';
+                    var win = function (fileEntry) {
+                        expect(fileEntry).toBeDefined();
+                        expect(fileEntry.isFile).toBe(true);
+                        expect(fileEntry.isDirectory).toBe(false);
+                        expect(fileEntry.name).toCanonicallyMatch(fileName);
+                        expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use
cdvfile URL');
+                        expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end
with a slash');
+                        // Clean-up
+                        deleteEntry(fileName, done);
+                    };
+                    createFile(fileName, function (entry) {
+                        var entryURL = entry.toURL() + '/';
+                        window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null,
done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
+                    }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName),
failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
+                });
                 it("file.spec.9.5 should resolve a directory", function (done) {
                     var fileName = 'file.spec.9.5';
                     var win = function (fileEntry) {
                         expect(fileEntry).toBeDefined();
+                        expect(fileEntry.isFile).toBe(false);
+                        expect(fileEntry.isDirectory).toBe(true);
                         expect(fileEntry.name).toCanonicallyMatch(fileName);
                         expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use
cdvfile URL');
                         expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
@@ -327,6 +348,26 @@ exports.defineAutoTests = function () {
                     }
                     createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory
- Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error
creating directory: ' + fileName));
                 });
+                it("file.spec.9.6 should resolve a directory even without a terminating slash",
function (done) {
+                    var fileName = 'file.spec.9.6';
+                    var win = function (fileEntry) {
+                        expect(fileEntry).toBeDefined();
+                        expect(fileEntry.isFile).toBe(false);
+                        expect(fileEntry.isDirectory).toBe(true);
+                        expect(fileEntry.name).toCanonicallyMatch(fileName);
+                        expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use
cdvfile URL');
+                        expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
+                        // cleanup
+                        deleteEntry(fileName, done);
+                    };
+                    function gotDirectory(entry) {
+                        // lookup file system entry
+                        var entryURL = entry.toURL();
+                        entryURL = entryURL.substring(0, entryURL.length - 1);
+                        window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null,
done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
+                    }
+                    createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory
- Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error
creating directory: ' + fileName));
+                });
                 it("file.spec.10 resolve valid file name with parameters", function (done)
{
                     var fileName = "resolve.file.uri.params",
                     win = function (fileEntry) {
@@ -545,7 +586,6 @@ exports.defineAutoTests = function () {
 
                 var fileName = "de:invalid:path",
                 fail = function (error) {
-                    console.error(error);
                     expect(error).toBeDefined();
                     expect(error).toBeFileError(FileError.ENCODING_ERR);
                     done();


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


Mime
View raw message