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

mapUriToFile has no implementation for http / https schemes #1496

Open
breautek opened this issue Sep 21, 2022 · 2 comments
Open

mapUriToFile has no implementation for http / https schemes #1496

breautek opened this issue Sep 21, 2022 · 2 comments
Labels
bug discussion Requires Triage Issues that requires further analysis

Comments

@breautek
Copy link
Contributor

public File mapUriToFile(Uri uri) {
assertBackgroundThread();
switch (getUriType(uri)) {
case URI_TYPE_FILE:
return new File(uri.getPath());
case URI_TYPE_CONTENT: {
Cursor cursor = contentResolver.query(uri, LOCAL_FILE_PROJECTION, null, null, null);
if (cursor != null) {
try {
int columnIndex = cursor.getColumnIndex(LOCAL_FILE_PROJECTION[0]);
if (columnIndex != -1 && cursor.getCount() > 0) {
cursor.moveToFirst();
String realPath = cursor.getString(columnIndex);
if (realPath != null) {
return new File(realPath);
}
}
} finally {
cursor.close();
}
}
}
}
return null;
}

The cordova-plugin-file-transfer plugin utilizes this method to map a uri value such as http://localhost/__cdvfile_persistent__/testFile.txt to a local file url, when using the file transfer's download API. However because this API does not handle the http scheme, it returns a Null, which eventually leads to a NullPointerException and failing unit tests.

the testFile.txt url comes from the following code:

this.root = this.persistentRoot;
this.fileName = 'testFile.txt';
this.localFilePath = this.root.toURL() + this.fileName;

Where this.persistentRoot resolved by:

window.requestFileSystem(
    LocalFileSystem.PERSISTENT,
    DEFAULT_FILESYSTEM_SIZE,
    function (fileSystem) {
        specContext.persistentRoot = fileSystem.root;
        done();
    },
    function () {
        throw new Error('Failed to initialize persistent file system.');
    }
);

I'm raising the issue here because the method in question is located inside cordova-android, but it's unclear if the solution is to update mapUriToFile support this use case, or if the file transfer plugin should expect file:// urls instead of http:// urls.

@breautek breautek added bug discussion Requires Triage Issues that requires further analysis labels Sep 21, 2022
@breautek
Copy link
Contributor Author

@erisu @jcesarmobile @NiklasMerz

Would be interested in your feedback on this. I'm not particularly sure why the file transfer plugin is receiving a http:// or https:// uri for the target destination when downloading files, but it kinda feels wrong to me.

but: the http://localhost/__cdvfile_persistent__ url is suppose to be the "cdvfile:" replacement and suppose to map to a local filesystem. So a solution could be to add support for http / https schemes, but only if the path contains one of these special __cdvfile_ urls.

@weareu
Copy link

weareu commented Oct 31, 2022

On Crodova 10+ all FileEntry returns will only return a http url (this is toURL, toInternalURL toNativeURL) all return the same http/s based __cdvfile url, there is only one way to get the file: uri's from any of the cordova-file plugin calls via javascript and that is to use entry.nativeURL (note not toNativeURL as that will also return http based URI). Most platforms from ios to electron will support the urls correctly. So I do think it may be a good idea to add support for the new CDVfile scheme on http for android. However for my purpose I will change the passed URL to file transfer to entry.nativeURL. @breautek for your purposes change:

this.root.toURL()

to

this.root.nativeURL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug discussion Requires Triage Issues that requires further analysis
Projects
None yet
Development

No branches or pull requests

2 participants