You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2022/03/30 07:14:49 UTC
[cordova-plugin-file] branch master updated: fix(android): support cdvfile assets for custom scheme (#517)
This is an automated email from the ASF dual-hosted git repository.
erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-file.git
The following commit(s) were added to refs/heads/master by this push:
new ef301bc fix(android): support cdvfile assets for custom scheme (#517)
ef301bc is described below
commit ef301bcd17bd2e86367cdf90b7f1b06a84f71f1b
Author: エリス <er...@users.noreply.github.com>
AuthorDate: Wed Mar 30 16:14:45 2022 +0900
fix(android): support cdvfile assets for custom scheme (#517)
---
src/android/FileUtils.java | 26 +++++++++++++++++++++-----
tests/tests.js | 13 +++++++++----
2 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index c2b1ca0..24c0a40 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -1286,12 +1286,15 @@ public class FileUtils extends CordovaPlugin {
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 class FileUtils extends CordovaPlugin {
// 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());
}
}
}
diff --git a/tests/tests.js b/tests/tests.js
index af296ba..80862c3 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -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 + '__/'
} else if (cordova.platformId === 'ios') {
cdvfileApplicationDirectoryFsRootName = 'bundle';
+ cdvfileApplicationDirectoryFsRootNameURL = 'cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/'
} 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'));
});
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org