@@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one
36
36
import org .apache .cordova .CordovaInterface ;
37
37
import org .apache .cordova .CordovaPlugin ;
38
38
import org .apache .cordova .CordovaPluginPathHandler ;
39
+ import org .apache .cordova .CordovaResourceApi ;
39
40
import org .apache .cordova .CordovaWebView ;
40
41
import org .apache .cordova .LOG ;
41
42
import org .apache .cordova .PermissionHelper ;
@@ -1240,6 +1241,8 @@ private String getMimeType(Uri uri) {
1240
1241
}
1241
1242
1242
1243
public CordovaPluginPathHandler getPathHandler () {
1244
+ final CordovaResourceApi resourceApi = webView .getResourceApi ();
1245
+
1243
1246
WebViewAssetLoader .PathHandler pathHandler = path -> {
1244
1247
String targetFileSystem = null ;
1245
1248
@@ -1263,9 +1266,11 @@ public CordovaPluginPathHandler getPathHandler() {
1263
1266
targetFileSystem = "cache-external" ;
1264
1267
} else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("assets" ))) {
1265
1268
targetFileSystem = "assets" ;
1269
+ } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("content" ))) {
1270
+ targetFileSystem = "content" ;
1266
1271
}
1267
1272
1268
- boolean isAssetsFS = targetFileSystem == "assets" ;
1273
+ boolean isAssetsFS = "assets" . equals ( targetFileSystem ) ;
1269
1274
1270
1275
if (targetFileSystem != null ) {
1271
1276
// Loop the registered file systems to find the target.
@@ -1279,26 +1284,46 @@ public CordovaPluginPathHandler getPathHandler() {
1279
1284
*/
1280
1285
if (fileSystem .name .equals (targetFileSystem )) {
1281
1286
// 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
+
1282
1305
String fileSystemNativeUri = fileSystem .rootUri .toString ().replace ("file://" , "" );
1283
1306
String fileTarget = path .replace (LocalFilesystemURL .fsNameToCdvKeyword (targetFileSystem ) + "/" , fileSystemNativeUri );
1284
- File file = null ;
1285
1307
1286
1308
if (isAssetsFS ) {
1287
1309
fileTarget = fileTarget .replace ("/android_asset/" , "" );
1288
- } else {
1289
- file = new File (fileTarget );
1290
1310
}
1291
1311
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 ;
1296
1314
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
+ }
1300
1325
1301
- return new WebResourceResponse (fileMimeType , null , fileIS );
1326
+ return new WebResourceResponse (mimeType , null , io );
1302
1327
} catch (FileNotFoundException e ) {
1303
1328
Log .e (LOG_TAG , e .getMessage ());
1304
1329
} catch (IOException e ) {
0 commit comments