Skip to content
Permalink
Browse files
CB-10577: Android resolveLocalFileSystemURL should detect directory v…
…s file. This closes #167
  • Loading branch information
jasongin authored and riknoll committed Mar 8, 2016
1 parent 68e4043 commit 3e09b31fd7530e02a2f70ea3d7b8d53cd99941cc
Showing 3 changed files with 62 additions and 14 deletions.
@@ -659,27 +659,35 @@ public void run() {
* @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();
}
@@ -93,7 +93,7 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) {
if (!subPath.isEmpty()) {
b.appendEncodedPath(subPath);
}
if (f.isDirectory() || inputURL.getPath().endsWith("/")) {
if (f.isDirectory()) {
// Add trailing / for directories.
b.appendEncodedPath("");
}
@@ -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();

0 comments on commit 3e09b31

Please sign in to comment.