mapUriToFile has no implementation for `http` / `https` schemes
https://github.com/apache/cordova-android/blob/954d3e0e7542356e6ea995ddc75dcb1db8bb36cc/framework/src/org/apache/cordova/CordovaResourceApi.java#L149-L173
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.
@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.
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