Skip to content

Commit ab99f56

Browse files
committedMar 28, 2022
Revert "feat(android): add WebViewAssetLoader proxy handler for cdvfile (apache#513)"
This reverts commit 3366ea6.
1 parent a56ab1e commit ab99f56

11 files changed

+50
-174
lines changed
 

‎README.md

-15
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,7 @@ This method will now return filesystem URLs of the form
422422

423423
which can be used to identify the file uniquely.
424424

425-
In v7.0.0 the return value of `toURL()` for Android was updated to return the absolute `file://` URL when app content is served from the `file://` scheme.
426-
427-
If app content is served from the `http(s)://` scheme, a `cdvfile` formatted URL will be returned instead. The `cdvfile` formatted URL is created from the internal method `toInternalURL()`.
428-
429-
An example `toInternalURL()` return filesystem URL:
430-
431-
https://localhost/persistent/path/to/file
432-
433-
[![toURL flow](https://sketchviz.com/@erisu/7b05499842275be93a0581e8e3576798/6dc71d8302cafd05b443d874a592d10fa415b8e3.sketchy.png)](//sketchviz.com/@erisu/7b05499842275be93a0581e8e3576798)
434-
435-
It is recommended to always use the `toURL()` to ensure that the correct URL is returned.
436-
437425
## cdvfile protocol
438-
439-
- Not Supported on Android
440-
441426
**Purpose**
442427

443428
`cdvfile://localhost/persistent|temporary|another-fs-root*/path/to/file` can be used for platform-independent file paths.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"cordova-android": ">=6.3.0"
3838
},
3939
"7.0.0": {
40-
"cordova-android": ">=10.0.0"
40+
"cordova-android": ">=9.0.0"
4141
},
4242
"8.0.0": {
4343
"cordova": ">100"

‎plugin.xml

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"
3030
<issue>https://github.com/apache/cordova-plugin-file/issues</issue>
3131

3232
<engines>
33-
<engine name="cordova-android" version=">=10.0.0" />
33+
<engine name="cordova-android" version=">=9.0.0" />
3434
</engines>
3535

3636
<js-module src="www/DirectoryEntry.js" name="DirectoryEntry">
@@ -150,9 +150,6 @@ to config.xml in order for the application to find previously stored files.
150150
<source-file src="src/android/AssetFilesystem.java" target-dir="src/org/apache/cordova/file" />
151151
<source-file src="src/android/PendingRequests.java" target-dir="src/org/apache/cordova/file" />
152152

153-
<preference name="ANDROIDX_WEBKIT_VERSION" default="1.4.0"/>
154-
<framework src="androidx.webkit:webkit:$ANDROIDX_WEBKIT_VERSION" />
155-
156153
<!-- android specific file apis -->
157154
<js-module src="www/android/FileSystem.js" name="androidFileSystem">
158155
<merges target="FileSystem" />

‎src/android/AssetFilesystem.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Licensed to the Apache Software Foundation (ASF) under one
2121
import android.content.res.AssetManager;
2222
import android.net.Uri;
2323

24-
import org.apache.cordova.CordovaPreferences;
2524
import org.apache.cordova.CordovaResourceApi;
2625
import org.apache.cordova.LOG;
2726
import org.json.JSONArray;
@@ -134,8 +133,8 @@ private long getAssetSize(String assetPath) throws FileNotFoundException {
134133
}
135134
}
136135

137-
public AssetFilesystem(AssetManager assetManager, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
138-
super(Uri.parse("file:///android_asset/"), "assets", resourceApi, preferences);
136+
public AssetFilesystem(AssetManager assetManager, CordovaResourceApi resourceApi) {
137+
super(Uri.parse("file:///android_asset/"), "assets", resourceApi);
139138
this.assetManager = assetManager;
140139
}
141140

@@ -162,9 +161,10 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) {
162161
if (!subPath.isEmpty()) {
163162
subPath = subPath.substring(1);
164163
}
165-
166-
Uri.Builder b = createLocalUriBuilder();
167-
164+
Uri.Builder b = new Uri.Builder()
165+
.scheme(LocalFilesystemURL.FILESYSTEM_PROTOCOL)
166+
.authority("localhost")
167+
.path(name);
168168
if (!subPath.isEmpty()) {
169169
b.appendEncodedPath(subPath);
170170
}

‎src/android/ContentFilesystem.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ Licensed to the Apache Software Foundation (ASF) under one
2828
import java.io.File;
2929
import java.io.FileNotFoundException;
3030
import java.io.IOException;
31-
32-
import org.apache.cordova.CordovaPreferences;
3331
import org.apache.cordova.CordovaResourceApi;
3432
import org.json.JSONException;
3533
import org.json.JSONObject;
@@ -38,8 +36,8 @@ public class ContentFilesystem extends Filesystem {
3836

3937
private final Context context;
4038

41-
public ContentFilesystem(Context context, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
42-
super(Uri.parse("content://"), "content", resourceApi, preferences);
39+
public ContentFilesystem(Context context, CordovaResourceApi resourceApi) {
40+
super(Uri.parse("content://"), "content", resourceApi);
4341
this.context = context;
4442
}
4543

@@ -70,9 +68,11 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) {
7068
if (subPath.length() > 0) {
7169
subPath = subPath.substring(1);
7270
}
73-
74-
Uri.Builder b = createLocalUriBuilder().appendPath(inputURL.getAuthority());
75-
71+
Uri.Builder b = new Uri.Builder()
72+
.scheme(LocalFilesystemURL.FILESYSTEM_PROTOCOL)
73+
.authority("localhost")
74+
.path(name)
75+
.appendPath(inputURL.getAuthority());
7676
if (subPath.length() > 0) {
7777
b.appendEncodedPath(subPath);
7878
}

‎src/android/FileUtils.java

+17-100
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,16 @@ Licensed to the Apache Software Foundation (ASF) under one
2020

2121
import android.Manifest;
2222
import android.app.Activity;
23-
import android.content.ContentResolver;
2423
import android.content.Context;
2524
import android.content.pm.PackageManager;
2625
import android.net.Uri;
2726
import android.os.Build;
2827
import android.os.Environment;
2928
import android.util.Base64;
30-
import android.util.Log;
31-
import android.webkit.MimeTypeMap;
32-
import android.webkit.WebResourceResponse;
33-
34-
import androidx.webkit.WebViewAssetLoader;
3529

3630
import org.apache.cordova.CallbackContext;
3731
import org.apache.cordova.CordovaInterface;
3832
import org.apache.cordova.CordovaPlugin;
39-
import org.apache.cordova.CordovaPluginPathHandler;
4033
import org.apache.cordova.CordovaWebView;
4134
import org.apache.cordova.LOG;
4235
import org.apache.cordova.PermissionHelper;
@@ -46,16 +39,12 @@ Licensed to the Apache Software Foundation (ASF) under one
4639
import org.json.JSONException;
4740
import org.json.JSONObject;
4841

49-
import java.io.BufferedInputStream;
5042
import java.io.ByteArrayOutputStream;
5143
import java.io.File;
52-
import java.io.FileInputStream;
5344
import java.io.FileNotFoundException;
5445
import java.io.IOException;
5546
import java.io.InputStream;
56-
import java.net.HttpURLConnection;
5747
import java.net.MalformedURLException;
58-
import java.net.URL;
5948
import java.security.Permission;
6049
import java.util.ArrayList;
6150
import java.util.HashMap;
@@ -99,6 +88,8 @@ public class FileUtils extends CordovaPlugin {
9988

10089
private PendingRequests pendingRequests;
10190

91+
92+
10293
/*
10394
* We need both read and write when accessing the storage, I think.
10495
*/
@@ -146,10 +137,10 @@ protected void registerExtraFileSystems(String[] filesystems, HashMap<String, St
146137
if (fsRoot != null) {
147138
File newRoot = new File(fsRoot);
148139
if (newRoot.mkdirs() || newRoot.isDirectory()) {
149-
registerFilesystem(new LocalFilesystem(fsName, webView.getContext(), webView.getResourceApi(), newRoot, preferences));
140+
registerFilesystem(new LocalFilesystem(fsName, webView.getContext(), webView.getResourceApi(), newRoot));
150141
installedFileSystems.add(fsName);
151142
} else {
152-
LOG.d(LOG_TAG, "Unable to create root dir for filesystem \"" + fsName + "\", skipping");
143+
LOG.d(LOG_TAG, "Unable to create root dir for filesystem \"" + fsName + "\", skipping");
153144
}
154145
} else {
155146
LOG.d(LOG_TAG, "Unrecognized extra filesystem identifier: " + fsName);
@@ -227,10 +218,10 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
227218
// Note: The temporary and persistent filesystems need to be the first two
228219
// registered, so that they will match window.TEMPORARY and window.PERSISTENT,
229220
// per spec.
230-
this.registerFilesystem(new LocalFilesystem("temporary", webView.getContext(), webView.getResourceApi(), tmpRootFile, preferences));
231-
this.registerFilesystem(new LocalFilesystem("persistent", webView.getContext(), webView.getResourceApi(), persistentRootFile, preferences));
232-
this.registerFilesystem(new ContentFilesystem(webView.getContext(), webView.getResourceApi(), preferences));
233-
this.registerFilesystem(new AssetFilesystem(webView.getContext().getAssets(), webView.getResourceApi(), preferences));
221+
this.registerFilesystem(new LocalFilesystem("temporary", webView.getContext(), webView.getResourceApi(), tmpRootFile));
222+
this.registerFilesystem(new LocalFilesystem("persistent", webView.getContext(), webView.getResourceApi(), persistentRootFile));
223+
this.registerFilesystem(new ContentFilesystem(webView.getContext(), webView.getResourceApi()));
224+
this.registerFilesystem(new AssetFilesystem(webView.getContext().getAssets(), webView.getResourceApi()));
234225

235226
registerExtraFileSystems(getExtraFileSystemsPreference(activity), getAvailableFileSystems(activity));
236227

@@ -259,15 +250,13 @@ public Uri remapUri(Uri uri) {
259250
if (!LocalFilesystemURL.FILESYSTEM_PROTOCOL.equals(uri.getScheme())) {
260251
return null;
261252
}
262-
263253
try {
264254
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(uri);
265255
Filesystem fs = this.filesystemForURL(inputURL);
266256
if (fs == null) {
267257
return null;
268258
}
269259
String path = fs.filesystemPathForURL(inputURL);
270-
271260
if (path != null) {
272261
return Uri.parse("file://" + fs.filesystemPathForURL(inputURL));
273262
}
@@ -282,7 +271,6 @@ public boolean execute(String action, final String rawArgs, final CallbackContex
282271
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "File plugin is not configured. Please see the README.md file for details on how to update config.xml"));
283272
return true;
284273
}
285-
286274
if (action.equals("testSaveLocationExists")) {
287275
threadhelper(new FileOp() {
288276
public void run(JSONArray args) {
@@ -473,24 +461,18 @@ else if (action.equals("getFile")) {
473461
public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
474462
String dirname = args.getString(0);
475463
String path = args.getString(1);
464+
String nativeURL = resolveLocalFileSystemURI(dirname).getString("nativeURL");
465+
boolean containsCreate = (args.isNull(2)) ? false : args.getJSONObject(2).optBoolean("create", false);
476466

477-
if (dirname.contains(LocalFilesystemURL.CDVFILE_KEYWORD) == true) {
467+
if(containsCreate && needPermission(nativeURL, WRITE)) {
468+
getWritePermission(rawArgs, ACTION_GET_FILE, callbackContext);
469+
}
470+
else if(!containsCreate && needPermission(nativeURL, READ)) {
471+
getReadPermission(rawArgs, ACTION_GET_FILE, callbackContext);
472+
}
473+
else {
478474
JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false);
479475
callbackContext.success(obj);
480-
} else {
481-
String nativeURL = resolveLocalFileSystemURI(dirname).getString("nativeURL");
482-
boolean containsCreate = (args.isNull(2)) ? false : args.getJSONObject(2).optBoolean("create", false);
483-
484-
if(containsCreate && needPermission(nativeURL, WRITE)) {
485-
getWritePermission(rawArgs, ACTION_GET_FILE, callbackContext);
486-
}
487-
else if(!containsCreate && needPermission(nativeURL, READ)) {
488-
getReadPermission(rawArgs, ACTION_GET_FILE, callbackContext);
489-
}
490-
else {
491-
JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false);
492-
callbackContext.success(obj);
493-
}
494476
}
495477
}
496478
}, rawArgs, callbackContext);
@@ -904,7 +886,6 @@ private boolean remove(String baseURLstr) throws NoModificationAllowedException,
904886
private JSONObject getFile(String baseURLstr, String path, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
905887
try {
906888
LocalFilesystemURL inputURL = LocalFilesystemURL.parse(baseURLstr);
907-
908889
Filesystem fs = this.filesystemForURL(inputURL);
909890
if (fs == null) {
910891
throw new MalformedURLException("No installed handlers for this URL");
@@ -1258,68 +1239,4 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal
12581239
LOG.d(LOG_TAG, "Received permission callback for unknown request code");
12591240
}
12601241
}
1261-
1262-
private String getMimeType(Uri uri) {
1263-
String fileExtensionFromUrl = MimeTypeMap.getFileExtensionFromUrl(uri.toString()).toLowerCase();
1264-
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtensionFromUrl);
1265-
}
1266-
1267-
public CordovaPluginPathHandler getPathHandler() {
1268-
WebViewAssetLoader.PathHandler pathHandler = path -> {
1269-
String targetFileSystem = null;
1270-
1271-
if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("persistent"))) {
1272-
targetFileSystem = "persistent";
1273-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("temporary"))) {
1274-
targetFileSystem = "temporary";
1275-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("files"))) {
1276-
targetFileSystem = "files";
1277-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("documents"))) {
1278-
targetFileSystem = "documents";
1279-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache"))) {
1280-
targetFileSystem = "cache";
1281-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("root"))) {
1282-
targetFileSystem = "root";
1283-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("files-external"))) {
1284-
targetFileSystem = "files-external";
1285-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("sdcard"))) {
1286-
targetFileSystem = "sdcard";
1287-
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("cache-external"))) {
1288-
targetFileSystem = "cache-external";
1289-
}
1290-
1291-
if (targetFileSystem != null) {
1292-
// Loop the registered file systems to find the target.
1293-
for (Filesystem fileSystem : filesystems) {
1294-
1295-
/*
1296-
* When target is discovered:
1297-
* 1. Transform the url path to the native path
1298-
* 2. Load the file contents
1299-
* 3. Get the file mime type
1300-
* 4. Return the file & mime information back we Web Resources
1301-
*/
1302-
if (fileSystem.name.equals(targetFileSystem)) {
1303-
// E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/"
1304-
String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", "");
1305-
String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri);
1306-
1307-
File file = new File(fileTarget);
1308-
1309-
try {
1310-
InputStream in = new FileInputStream(file);
1311-
String mimeType = getMimeType(Uri.parse(file.toString()));
1312-
return new WebResourceResponse(mimeType, null, in);
1313-
} catch (FileNotFoundException e) {
1314-
Log.e(LOG_TAG, e.getMessage());
1315-
}
1316-
}
1317-
}
1318-
}
1319-
1320-
return null;
1321-
};
1322-
1323-
return new CordovaPluginPathHandler(pathHandler);
1324-
}
13251242
}

