Skip to content

Commit ef301bc

Browse files
authored
fix(android): support cdvfile assets for custom scheme (apache#517)
1 parent 2ecccbf commit ef301bc

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/android/FileUtils.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,12 +1286,15 @@ public CordovaPluginPathHandler getPathHandler() {
12861286
targetFileSystem = "sdcard";
12871287
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache-external"))) {
12881288
targetFileSystem = "cache-external";
1289+
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("assets"))) {
1290+
targetFileSystem = "assets";
12891291
}
12901292

1293+
boolean isAssetsFS = targetFileSystem == "assets";
1294+
12911295
if (targetFileSystem != null) {
12921296
// Loop the registered file systems to find the target.
12931297
for (Filesystem fileSystem : filesystems) {
1294-
12951298
/*
12961299
* When target is discovered:
12971300
* 1. Transform the url path to the native path
@@ -1303,15 +1306,28 @@ public CordovaPluginPathHandler getPathHandler() {
13031306
// E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/"
13041307
String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", "");
13051308
String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri);
1309+
File file = null;
13061310

1307-
File file = new File(fileTarget);
1311+
if (isAssetsFS) {
1312+
fileTarget = fileTarget.replace("/android_asset/", "");
1313+
} else {
1314+
file = new File(fileTarget);
1315+
}
13081316

13091317
try {
1310-
InputStream in = new FileInputStream(file);
1311-
String mimeType = getMimeType(Uri.parse(file.toString()));
1312-
return new WebResourceResponse(mimeType, null, in);
1318+
InputStream fileIS = !isAssetsFS ?
1319+
new FileInputStream(file) :
1320+
webView.getContext().getAssets().open(fileTarget);
1321+
1322+
String filePath = !isAssetsFS ? file.toString() : fileTarget;
1323+
Uri fileUri = Uri.parse(filePath);
1324+
String fileMimeType = getMimeType(fileUri);
1325+
1326+
return new WebResourceResponse(fileMimeType, null, fileIS);
13131327
} catch (FileNotFoundException e) {
13141328
Log.e(LOG_TAG, e.getMessage());
1329+
} catch (IOException e) {
1330+
Log.e(LOG_TAG, e.getMessage());
13151331
}
13161332
}
13171333
}

tests/tests.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,6 +3436,7 @@ exports.defineAutoTests = function () {
34363436
* which appears to be sane.
34373437
*/
34383438
var pathExpect = cordova.platformId === 'windowsphone' ? '//nativ' : 'file://'; // eslint-disable-line no-undef
3439+
34393440
if (isChrome) {
34403441
pathExpect = 'filesystem:http://';
34413442
}
@@ -3805,18 +3806,22 @@ exports.defineAutoTests = function () {
38053806
});
38063807
});
38073808

3808-
describe('resolveLocalFileSystemURL on cdvfile://', function () {
3809+
describe('resolveLocalFileSystemURL for cdvfile', function () {
38093810
it('file.spec.147 should be able to resolve cdvfile applicationDirectory fs root', function (done) {
38103811
var cdvfileApplicationDirectoryFsRootName;
3812+
var cdvfileApplicationDirectoryFsRootNameURL;
38113813
if (cordova.platformId === 'android') {
38123814
cdvfileApplicationDirectoryFsRootName = 'assets';
3815+
cdvfileApplicationDirectoryFsRootNameURL = 'https://localhost/__cdvfile_' + cdvfileApplicationDirectoryFsRootName + '__/'
38133816
} else if (cordova.platformId === 'ios') {
38143817
cdvfileApplicationDirectoryFsRootName = 'bundle';
3818+
cdvfileApplicationDirectoryFsRootNameURL = 'cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/'
38153819
} else {
38163820
pending();
38173821
}
38183822

3819-
resolveLocalFileSystemURL('cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/', function (applicationDirectoryRoot) {
3823+
resolveLocalFileSystemURL(cdvfileApplicationDirectoryFsRootNameURL, function (applicationDirectoryRoot) {
3824+
console.log(applicationDirectoryRoot);
38203825
expect(applicationDirectoryRoot.isFile).toBe(false);
38213826
expect(applicationDirectoryRoot.isDirectory).toBe(true);
38223827
expect(applicationDirectoryRoot.name).toCanonicallyMatch('');
@@ -3825,7 +3830,7 @@ exports.defineAutoTests = function () {
38253830

38263831
// Requires HelloCordova www assets, <allow-navigation href="cdvfile:*" /> in config.xml or
38273832
// cdvfile: in CSP and <access origin="cdvfile://*" /> in config.xml
3828-
resolveLocalFileSystemURL('cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/www/img/logo.png', function (entry) {
3833+
resolveLocalFileSystemURL(cdvfileApplicationDirectoryFsRootNameURL + '/www/img/logo.png', function (entry) {
38293834
/* eslint-enable no-undef */
38303835
expect(entry.isFile).toBe(true);
38313836
expect(entry.isDirectory).toBe(false);
@@ -3841,7 +3846,7 @@ exports.defineAutoTests = function () {
38413846
img.onload = function () {
38423847
done();
38433848
};
3844-
img.src = entry.toInternalURL();
3849+
img.src = entry.toURL();
38453850
}, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
38463851
}, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
38473852
});

0 commit comments

Comments
 (0)