Skip to content

Commit

Permalink
Merge pull request #279 from fefc/master
Browse files Browse the repository at this point in the history
Added minimal browser support.
  • Loading branch information
shnist authored Nov 26, 2019
2 parents dac2053 + f193cbd commit ff29e28
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
23 changes: 23 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 125 additions & 0 deletions src/browser/FileOpener2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
The MIT License (MIT)
Copyright (c) 2019 fefc - [email protected]
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 changes: 3 additions & 0 deletions src/browser/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions www/browser/isChrome.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit ff29e28

Please sign in to comment.