Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): Content FS support in PathHandler #629

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/android/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1240,6 +1241,8 @@ private String getMimeType(Uri uri) {
}

public CordovaPluginPathHandler getPathHandler() {
final CordovaResourceApi resourceApi = webView.getResourceApi();

WebViewAssetLoader.PathHandler pathHandler = path -> {
String targetFileSystem = null;

Expand All @@ -1263,9 +1266,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.
Expand All @@ -1279,26 +1284,46 @@ 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);
Uri fileUri = Uri.parse(fileTarget);
String mimeType = null;

String filePath = !isAssetsFS ? file.toString() : fileTarget;
Uri fileUri = Uri.parse(filePath);
String fileMimeType = getMimeType(fileUri);
try {
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(fileMimeType, null, fileIS);
return new WebResourceResponse(mimeType, null, io);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, e.getMessage());
} catch (IOException e) {
Expand Down
Loading