Update fileOpener2Proxy.js

It didn't work on windows, mainly due to uri/path problems. I fixed it, + some changes like an error on callback.
This commit is contained in:
J3r0M3D3V 2016-10-12 12:50:55 +02:00 committed by GitHub
parent cd924d4757
commit e2ea30f973

View File

@ -4,18 +4,46 @@
var schemes = [ var schemes = [
{ protocol: 'ms-app', getFile: getFileFromApplicationUri }, { protocol: 'ms-app', getFile: getFileFromApplicationUri },
{ protocol: 'file:///', getFile: getFileFromFileUri } { protocol: 'cdvfile', getFile: getFileFromFileUri } //protocol cdvfile
] ]
function getFileFromApplicationUri(uri) { function nthIndex(str, pat, n) {
var applicationUri = new Windows.Foundation.Uri(uri); var L = str.length, i = -1;
while (n-- && i++ < L) {
i = str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri); function getFileFromApplicationUri(uri) {
/* bad path from a file entry due to the last '//'
example: ms-appdata:///local//path/to/file
*/
var index = nthIndex(uri, "//", 3);
var newUri = uri.substr(0, index) + uri.substr(index + 1);
var applicationUri = new Windows.Foundation.Uri(newUri);
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri);;
} }
function getFileFromFileUri(uri) { function getFileFromFileUri(uri) {
var path = Windows.Storage.ApplicationData.current.localFolder.path + /* uri example:
uri.substr(8); cdvfile://localhost/persistent|temporary|another-fs-root/path/to/file
*/
var indexFrom = nthIndex(uri, "/", 3) + 1;
var indexTo = nthIndex(uri, "/", 4);
var whichFolder = uri.substring(indexFrom, indexTo);
var filePath = uri.substr(indexTo + 1);
var path = "\\" + filePath;
if (whichFolder == "persistent") {
path = Windows.Storage.ApplicationData.current.localFolder.path + path;
}
else { //temporary, note: no roaming management
path = Windows.Storage.ApplicationData.current.temporaryFolder.path + path;
}
return getFileFromNativePath(path); return getFileFromNativePath(path);
} }
@ -39,23 +67,22 @@
module.exports = { module.exports = {
open: function (successCallback, errorCallback, args) { open: function (successCallback, errorCallback, args) {
var path = args[0]; var path = args[0];
var getFile = getFileLoaderForScheme(path); var getFile = getFileLoaderForScheme(path);
getFile(path).then(function (file) { getFile(path).then(function (file) {
var options = new Windows.System.LauncherOptions(); var options = new Windows.System.LauncherOptions();
Windows.System.Launcher.launchFileAsync(file, options).then(function (success) { Windows.System.Launcher.launchFileAsync(file, options).done(function (success) {
if (success) { successCallback();
successCallback(); }, function (error) {
} else { errorCallback(error);
errorCallback();
}
}); });
}, function (errror) { }, function (error) {
console.log("Error abriendo el archivo"); console.log("Error while opening the file: "+error);
}); });
} }