From 728824228d99775ac51cccfef67399930385076d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Elcoro?= Date: Thu, 16 Jul 2015 19:17:54 +0200 Subject: [PATCH] Add suport for Windows Uri Schemes --- src/windows/fileOpener2Proxy.js | 46 ++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/windows/fileOpener2Proxy.js b/src/windows/fileOpener2Proxy.js index 71692ca..ce13e32 100644 --- a/src/windows/fileOpener2Proxy.js +++ b/src/windows/fileOpener2Proxy.js @@ -2,13 +2,51 @@ var cordova = require('cordova'), fileOpener2 = require('./FileOpener2'); + var schemes = [ + { protocol: 'ms-app', getFile: getFileFromApplicationUri }, + { protocol: 'file:///', getFile: getFileFromFileUri } + ] + + function getFileFromApplicationUri(uri) { + var applicationUri = new Windows.Foundation.Uri(uri); + + return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri); + } + + function getFileFromFileUri(uri) { + var path = Windows.Storage.ApplicationData.current.localFolder.path + + uri.substr(8); + + return getFileFromNativePath(path); + } + + function getFileFromNativePath(path) { + var nativePath = path.split("/").join("\\"); + + return Windows.Storage.StorageFile.getFileFromPathAsync(nativePath); + } + + function getFileLoaderForScheme(path) { + var fileLoader = getFileFromNativePath; + + schemes.some(function (scheme) { + return path.indexOf(scheme.protocol) === 0 ? ((fileLoader = scheme.getFile), true) : false; + }); + + return fileLoader; + } + module.exports = { open: function (successCallback, errorCallback, args) { - Windows.Storage.StorageFile.getFileFromPathAsync(args[0]).then(function (file) { - var options = new Windows.System.LauncherOptions(); - options.displayApplicationPicker = true; + var path = args[0]; + + var getFile = getFileLoaderForScheme(path); + getFile(path).then(function (file) { + var options = new Windows.System.LauncherOptions(); + options.displayApplicationPicker = true; + Windows.System.Launcher.launchFileAsync(file, options).then(function (success) { if (success) { successCallback(); @@ -17,6 +55,8 @@ } }); + }, function (errror) { + console.log("Error abriendo el archivo"); }); }