Skip to content

Commit 99ca439

Browse files
breautekerisu
andauthored
fix(android): Content FS support in PathHandler (#629)
* fix(android): Content FS support in PathHandler * chore(android): add missing CordovaResourceApi import * fix(test): Regression when using asset URLs --------- Co-authored-by: Erisu <[email protected]>
1 parent 03a4f4c commit 99ca439

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/android/FileUtils.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one
3636
import org.apache.cordova.CordovaInterface;
3737
import org.apache.cordova.CordovaPlugin;
3838
import org.apache.cordova.CordovaPluginPathHandler;
39+
import org.apache.cordova.CordovaResourceApi;
3940
import org.apache.cordova.CordovaWebView;
4041
import org.apache.cordova.LOG;
4142
import org.apache.cordova.PermissionHelper;
@@ -1240,6 +1241,8 @@ private String getMimeType(Uri uri) {
12401241
}
12411242

12421243
public CordovaPluginPathHandler getPathHandler() {
1244+
final CordovaResourceApi resourceApi = webView.getResourceApi();
1245+
12431246
WebViewAssetLoader.PathHandler pathHandler = path -> {
12441247
String targetFileSystem = null;
12451248

@@ -1263,9 +1266,11 @@ public CordovaPluginPathHandler getPathHandler() {
12631266
targetFileSystem = "cache-external";
12641267
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("assets"))) {
12651268
targetFileSystem = "assets";
1269+
} else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("content"))) {
1270+
targetFileSystem = "content";
12661271
}
12671272

1268-
boolean isAssetsFS = targetFileSystem == "assets";
1273+
boolean isAssetsFS = "assets".equals(targetFileSystem);
12691274

12701275
if (targetFileSystem != null) {
12711276
// Loop the registered file systems to find the target.
@@ -1279,26 +1284,46 @@ public CordovaPluginPathHandler getPathHandler() {
12791284
*/
12801285
if (fileSystem.name.equals(targetFileSystem)) {
12811286
// E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/"
1287+
1288+
if ("content".equals(targetFileSystem)) {
1289+
// The WebviewAssetLoader uses getPath API, which gives us a decoded path
1290+
// For content paths however, we need it to remain encoded.
1291+
// The Uri.encode API will encode forward slashes, therefore we must
1292+
// split the path, encode each segment, and rebuild the path
1293+
String[] pathParts = path.split("/");
1294+
path = "";
1295+
for (String part : pathParts) {
1296+
if ("".equals(path)) {
1297+
path = Uri.encode(part);
1298+
}
1299+
else {
1300+
path += "/" + Uri.encode(part);
1301+
}
1302+
}
1303+
}
1304+
12821305
String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", "");
12831306
String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri);
1284-
File file = null;
12851307

12861308
if (isAssetsFS) {
12871309
fileTarget = fileTarget.replace("/android_asset/", "");
1288-
} else {
1289-
file = new File(fileTarget);
12901310
}
12911311

1292-
try {
1293-
InputStream fileIS = !isAssetsFS ?
1294-
new FileInputStream(file) :
1295-
webView.getContext().getAssets().open(fileTarget);
1312+
Uri fileUri = Uri.parse(fileTarget);
1313+
String mimeType = null;
12961314

1297-
String filePath = !isAssetsFS ? file.toString() : fileTarget;
1298-
Uri fileUri = Uri.parse(filePath);
1299-
String fileMimeType = getMimeType(fileUri);
1315+
try {
1316+
InputStream io = null;
1317+
if (isAssetsFS) {
1318+
io = webView.getContext().getAssets().open(fileTarget);
1319+
mimeType = getMimeType(fileUri);
1320+
} else {
1321+
CordovaResourceApi.OpenForReadResult resource = resourceApi.openForRead(fileUri);
1322+
io = resource.inputStream;
1323+
mimeType = resource.mimeType;
1324+
}
13001325

1301-
return new WebResourceResponse(fileMimeType, null, fileIS);
1326+
return new WebResourceResponse(mimeType, null, io);
13021327
} catch (FileNotFoundException e) {
13031328
Log.e(LOG_TAG, e.getMessage());
13041329
} catch (IOException e) {

0 commit comments

Comments
 (0)