Update fileOpener2Proxy.js

This commit is contained in:
Nathaniel van Diepen 2018-12-11 09:02:14 -07:00 committed by GitHub
parent d84b20e620
commit 0c6cbedb17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,22 +1,22 @@
var cordova = require('cordova'),
var cordova = require('cordova'),
fileOpener2 = require('./FileOpener2');
var schemes = [
var schemes = [
{ protocol: 'ms-app', getFile: getFileFromApplicationUri },
{ protocol: 'cdvfile', getFile: getFileFromFileUri } //protocol cdvfile
]
]
function nthIndex(str, pat, n) {
function nthIndex(str, pat, n) {
var L = str.length, i = -1;
while (n-- && i++ < L) {
i = str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
}
function getFileFromApplicationUri(uri) {
function getFileFromApplicationUri(uri) {
/* bad path from a file entry due to the last '//'
example: ms-appdata:///local//path/to/file
*/
@ -26,9 +26,9 @@ function getFileFromApplicationUri(uri) {
var applicationUri = new Windows.Foundation.Uri(newUri);
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri);
}
}
function getFileFromFileUri(uri) {
function getFileFromFileUri(uri) {
/* uri example:
cdvfile://localhost/persistent|temporary|another-fs-root/path/to/file
*/
@ -46,15 +46,15 @@ function getFileFromFileUri(uri) {
}
return getFileFromNativePath(path);
}
}
function getFileFromNativePath(path) {
function getFileFromNativePath(path) {
var nativePath = path.split("/").join("\\");
return Windows.Storage.StorageFile.getFileFromPathAsync(nativePath);
}
}
function getFileLoaderForScheme(path) {
function getFileLoaderForScheme(path) {
var fileLoader = getFileFromNativePath;
schemes.some(function (scheme) {
@ -62,34 +62,36 @@ function getFileLoaderForScheme(path) {
});
return fileLoader;
}
}
module.exports = {
module.exports = {
open: function (successCallback, errorCallback, args) {
var path = args[0];
var getFile = getFileLoaderForScheme(path);
try{
getFile(path).then(function (file) {
var options = new Windows.System.LauncherOptions();
try{
Windows.System.Launcher.launchFileAsync(file, options).then(function (success) {
successCallback();
}, function (error) {
errorCallback(error);
});
}catch(error){
errorCallback(error);
}
}, function (error) {
console.log("Error while opening the file: "+error);
errorCallback(error);
});
}catch(error){
errorCallback(error);
}
}
};
};
require("cordova/exec/proxy").add("FileOpener2", module.exports);
require("cordova/exec/proxy").add("FileOpener2", module.exports);