‎src/android/Filesystem.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Licensed to the Apache Software Foundation (ASF) under one
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
3131

32-
import org.apache.cordova.CordovaPreferences;
3332
import org.apache.cordova.CordovaResourceApi;
3433
import org.json.JSONArray;
3534
import org.json.JSONException;
@@ -39,18 +38,13 @@ public abstract class Filesystem {
3938

4039
protected final Uri rootUri;
4140
protected final CordovaResourceApi resourceApi;
42-
protected final CordovaPreferences preferences;
4341
public final String name;
4442
private JSONObject rootEntry;
4543

46-
static String SCHEME_HTTPS = "https";
47-
static String DEFAULT_HOSTNAME = "localhost";
48-
49-
public Filesystem(Uri rootUri, String name, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
44+
public Filesystem(Uri rootUri, String name, CordovaResourceApi resourceApi) {
5045
this.rootUri = rootUri;
5146
this.name = name;
5247
this.resourceApi = resourceApi;
53-
this.preferences = preferences;
5448
}
5549

5650
public interface ReadFileCallback {
@@ -334,15 +328,4 @@ public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException
334328
return numBytesRead;
335329
}
336330
}
337-
338-
protected Uri.Builder createLocalUriBuilder() {
339-
String scheme = preferences.getString("scheme", SCHEME_HTTPS).toLowerCase();
340-
String hostname = preferences.getString("hostname", DEFAULT_HOSTNAME).toLowerCase();
341-
String path = LocalFilesystemURL.fsNameToCdvKeyword(name);
342-
343-
return new Uri.Builder()
344-
.scheme(scheme)
345-
.authority(hostname)
346-
.path(path);
347-
}
348331
}

0 commit comments

Comments
 (0)