diff --git a/www/js/app.js b/www/js/app.js index 4c92f007..f19c621f 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -824,30 +824,14 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook cache.idxDB('pickedFileHandle', function(val) { if (val) { var handle = val; - // Check if we have persmission to open - handle.requestPermission({mode: 'read'}).then(function(readPermission) { - if (readPermission === 'granted') { - processNativeFileHandle(handle); - } else { - pickFileNativeFS(); - } - }).catch(function(err) { - console.error('Unable to load previously picked file', err); - pickFileNativeFS(); + cache.verifyPermission(handle, false).then(function(status) { + if (status) processNativeFileHandle(handle); }); - // handle.queryPermission({mode: 'read'}).then(function(permission) { - // if (permission === 'granted') { - // processNativeFileHandle(handle); - // } else if (permission === 'prompt') { - // // Do nothing - // } - // }).catch(function(err) { - // console.error('Unable to load previously picked file', err); - // }); } }); } }); + document.getElementById('btnAbout').addEventListener('click', function () { var btnAboutElement = document.getElementById('btnAbout'); if (/glyphicon-print/.test(btnAboutElement.innerHTML)) { diff --git a/www/js/lib/cache.js b/www/js/lib/cache.js index 5243d7f4..30efdcd2 100644 --- a/www/js/lib/cache.js +++ b/www/js/lib/cache.js @@ -625,6 +625,27 @@ define(['q', 'uiUtil'], function(Q, uiUtil) { return string; } + /** + * Provide + * + * @param {Object} fileHandle The file handle that we wish to verify with the Native Filesystem API + * @param {Boolean} withWrite Indicates read only or read/write persmissions + * @returns {Promise} A Promise for a Boolean value indicating whether permission has been granted or not + */ + function verifyPermission(fileHandle, withWrite) { + var opts = withWrite ? { mode: 'readwrite' } : {}; + return fileHandle.queryPermission(opts).then(function(permission) { + if (permission === "granted") return true; + return fileHandle.requestPermission(opts).then(function(permission) { + if (permission === 'granted') return true; + console.error('Permission for ' + fileHandle.name + ' was not granted: ' + permission); + return false; + }).catch(function(error) { + console.error('There was an error reading previously picked file ' + fileHandle.name, error); + }); + }); + }; + /** * Wraps a semaphor in a Promise. A function can signal that it is done by setting a sempahor to true, * if it has first set it to false at the outset of the procedure. Ensure no other functions use the same @@ -676,6 +697,7 @@ define(['q', 'uiUtil'], function(Q, uiUtil) { clear: clear, wait: wait, getItemFromCacheOrZIM: getItemFromCacheOrZIM, - replaceAssetRefsWithUri: replaceAssetRefsWithUri + replaceAssetRefsWithUri: replaceAssetRefsWithUri, + verifyPermission: verifyPermission }; }); \ No newline at end of file