Skip to content

Commit 3e09b31

Browse files
jasonginriknoll
authored andcommitted
CB-10577: Android resolveLocalFileSystemURL should detect directory vs file. This closes apache#167
1 parent 68e4043 commit 3e09b31

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

src/android/FileUtils.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -659,27 +659,35 @@ public void run() {
659659
* @throws JSONException
660660
*/
661661
private JSONObject resolveLocalFileSystemURI(String uriString) throws IOException, JSONException {
662-
if (uriString == null) {
663-
throw new MalformedURLException("Unrecognized filesystem URL");
664-
}
665-
Uri uri = Uri.parse(uriString);
662+
if (uriString == null) {
663+
throw new MalformedURLException("Unrecognized filesystem URL");
664+
}
665+
Uri uri = Uri.parse(uriString);
666+
boolean isNativeUri = false;
666667

667668
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(uri);
668669
if (inputURL == null) {
669-
/* Check for file://, content:// urls */
670-
inputURL = resolveNativeUri(uri);
671-
}
670+
/* Check for file://, content:// urls */
671+
inputURL = resolveNativeUri(uri);
672+
isNativeUri = true;
673+
}
672674

673675
try {
674-
Filesystem fs = this.filesystemForURL(inputURL);
675-
if (fs == null) {
676-
throw new MalformedURLException("No installed handlers for this URL");
677-
}
676+
Filesystem fs = this.filesystemForURL(inputURL);
677+
if (fs == null) {
678+
throw new MalformedURLException("No installed handlers for this URL");
679+
}
678680
if (fs.exists(inputURL)) {
681+
if (!isNativeUri) {
682+
// If not already resolved as native URI, resolve to a native URI and back to
683+
// fix the terminating slash based on whether the entry is a directory or file.
684+
inputURL = fs.toLocalUri(fs.toNativeUri(inputURL));
685+
}
686+
679687
return fs.getEntryForLocalURL(inputURL);
680688
}
681689
} catch (IllegalArgumentException e) {
682-
throw new MalformedURLException("Unrecognized filesystem URL");
690+
throw new MalformedURLException("Unrecognized filesystem URL");
683691
}
684692
throw new FileNotFoundException();
685693
}

src/android/LocalFilesystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) {
9393
if (!subPath.isEmpty()) {
9494
b.appendEncodedPath(subPath);
9595
}
96-
if (f.isDirectory() || inputURL.getPath().endsWith("/")) {
96+
if (f.isDirectory()) {
9797
// Add trailing / for directories.
9898
b.appendEncodedPath("");
9999
}

tests/tests.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ exports.defineAutoTests = function () {
301301
var fileName = 'file.spec.9';
302302
var win = function (fileEntry) {
303303
expect(fileEntry).toBeDefined();
304+
expect(fileEntry.isFile).toBe(true);
305+
expect(fileEntry.isDirectory).toBe(false);
304306
expect(fileEntry.name).toCanonicallyMatch(fileName);
305307
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
306308
expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
@@ -311,10 +313,29 @@ exports.defineAutoTests = function () {
311313
window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
312314
}, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
313315
});
316+
it("file.spec.9.1 should resolve a file even with a terminating slash", function (done) {
317+
var fileName = 'file.spec.9.1';
318+
var win = function (fileEntry) {
319+
expect(fileEntry).toBeDefined();
320+
expect(fileEntry.isFile).toBe(true);
321+
expect(fileEntry.isDirectory).toBe(false);
322+
expect(fileEntry.name).toCanonicallyMatch(fileName);
323+
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
324+
expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
325+
// Clean-up
326+
deleteEntry(fileName, done);
327+
};
328+
createFile(fileName, function (entry) {
329+
var entryURL = entry.toURL() + '/';
330+
window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
331+
}, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
332+
});
314333
it("file.spec.9.5 should resolve a directory", function (done) {
315334
var fileName = 'file.spec.9.5';
316335
var win = function (fileEntry) {
317336
expect(fileEntry).toBeDefined();
337+
expect(fileEntry.isFile).toBe(false);
338+
expect(fileEntry.isDirectory).toBe(true);
318339
expect(fileEntry.name).toCanonicallyMatch(fileName);
319340
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
320341
expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
@@ -327,6 +348,26 @@ exports.defineAutoTests = function () {
327348
}
328349
createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
329350
});
351+
it("file.spec.9.6 should resolve a directory even without a terminating slash", function (done) {
352+
var fileName = 'file.spec.9.6';
353+
var win = function (fileEntry) {
354+
expect(fileEntry).toBeDefined();
355+
expect(fileEntry.isFile).toBe(false);
356+
expect(fileEntry.isDirectory).toBe(true);
357+
expect(fileEntry.name).toCanonicallyMatch(fileName);
358+
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
359+
expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
360+
// cleanup
361+
deleteEntry(fileName, done);
362+
};
363+
function gotDirectory(entry) {
364+
// lookup file system entry
365+
var entryURL = entry.toURL();
366+
entryURL = entryURL.substring(0, entryURL.length - 1);
367+
window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
368+
}
369+
createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
370+
});
330371
it("file.spec.10 resolve valid file name with parameters", function (done) {
331372
var fileName = "resolve.file.uri.params",
332373
win = function (fileEntry) {
@@ -545,7 +586,6 @@ exports.defineAutoTests = function () {
545586

546587
var fileName = "de:invalid:path",
547588
fail = function (error) {
548-
console.error(error);
549589
expect(error).toBeDefined();
550590
expect(error).toBeFileError(FileError.ENCODING_ERR);
551591
done();

0 commit comments

Comments
 (0)