Skip to content

Commit ff29e28

Browse files
authored
Merge pull request #279 from fefc/master
Added minimal browser support.
2 parents dac2053 + f193cbd commit ff29e28

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

plugin.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,27 @@
6262
<merges target="" />
6363
</js-module>
6464
</platform>
65+
66+
<!-- browser -->
67+
<platform name="browser">
68+
<config-file parent="/*" target="config.xml">
69+
<feature name="FileOpener2">
70+
<param name="browser-package" value="FileOpener2"/>
71+
</feature>
72+
</config-file>
73+
74+
<!-- Required for browserify: we always link module below as there is conditional reference
75+
to this module from requestFileSystem and resolveLocalFileSystemURI modules. -->
76+
<js-module src="www/browser/isChrome.js" name="isChrome">
77+
<runs />
78+
</js-module>
79+
80+
<js-module src="src/browser/FileSaver.min.js" name="FileSaver">
81+
<clobbers target="FileSaver"/>
82+
</js-module>
83+
84+
<js-module src="src/browser/FileOpener2.js" name="FileOpener2Proxy">
85+
<runs/>
86+
</js-module>
87+
</platform>
6588
</plugin>

src/browser/FileOpener2.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 fefc - [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
const cacheDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/temporary/' : 'file:///temporary/';
25+
const dataDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/persistent/' : 'file:///persistent/';
26+
27+
function open(successCallback, errorCallback, data) {
28+
var fullFilePath = data[0];
29+
//var contentType = data[1]; //Not needed in browser
30+
//var openDialog = data[2]; //Not needed in browser
31+
32+
var dirPath = fullFilePath.substring(0, fullFilePath.lastIndexOf('/') + 1);
33+
var fileName = fullFilePath.substring(fullFilePath.lastIndexOf('/') + 1, fullFilePath.length);
34+
var fileSystemLocalPath = getLocalPathAndFileSystem(dirPath);
35+
36+
if (!fileSystemLocalPath.error) {
37+
window.requestFileSystem(fileSystemLocalPath.fileSystem, 0, (fs) => {
38+
readFile(fs.root, fileSystemLocalPath.localPath + fileName).then((blob) => {
39+
FileSaver.saveAs(blob, fileName);
40+
successCallback();
41+
}).catch((error) => {
42+
errorCallback(error);
43+
});
44+
}, (error) => {
45+
errorCallback(error);
46+
});
47+
} else {
48+
errorCallback('INVALID_PATH');
49+
}
50+
}
51+
52+
/**
53+
*
54+
* Gets the localPath according to the fileSystem (TEMPORARY or PERSISTENT).
55+
*
56+
* @param {String} Path to the file or directory to check
57+
* @returns {Object} value with informations to requestFileSystem later
58+
* @returns {string} value.localPath The localPath in relation with fileSystem.
59+
* @returns {number} value.fileSystem the fileSystem (TEMPORARY or PERSISTENT).
60+
* @returns {error} value.error if the path is not valid.
61+
* @returns {message} value.message error message.
62+
*/
63+
function getLocalPathAndFileSystem(pathToCheck) {
64+
let ret = {
65+
localPath: '',
66+
fileSystem: window.TEMPORARY
67+
};
68+
69+
if (pathToCheck.startsWith(cacheDirectory)) {
70+
ret.localPath = pathToCheck.replace(cacheDirectory, '');
71+
ret.fileSystem = window.TEMPORARY;
72+
73+
} else if (pathToCheck.startsWith(dataDirectory)) {
74+
ret.localPath = pathToCheck.replace(dataDirectory, '');
75+
ret.fileSystem = window.PERSISTENT;
76+
77+
} else {
78+
return {error: true, message: 'INVALID_PATH'};
79+
}
80+
81+
if (!ret.localPath.endsWith('/')) ret.localPath += '/';
82+
83+
return ret;
84+
}
85+
86+
/**
87+
*
88+
* Reads a file in the fileSystem as an DataURL.
89+
*
90+
* @param {String} Root is the root folder of the fileSystem.
91+
* @param {String} Path is the file to be red.
92+
* @returns {Promise} which resolves with an Object containing DataURL, rejects if something went wrong.
93+
*/
94+
function readFile(root, filePath) {
95+
return new Promise((resolve, reject) => {
96+
if (filePath.startsWith('/')) filePath = filePath.substring(1);
97+
98+
root.getFile(filePath, {}, (fileEntry) => {
99+
fileEntry.file((file) => {
100+
let reader = new FileReader();
101+
102+
reader.onload = function() {
103+
resolve(reader.result);
104+
};
105+
106+
reader.onerror = function() {
107+
reject(reader.error);
108+
}
109+
110+
reader.readAsDataURL(file);
111+
112+
}, (error) => {
113+
reject(error);
114+
});
115+
}, (error) => {
116+
reject(error);
117+
});
118+
});
119+
}
120+
121+
module.exports = {
122+
open: open
123+
};
124+
125+
require( "cordova/exec/proxy" ).add( "FileOpener2", module.exports );

src/browser/FileSaver.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

www/browser/isChrome.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*
20+
*/
21+
22+
module.exports = function () {
23+
// window.webkitRequestFileSystem and window.webkitResolveLocalFileSystemURL are available only in Chrome and
24+
// possibly a good flag to indicate that we're running in Chrome
25+
return window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
26+
};

0 commit comments

Comments
 (0)