Skip to content
Permalink
Browse files
fix(android): support cdvfile assets for custom scheme (#517)
  • Loading branch information
erisu committed Mar 30, 2022
1 parent 2ecccbf commit ef301bcd17bd2e86367cdf90b7f1b06a84f71f1b
Showing 2 changed files with 30 additions and 9 deletions.
@@ -1286,12 +1286,15 @@ public CordovaPluginPathHandler getPathHandler() {
targetFileSystem = "sdcard";
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache-external"))) {
targetFileSystem = "cache-external";
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("assets"))) {
targetFileSystem = "assets";
}

boolean isAssetsFS = targetFileSystem == "assets";

if (targetFileSystem != null) {
// Loop the registered file systems to find the target.
for (Filesystem fileSystem : filesystems) {

/*
* When target is discovered:
* 1. Transform the url path to the native path
@@ -1303,15 +1306,28 @@ public CordovaPluginPathHandler getPathHandler() {
// E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/"
String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", "");
String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri);
File file = null;

File file = new File(fileTarget);
if (isAssetsFS) {
fileTarget = fileTarget.replace("/android_asset/", "");
} else {
file = new File(fileTarget);
}

try {
InputStream in = new FileInputStream(file);
String mimeType = getMimeType(Uri.parse(file.toString()));
return new WebResourceResponse(mimeType, null, in);
InputStream fileIS = !isAssetsFS ?
new FileInputStream(file) :
webView.getContext().getAssets().open(fileTarget);

String filePath = !isAssetsFS ? file.toString() : fileTarget;
Uri fileUri = Uri.parse(filePath);
String fileMimeType = getMimeType(fileUri);

return new WebResourceResponse(fileMimeType, null, fileIS);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, e.getMessage());
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
}
}
}
@@ -3436,6 +3436,7 @@ exports.defineAutoTests = function () {
* which appears to be sane.
*/
var pathExpect = cordova.platformId === 'windowsphone' ? '//nativ' : 'file://'; // eslint-disable-line no-undef

if (isChrome) {
pathExpect = 'filesystem:http://';
}
@@ -3805,18 +3806,22 @@ exports.defineAutoTests = function () {
});
});

describe('resolveLocalFileSystemURL on cdvfile://', function () {
describe('resolveLocalFileSystemURL for cdvfile', function () {
it('file.spec.147 should be able to resolve cdvfile applicationDirectory fs root', function (done) {
var cdvfileApplicationDirectoryFsRootName;
var cdvfileApplicationDirectoryFsRootNameURL;
if (cordova.platformId === 'android') {
cdvfileApplicationDirectoryFsRootName = 'assets';
cdvfileApplicationDirectoryFsRootNameURL = 'https://localhost/__cdvfile_' + cdvfileApplicationDirectoryFsRootName + '__/'

Check failure on line 3815 in tests/tests.js

GitHub Actions / Lint Test

tests/tests.js#L3815

Missing semicolon
} else if (cordova.platformId === 'ios') {
cdvfileApplicationDirectoryFsRootName = 'bundle';
cdvfileApplicationDirectoryFsRootNameURL = 'cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/'

Check failure on line 3818 in tests/tests.js

GitHub Actions / Lint Test

tests/tests.js#L3818

Missing semicolon
} else {
pending();
}

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

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

0 comments on commit ef301bc

Please sign in to comment.