diff --git a/www/js/app.js b/www/js/app.js index d866a52b..02f45de8 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -3746,49 +3746,23 @@ function processNativeDirHandle (dirHandle, callback) { }); params.pickedFolder = dirHandle; params.pickedFile = ''; - // We have to iterate async function with Promise because IE11 compiler throws error if we use async - var archiveList = []; - var entryList = dirHandle.entries(); - (function iterateAsyncDirEntryArray () { - return entryList.next().then(function (result) { - if (!result.done) { - var entry = result.value[1]; - if (/\.zim(\w\w)?$/.test(entry.name)) { - if (callback) archiveList.push(entry); - // Hide all parts of split file except first in UI - else if (/\.zim(aa)?$/.test(entry.name)) archiveList.push(entry.name); - if (!params.pickedFolder.path) { - entry.getFile().then(function (file) { - params.pickedFolder.path = file.path; - }); - } - } - iterateAsyncDirEntryArray(); - } else { - var noZIMFound = document.getElementById('noZIMFound'); - if (archiveList.length) { - if (callback) { - callback(archiveList); - } else { - noZIMFound.style.display = 'none'; - populateDropDownListOfArchives(archiveList); - } - } else { - if (callback) { - callback(null); - } else { - noZIMFound.style.display = 'block'; - populateDropDownListOfArchives(archiveList, true); - } - } - } - }).catch(function (err) { - uiUtil.systemAlert('

We could not find your archive! Is the location or file still available? Try picking the file or folder again.

' + - '

[System error message: ' + err.message + ']

', 'Error!'); - }); - })(); var archiveDisplay = document.getElementById('chooseArchiveFromLocalStorage'); archiveDisplay.style.display = 'block'; + var iterableEntryList = dirHandle.entries(); + return cache.iterateAsyncDirEntries(iterableEntryList, [], !!callback).then(function (archiveList) { + var noZIMFound = document.getElementById('noZIMFound'); + var hasArchives = archiveList.length > 0; + if (!hasArchives) console.warn('No archives found in directory ' + dirHandle.name); + if (callback) { + callback(archiveList); + } else { + noZIMFound.style.display = hasArchives ? 'none' : 'block'; + populateDropDownListOfArchives(archiveList, !hasArchives); + } + }).catch(function (err) { + uiUtil.systemAlert('

We could not find your archive! Is the location or file still available? Try picking the file or folder again.

' + + '

[System error message: ' + err.message + ']

', 'Error!'); + }); } function scanNodeFolderforArchives (folder, callback) { diff --git a/www/js/lib/cache.js b/www/js/lib/cache.js index 891d2f39..c1616643 100644 --- a/www/js/lib/cache.js +++ b/www/js/lib/cache.js @@ -880,6 +880,39 @@ function deleteOPFSEntry (name) { } } +/** + * Iterates an iterable entry list of files using the File System API and returns an array of entries found + * + * @param {Iterator} entries An asychronous iterator of entries derived from a directory handle + * @param {Array} archives An array to which to add the entries (may be an empty array) + * @param {Boolean} noFilter An optional flag to indicate that no filtering should be applied to the entries + * @returns {Promise} A Promise for an array of entries in the file system directory + */ +function iterateAsyncDirEntries (entries, archives, noFilter) { + return entries.next().then(function (result) { + if (!result.done) { + var entry = result.value[1]; + if (/\.zim(\w\w)?$/.test(entry.name)) { + if (noFilter) archives.push(entry); + // Hide all parts of split file except first in UI + else if (/\.zim(aa)?$/.test(entry.name)) archives.push(entry.name); + // In an Electron app, we should be able to get the path of the files + if (window.fs && !params.pickedFolder.path) { + entry.getFile().then(function (file) { + params.pickedFolder.path = file.path; + }); + } + } + return iterateAsyncDirEntries(entries, archives, noFilter); + } else { + // We've processed all the entries + return archives; + } + }).catch(function (err) { + throw err; + }); +} + /** * Iterates the OPFS file system and returns an array of entries found * @@ -976,6 +1009,7 @@ export default { importOPFSEntries: importOPFSEntries, exportOPFSEntry: exportOPFSEntry, deleteOPFSEntry: deleteOPFSEntry, + iterateAsyncDirEntries: iterateAsyncDirEntries, iterateOPFSEntries: iterateOPFSEntries, populateOPFSStorageQuota: populateOPFSStorageQuota };