Merge pull request #279 from fefc/master

Added minimal browser support.
This commit is contained in:
Aaron Faber 2019-11-26 16:34:38 +00:00 committed by GitHub
commit ff29e28ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 177 additions and 0 deletions

View File

@ -62,4 +62,27 @@
<merges target="" />
</js-module>
</platform>
<!-- browser -->
<platform name="browser">
<config-file parent="/*" target="config.xml">
<feature name="FileOpener2">
<param name="browser-package" value="FileOpener2"/>
</feature>
</config-file>
<!-- Required for browserify: we always link module below as there is conditional reference
to this module from requestFileSystem and resolveLocalFileSystemURI modules. -->
<js-module src="www/browser/isChrome.js" name="isChrome">
<runs />
</js-module>
<js-module src="src/browser/FileSaver.min.js" name="FileSaver">
<clobbers target="FileSaver"/>
</js-module>
<js-module src="src/browser/FileOpener2.js" name="FileOpener2Proxy">
<runs/>
</js-module>
</platform>
</plugin>

125
src/browser/FileOpener2.js Normal file
View File

@ -0,0 +1,125 @@
/*
The MIT License (MIT)
Copyright (c) 2019 fefc - fefc.dev@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const cacheDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/temporary/' : 'file:///temporary/';
const dataDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/persistent/' : 'file:///persistent/';
function open(successCallback, errorCallback, data) {
var fullFilePath = data[0];
//var contentType = data[1]; //Not needed in browser
//var openDialog = data[2]; //Not needed in browser
var dirPath = fullFilePath.substring(0, fullFilePath.lastIndexOf('/') + 1);
var fileName = fullFilePath.substring(fullFilePath.lastIndexOf('/') + 1, fullFilePath.length);
var fileSystemLocalPath = getLocalPathAndFileSystem(dirPath);
if (!fileSystemLocalPath.error) {
window.requestFileSystem(fileSystemLocalPath.fileSystem, 0, (fs) => {
readFile(fs.root, fileSystemLocalPath.localPath + fileName).then((blob) => {
FileSaver.saveAs(blob, fileName);
successCallback();
}).catch((error) => {
errorCallback(error);
});
}, (error) => {
errorCallback(error);
});
} else {
errorCallback('INVALID_PATH');
}
}
/**
*
* Gets the localPath according to the fileSystem (TEMPORARY or PERSISTENT).
*
* @param {String} Path to the file or directory to check
* @returns {Object} value with informations to requestFileSystem later
* @returns {string} value.localPath The localPath in relation with fileSystem.
* @returns {number} value.fileSystem the fileSystem (TEMPORARY or PERSISTENT).
* @returns {error} value.error if the path is not valid.
* @returns {message} value.message error message.
*/
function getLocalPathAndFileSystem(pathToCheck) {
let ret = {
localPath: '',
fileSystem: window.TEMPORARY
};
if (pathToCheck.startsWith(cacheDirectory)) {
ret.localPath = pathToCheck.replace(cacheDirectory, '');
ret.fileSystem = window.TEMPORARY;
} else if (pathToCheck.startsWith(dataDirectory)) {
ret.localPath = pathToCheck.replace(dataDirectory, '');
ret.fileSystem = window.PERSISTENT;
} else {
return {error: true, message: 'INVALID_PATH'};
}
if (!ret.localPath.endsWith('/')) ret.localPath += '/';
return ret;
}
/**
*
* Reads a file in the fileSystem as an DataURL.
*
* @param {String} Root is the root folder of the fileSystem.
* @param {String} Path is the file to be red.
* @returns {Promise} which resolves with an Object containing DataURL, rejects if something went wrong.
*/
function readFile(root, filePath) {
return new Promise((resolve, reject) => {
if (filePath.startsWith('/')) filePath = filePath.substring(1);
root.getFile(filePath, {}, (fileEntry) => {
fileEntry.file((file) => {
let reader = new FileReader();
reader.onload = function() {
resolve(reader.result);
};
reader.onerror = function() {
reject(reader.error);
}
reader.readAsDataURL(file);
}, (error) => {
reject(error);
});
}, (error) => {
reject(error);
});
});
}
module.exports = {
open: open
};
require( "cordova/exec/proxy" ).add( "FileOpener2", module.exports );

3
src/browser/FileSaver.min.js vendored Normal file
View File

@ -0,0 +1,3 @@
(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(b,c,d){var e=new XMLHttpRequest;e.open("GET",b),e.responseType="blob",e.onload=function(){a(e.response,c,d)},e.onerror=function(){console.error("could not download file")},e.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=f.saveAs||("object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(a,b,d,e){if(e=e||open("","_blank"),e&&(e.document.title=e.document.body.innerText="downloading..."),"string"==typeof a)return c(a,b,d);var g="application/octet-stream"===a.type,h=/constructor/i.test(f.HTMLElement)||f.safari,i=/CriOS\/[\d]+/.test(navigator.userAgent);if((i||g&&h)&&"undefined"!=typeof FileReader){var j=new FileReader;j.onloadend=function(){var a=j.result;a=i?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),e?e.location.href=a:location=a,e=null},j.readAsDataURL(a)}else{var k=f.URL||f.webkitURL,l=k.createObjectURL(a);e?e.location=l:location.href=l,e=null,setTimeout(function(){k.revokeObjectURL(l)},4E4)}});f.saveAs=a.saveAs=a,"undefined"!=typeof module&&(module.exports=a)});
//# sourceMappingURL=FileSaver.min.js.map

26
www/browser/isChrome.js Normal file
View File

@ -0,0 +1,26 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
module.exports = function () {
// window.webkitRequestFileSystem and window.webkitResolveLocalFileSystemURL are available only in Chrome and
// possibly a good flag to indicate that we're running in Chrome
return window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
};