From 6a7aaf6ad07b2c6fa378f4fe4cbb1aa92c63846e Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Thu, 14 Jan 2021 11:07:56 +0000 Subject: [PATCH] Delete deprecated idxDB databases if possible Former-commit-id: 5eb3b04a137d525e1a2af3765c77ba309fde71b4 [formerly b1957276bedcd17227f16c1422ca4c87c0b328fd] [formerly 31426d4308472b82aa9fa9aee836fec0a2ace6c6] [formerly 1f27164b9fb5f9674a081c6ea1f783b6f79a41fc [formerly 1fe9a54a409fd9c7fce2eff03aac496dfb2846ed [formerly 617fac3bd55b852f18d0b0135e29ebf1b3dbafe4]]] Former-commit-id: abd408ba23fb78e1715439be87c5ee0b1e78bcea [formerly ca05b66dbf2d1e1a18dd6ebd1ce46caaa012018f [formerly 0f082ff7d99dec559d5c1b92ad87ce5c0c8e8d95]] Former-commit-id: c48939cd33def4e3d788238c6368667b270762f7 [formerly caf383f92cb055be47470d92797d9b0bf87bd24d] Former-commit-id: 6181f792d034406de65711001eac29b33378e177 --- pwabuilder-sw.js | 2 +- www/index.html | 2 +- www/js/app.js | 14 +++++++++++--- www/js/init.js | 2 +- www/js/lib/cache.js | 38 ++++++++++++++++++++++++++++++-------- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/pwabuilder-sw.js b/pwabuilder-sw.js index ea4c0221..f6098650 100644 --- a/pwabuilder-sw.js +++ b/pwabuilder-sw.js @@ -4,7 +4,7 @@ // App version number - ENSURE IT MATCHES VALUE IN init.js // DEV: Changing this will cause the browser to recognize that the Service Worker has changed, and it will download and // install a new copy -const appVersion = '1.1.4-RP5'; +const appVersion = '1.1.4-RP6'; // Kiwix ZIM Archive Download Server in regex form // DEV: The server URL is defined in init.js, but is not available to us in SW diff --git a/www/index.html b/www/index.html index 14d0d5fb..d7d9d48f 100644 --- a/www/index.html +++ b/www/index.html @@ -529,7 +529,7 @@ - +
Tap to select app's local storage or to select a file or folderTap to select app's local storage or to select a file or folder. You can also drag and drop a ZIM file into the app.
diff --git a/www/js/app.js b/www/js/app.js index 03641589..9b00f2e3 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1368,12 +1368,20 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett //Code below triggers display of modal info box if app is run for the first time, or it has been upgraded to new version if (settingsStore.getItem('version') !== params.version) { firstRun = true; - // Make user re-pick file if we have upgraded (it's less confusing than filehandle errors) - settingsStore.removeItem('listOfArchives'); + + // Make user re-pick file if we have upgraded (it's less confusing than filehandle errors) - NOT NEEDED, remove in due course + // settingsStore.removeItem('listOfArchives'); + + // Update the installed version if (params.PWAInstalled) { params.PWAInstalled = params.version; settingsStore.setItem('PWAInstalled', params.PWAInstalled); - } + } + // One-time cleanup of idxDB files to delete deprecated databases if possible + cache.idxDB('deleteNonCurrent', function (result) { + if (result === false) console.log('Unable to delete old idxDB databases (this is normal in non-Chromium browsers'); + else console.log('Deleted ' + result + ' deprecated database(s).'); + }); // On some platforms, bootstrap's jQuery functions have not been injected yet, so we have to run in a timeout setTimeout(function () { $('#myModal').modal({ diff --git a/www/js/init.js b/www/js/init.js index 0aa42746..329f2da4 100644 --- a/www/js/init.js +++ b/www/js/init.js @@ -49,7 +49,7 @@ var params = {}; */ var appstate = {}; /******** UPDATE VERSION IN pwabuilder-sw.js TO MATCH VERSION *******/ -params['version'] = "1.1.4-RP5"; //DEV: Manually update this version when there is a new release: it is compared to the Settings Store "version" in order to show first-time info, and the cookie is updated in app.js +params['version'] = "1.1.4-RP6"; //DEV: Manually update this version when there is a new release: it is compared to the Settings Store "version" in order to show first-time info, and the cookie is updated in app.js /******* UPDATE THIS ^^^^^^ IN serveice worker!! ********************/ params['packagedFile'] = "wikipedia_en_100_maxi.zim"; //For packaged Kiwix JS (e.g. with Wikivoyage file), set this to the filename (for split files, give the first chunk *.zimaa) and place file(s) in default storage params['archivePath'] = "archives"; //The directory containing the packaged archive(s) (relative to app's root directory) diff --git a/www/js/lib/cache.js b/www/js/lib/cache.js index f70c4e2c..5d1be7fe 100644 --- a/www/js/lib/cache.js +++ b/www/js/lib/cache.js @@ -23,7 +23,8 @@ 'use strict'; define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { - const CACHE = 'kiwix-precache-' + params.version; // Set the database or cache name here + const CACHEAPI = 'kiwix-precache-' + params.version; // Set the database or cache name here + const CACHEIDB = 'kiwix-assetsCache'; // For idxDB we don't want the name to change var objStore = 'kiwix-assets'; // Name of the object store // DEV: Regex below defines the permitted key types for the cache; add further types as needed @@ -111,7 +112,7 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { case 'cacheAPI': type = 'cacheAPI'; description = 'CacheAPI'; - caches.open(CACHE).then(function (cache) { + caches.open(CACHEAPI).then(function (cache) { cache.keys().then(function (keys) { callback({'type': type, 'description': description, 'count': keys.length}); }); @@ -134,7 +135,8 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { * on the database * * @param {String} keyOrCommand The key of the value to be written or read, or commands 'clear' (clears objStore), - * 'count' (counts number of objects in objStore), 'delete' (deletes a record with key passed in valueOrCallback) + * 'count' (counts number of objects in objStore), 'delete' (deletes a record with key passed in valueOrCallback), + * 'deleteNonCurrent' (deletes all databases that do not match CACHEIDB - but only works in Chromium currently) * @param {Variable} valueOrCallback The value to write, or a callback function for read and command transactions * @param {Function} callback Callback for write transactions only */ @@ -145,9 +147,29 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { rtnFn(false); return; } + + // Delete all non-curren IdxDB databases (only works in Chromium currently) + if (keyOrCommand === 'deleteNonCurrent') { + if (indexedDB.databases) { + var result = 0; + indexedDB.databases().then(function (dbs) { + dbs.forEach(function (db) { + if (db.name !== CACHEIDB) { + result++; + indexedDB.deleteDatabase(db.name); + } + }); + }).then(function () { + rtnFn(result); + }); + } else { + rtnFn(false); + } + return; + } // Open (or create) the database - var open = indexedDB.open(CACHE, 1); + var open = indexedDB.open(CACHEIDB, 1); open.onerror = function(e) { // Suppress error reporting if testing (older versions of Firefox support indexedDB but cannot use it with @@ -222,14 +244,14 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { var rtnFn = callback || valueOrCallback; // Process commands if (keyOrCommand === 'clear') { - caches.delete(CACHE).then(rtnFn); + caches.delete(CACHEAPI).then(rtnFn); } else if (keyOrCommand === 'delete') { - caches.open(CACHE).then(function(cache) { + caches.open(CACHEAPI).then(function(cache) { cache.delete(value).then(rtnFn); }); } else if (value === null) { // Request retrieval of data - caches.open(CACHE).then(function(cache) { + caches.open(CACHEAPI).then(function(cache) { cache.match('../' + keyOrCommand).then(function(response) { if (!response) { rtnFn(null); @@ -245,7 +267,7 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { }); } else { // Request storing of data in cache - caches.open(CACHE).then(function(cache) { + caches.open(CACHEAPI).then(function(cache) { // Construct a Response from value var response = new Response(value); cache.put('../' + keyOrCommand, response).then(function() {