mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-01-31 12:02:49 +08:00
Update fileOpener2Proxy.js
This commit is contained in:
parent
0c6cbedb17
commit
85fa81fba3
@ -1,97 +1,97 @@
|
|||||||
|
|
||||||
var cordova = require('cordova'),
|
var cordova = require('cordova'),
|
||||||
fileOpener2 = require('./FileOpener2');
|
fileOpener2 = require('./FileOpener2');
|
||||||
|
|
||||||
var schemes = [
|
var schemes = [
|
||||||
{ protocol: 'ms-app', getFile: getFileFromApplicationUri },
|
{ protocol: 'ms-app', getFile: getFileFromApplicationUri },
|
||||||
{ protocol: 'cdvfile', getFile: getFileFromFileUri } //protocol cdvfile
|
{ protocol: 'cdvfile', getFile: getFileFromFileUri } //protocol cdvfile
|
||||||
]
|
]
|
||||||
|
|
||||||
function nthIndex(str, pat, n) {
|
function nthIndex(str, pat, n) {
|
||||||
var L = str.length, i = -1;
|
var L = str.length, i = -1;
|
||||||
while (n-- && i++ < L) {
|
while (n-- && i++ < L) {
|
||||||
i = str.indexOf(pat, i);
|
i = str.indexOf(pat, i);
|
||||||
if (i < 0) break;
|
if (i < 0) break;
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileFromApplicationUri(uri) {
|
function getFileFromApplicationUri(uri) {
|
||||||
/* bad path from a file entry due to the last '//'
|
/* bad path from a file entry due to the last '//'
|
||||||
example: ms-appdata:///local//path/to/file
|
example: ms-appdata:///local//path/to/file
|
||||||
*/
|
*/
|
||||||
var index = nthIndex(uri, "//", 3);
|
var index = nthIndex(uri, "//", 3);
|
||||||
var newUri = uri.substr(0, index) + uri.substr(index + 1);
|
var newUri = uri.substr(0, index) + uri.substr(index + 1);
|
||||||
|
|
||||||
var applicationUri = new Windows.Foundation.Uri(newUri);
|
var applicationUri = new Windows.Foundation.Uri(newUri);
|
||||||
|
|
||||||
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri);
|
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileFromFileUri(uri) {
|
function getFileFromFileUri(uri) {
|
||||||
/* uri example:
|
/* uri example:
|
||||||
cdvfile://localhost/persistent|temporary|another-fs-root/path/to/file
|
cdvfile://localhost/persistent|temporary|another-fs-root/path/to/file
|
||||||
*/
|
*/
|
||||||
var indexFrom = nthIndex(uri, "/", 3) + 1;
|
var indexFrom = nthIndex(uri, "/", 3) + 1;
|
||||||
var indexTo = nthIndex(uri, "/", 4);
|
var indexTo = nthIndex(uri, "/", 4);
|
||||||
var whichFolder = uri.substring(indexFrom, indexTo);
|
var whichFolder = uri.substring(indexFrom, indexTo);
|
||||||
var filePath = uri.substr(indexTo + 1);
|
var filePath = uri.substr(indexTo + 1);
|
||||||
var path = "\\" + filePath;
|
var path = "\\" + filePath;
|
||||||
|
|
||||||
if (whichFolder == "persistent") {
|
if (whichFolder == "persistent") {
|
||||||
path = Windows.Storage.ApplicationData.current.localFolder.path + path;
|
path = Windows.Storage.ApplicationData.current.localFolder.path + path;
|
||||||
}
|
}
|
||||||
else { //temporary, note: no roaming management
|
else { //temporary, note: no roaming management
|
||||||
path = Windows.Storage.ApplicationData.current.temporaryFolder.path + path;
|
path = Windows.Storage.ApplicationData.current.temporaryFolder.path + path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return getFileFromNativePath(path);
|
return getFileFromNativePath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileFromNativePath(path) {
|
function getFileFromNativePath(path) {
|
||||||
var nativePath = path.split("/").join("\\");
|
var nativePath = path.split("/").join("\\");
|
||||||
|
|
||||||
return Windows.Storage.StorageFile.getFileFromPathAsync(nativePath);
|
return Windows.Storage.StorageFile.getFileFromPathAsync(nativePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileLoaderForScheme(path) {
|
function getFileLoaderForScheme(path) {
|
||||||
var fileLoader = getFileFromNativePath;
|
var fileLoader = getFileFromNativePath;
|
||||||
|
|
||||||
schemes.some(function (scheme) {
|
schemes.some(function (scheme) {
|
||||||
return path.indexOf(scheme.protocol) === 0 ? ((fileLoader = scheme.getFile), true) : false;
|
return path.indexOf(scheme.protocol) === 0 ? ((fileLoader = scheme.getFile), true) : false;
|
||||||
});
|
});
|
||||||
|
|
||||||
return fileLoader;
|
return fileLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
try{
|
try{
|
||||||
Windows.System.Launcher.launchFileAsync(file, options).then(function (success) {
|
Windows.System.Launcher.launchFileAsync(file, options).then(function (success) {
|
||||||
successCallback();
|
successCallback();
|
||||||
}, function (error) {
|
}, function (error) {
|
||||||
errorCallback(error);
|
errorCallback(error);
|
||||||
});
|
});
|
||||||
}catch(error){
|
}catch(error){
|
||||||
errorCallback(error);
|
errorCallback(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}, function (error) {
|
}, function (error) {
|
||||||
console.log("Error while opening the file: "+error);
|
console.log("Error while opening the file: "+error);
|
||||||
errorCallback(error);
|
errorCallback(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require("cordova/exec/proxy").add("FileOpener2", module.exports);
|
require("cordova/exec/proxy").add("FileOpener2", module.exports);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user