From d99efadcd1ba82eaa12f353437c04fd84d563d19 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Thu, 18 Jul 2024 16:15:10 -0300 Subject: [PATCH 1/3] fix(android): Content FS support in PathHandler --- src/android/FileUtils.java | 40 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 0edf90a0..d65399dc 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -1240,6 +1240,8 @@ private String getMimeType(Uri uri) { } public CordovaPluginPathHandler getPathHandler() { + final CordovaResourceApi resourceApi = webView.getResourceApi(); + WebViewAssetLoader.PathHandler pathHandler = path -> { String targetFileSystem = null; @@ -1263,9 +1265,11 @@ public CordovaPluginPathHandler getPathHandler() { targetFileSystem = "cache-external"; } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("assets"))) { targetFileSystem = "assets"; + } else if (path.startsWith(LocalFilesystemURL.fsNameToCdvKeyword("content"))) { + targetFileSystem = "content"; } - boolean isAssetsFS = targetFileSystem == "assets"; + boolean isAssetsFS = "assets".equals(targetFileSystem); if (targetFileSystem != null) { // Loop the registered file systems to find the target. @@ -1279,26 +1283,36 @@ public CordovaPluginPathHandler getPathHandler() { */ if (fileSystem.name.equals(targetFileSystem)) { // E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/" + + if ("content".equals(targetFileSystem)) { + // The WebviewAssetLoader uses getPath API, which gives us a decoded path + // For content paths however, we need it to remain encoded. + // The Uri.encode API will encode forward slashes, therefore we must + // split the path, encode each segment, and rebuild the path + String[] pathParts = path.split("/"); + path = ""; + for (String part : pathParts) { + if ("".equals(path)) { + path = Uri.encode(part); + } + else { + path += "/" + Uri.encode(part); + } + } + } + String fileSystemNativeUri = fileSystem.rootUri.toString().replace("file://", ""); String fileTarget = path.replace(LocalFilesystemURL.fsNameToCdvKeyword(targetFileSystem) + "/", fileSystemNativeUri); - File file = null; if (isAssetsFS) { fileTarget = fileTarget.replace("/android_asset/", ""); - } else { - file = new File(fileTarget); } - try { - 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); + Uri fileUri = Uri.parse(fileTarget); - return new WebResourceResponse(fileMimeType, null, fileIS); + try { + CordovaResourceApi.OpenForReadResult resource = resourceApi.openForRead(fileUri); + return new WebResourceResponse(resource.mimeType, null, resource.inputStream); } catch (FileNotFoundException e) { Log.e(LOG_TAG, e.getMessage()); } catch (IOException e) { From 6b8c98179aa1b4df0202b6ff8f98e8733339e592 Mon Sep 17 00:00:00 2001 From: Erisu Date: Fri, 19 Jul 2024 11:32:04 +0900 Subject: [PATCH 2/3] chore(android): add missing CordovaResourceApi import --- src/android/FileUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index d65399dc..686dafec 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPluginPathHandler; +import org.apache.cordova.CordovaResourceApi; import org.apache.cordova.CordovaWebView; import org.apache.cordova.LOG; import org.apache.cordova.PermissionHelper; From f3ffb61f9ea876208f3fe1ed1fc8a90b0633b3e6 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Fri, 19 Jul 2024 14:57:13 -0300 Subject: [PATCH 3/3] fix(test): Regression when using asset URLs --- src/android/FileUtils.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 686dafec..45099ca3 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -1310,10 +1310,20 @@ public CordovaPluginPathHandler getPathHandler() { } Uri fileUri = Uri.parse(fileTarget); + String mimeType = null; try { - CordovaResourceApi.OpenForReadResult resource = resourceApi.openForRead(fileUri); - return new WebResourceResponse(resource.mimeType, null, resource.inputStream); + InputStream io = null; + if (isAssetsFS) { + io = webView.getContext().getAssets().open(fileTarget); + mimeType = getMimeType(fileUri); + } else { + CordovaResourceApi.OpenForReadResult resource = resourceApi.openForRead(fileUri); + io = resource.inputStream; + mimeType = resource.mimeType; + } + + return new WebResourceResponse(mimeType, null, io); } catch (FileNotFoundException e) { Log.e(LOG_TAG, e.getMessage()); } catch (IOException e) {