|
| 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 ); |
0 commit comments