CB-11305 Enable cdvfile: assets fs root for DOM requests
Added test for cdvfile applicationDirectory fs root
Updated the docs and added the assets to Android available fs roots list
Allow cdvfile: access for android automatically
diff --git a/README.md b/README.md
index b2f1efb..8a50749 100644
--- a/README.md
+++ b/README.md
@@ -164,7 +164,7 @@
| Device Path | `cordova.file.*` | `AndroidExtraFileSystems` | r/w? | persistent? | OS clears | private |
|:------------------------------------------------|:----------------------------|:--------------------------|:----:|:-----------:|:---------:|:-------:|
-| `file:///android_asset/` | applicationDirectory | | r | N/A | N/A | Yes |
+| `file:///android_asset/` | applicationDirectory | assets | r | N/A | N/A | Yes |
| `/data/data/<app-id>/` | applicationStorageDirectory | - | r/w | N/A | N/A | Yes |
| `cache` | cacheDirectory | cache | r/w | Yes | Yes\* | Yes |
| `files` | dataDirectory | files | r/w | Yes | No | Yes |
@@ -532,7 +532,7 @@
filesystems to be installed. By default, all file-system roots are enabled.
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
- <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
+ <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
### Android
@@ -541,6 +541,7 @@
* `sdcard`: The global external file storage directory (this is the root of the SD card, if one is installed). You must have the `android.permission.WRITE_EXTERNAL_STORAGE` permission to use this.
* `cache`: The application's internal cache directory
* `cache-external`: The application's external cache directory
+* `assets`: The application's bundle (read-only)
* `root`: The entire device filesystem
Android also supports a special filesystem named "documents", which represents a "/Documents/" subdirectory within the "files" filesystem.
diff --git a/plugin.xml b/plugin.xml
index 629c2e4..98588df 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -129,6 +129,7 @@
<param name="android-package" value="org.apache.cordova.file.FileUtils"/>
<param name="onload" value="true" />
</feature>
+ <allow-navigation href="cdvfile:*" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
diff --git a/src/android/AssetFilesystem.java b/src/android/AssetFilesystem.java
index 1b6fce7..2266f3e 100644
--- a/src/android/AssetFilesystem.java
+++ b/src/android/AssetFilesystem.java
@@ -273,7 +273,7 @@
@Override
String filesystemPathForURL(LocalFilesystemURL url) {
- return null;
+ return new File(rootUri.getPath(), url.path).toString();
}
@Override
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index f77daec..91e40e2 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -122,7 +122,7 @@
}
protected String[] getExtraFileSystemsPreference(Activity activity) {
- String fileSystemsStr = preferences.getString("androidextrafilesystems", "files,files-external,documents,sdcard,cache,cache-external,root");
+ String fileSystemsStr = preferences.getString("androidextrafilesystems", "files,files-external,documents,sdcard,cache,cache-external,assets,root");
return fileSystemsStr.split(",");
}
diff --git a/tests/tests.js b/tests/tests.js
index a7de7aa..33688dc 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -3441,6 +3441,46 @@
}
});
});
+ describe('resolveLocalFileSystemURL on cdvfile://', function () {
+ it("file.spec.147 should be able to resolve cdvfile applicationDirectory fs root", function(done) {
+ var cdvfileApplicationDirectoryFsRootName;
+ if (cordova.platformId === 'android') {
+ cdvfileApplicationDirectoryFsRootName = 'assets';
+ } else if (cordova.platformId === 'ios') {
+ cdvfileApplicationDirectoryFsRootName = 'bundle';
+ } else {
+ pending();
+ }
+
+ resolveLocalFileSystemURL('cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/', function(applicationDirectoryRoot) {
+ expect(applicationDirectoryRoot.isFile).toBe(false);
+ expect(applicationDirectoryRoot.isDirectory).toBe(true);
+ expect(applicationDirectoryRoot.name).toCanonicallyMatch('');
+ expect(applicationDirectoryRoot.fullPath).toCanonicallyMatch('/');
+ expect(applicationDirectoryRoot.filesystem.name).toEqual(cdvfileApplicationDirectoryFsRootName);
+
+ // 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) {
+ expect(entry.isFile).toBe(true);
+ expect(entry.isDirectory).toBe(false);
+ expect(entry.name).toCanonicallyMatch('logo.png');
+ expect(entry.fullPath).toCanonicallyMatch('/www/img/logo.png');
+ expect(entry.filesystem.name).toEqual(cdvfileApplicationDirectoryFsRootName);
+
+ var img = new Image();
+ img.onerror = function(err) {
+ expect(err).not.toBeDefined();
+ done();
+ };
+ img.onload = function() {
+ done();
+ };
+ img.src = entry.toInternalURL();
+ }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
+ }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
+ });
+ });
//cross-file-system copy and move
describe('IndexedDB-based impl', function () {
it("file.spec.131 Nested file or nested directory should be removed when removing a parent directory", function (done) {
@@ -3576,6 +3616,7 @@
// Content and Asset URLs
if (cordova.platformId == 'android') {
describe('content: URLs', function() {
+ // Warning: Default HelloWorld www directory structure is required for these tests (www/index.html at least)
function testContentCopy(src, done) {
var file2 = "entry.copy.file2b",
fullPath = joinURL(temp_root.fullPath, file2),
@@ -3786,7 +3827,7 @@
var fsRoots = {
"ios" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
"osx" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
- "android" : "files,files-external,documents,sdcard,cache,cache-external,root",
+ "android" : "files,files-external,documents,sdcard,cache,cache-external,assets,root",
"amazon-fireos" : "files,files-external,documents,sdcard,cache,cache-external,root",
"windows": "temporary,persistent"
};