Skip to content

Commit

Permalink
Provide list of mirrors for fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid committed Dec 1, 2024
1 parent fb058cb commit 24f640f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions archives/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you have a distribution of this app and want to delete the packaged archive t
params['packagedFile'] = "name_of_your_file.zim";
params['fileVersion'] = "descriptive_name_of_your_file (Jan-2020)"; // These values will show in the app
params['cachedStartPages'] = false;
params['kiwixDownloadLink'] = "https://download.kiwix.org/zim/"; //Include final slash
params['kiwixDownloadServer'] = "https://download.kiwix.org/zim/"; //Include final slash
```

You can have more than one ZIM archive in `archives`, but only one will launch on app startup as the packaged file. If you do have more than one, then be sure to set `params['showFileSelectors'] = true;` to aid in discoverability of the other archive, otherwise your users will not easily realize it is there. Keeping that parameter as `false` dedicates the app to the packaged archive, but users can still override and pick a different ZIM by changing the value in Expert Settings (Config).
Expand All @@ -21,4 +21,4 @@ You can have more than one ZIM archive in `archives`, but only one will launch o

If you are building a custom packaged version of the app, then remember that ZIMs will not appear in your online GitHub repo, so add a text file to `archives` to show the intended filename, like the `*.zim.txt` file in this directory, e.g. `wikivoyage_en_all_novid_2019-07.zim.txt`. The text file can be empty (0 bytes). You should also set the fields listed above in `www/js/init.js` to match, and additionally decide whether to set `params['showFileSelectors'] = false;` (if you want to simplify the interface in Config).

Advanced: If you wish to restrict the files that users can search for on the server, e.g. to ensure your app remains dedicated to WikiMed archives, and to aid discoverability of only those archives, then look in `kiwixServe.js` and search for `DEV:` (first comment labelled `DEV:`) for more info. Also, ensure `params['kiwixDownloadLink'] = "https://download.kiwix.org/zim/";` is set appropriately so that the download library opens in the directory where multilingual or updated versions of your packaged ZIM can be found.
Advanced: If you wish to restrict the files that users can search for on the server, e.g. to ensure your app remains dedicated to WikiMed archives, and to aid discoverability of only those archives, then look in `kiwixServe.js` and search for `DEV:` (first comment labelled `DEV:`) for more info. Also, ensure `params['kiwixDownloadServer'] = "https://download.kiwix.org/zim/";` is set appropriately so that the download library opens in the directory where multilingual or updated versions of your packaged ZIM can be found.
4 changes: 2 additions & 2 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let imageDisplay = 'all';

// Kiwix ZIM Archive Download Server and release update server in regex form
// DEV: The server URL is defined in init.js, but is not available to us in SW
const regexpKiwixDownloadLinks = /download\.kiwix\.org|api\.github\.com/i;
const regexpkiwixDownloadServers = /download\.kiwix\.org|api\.github\.com/i;

/**
* A global Boolean that records whether the ReplayWorker is available
Expand Down Expand Up @@ -379,7 +379,7 @@ self.addEventListener('fetch', function (event) {
if (!(regexpZIMUrlWithNamespace.test(strippedUrl) && /\.zim\//i.test(strippedUrl))) return;
}
// Don't cache download links
if (regexpKiwixDownloadLinks.test(rqUrl)) return;
if (regexpkiwixDownloadServers.test(rqUrl)) return;
// Select cache depending on request format
var cache = /\.zim\//i.test(strippedUrl) ? ASSETS_CACHE : APP_CACHE;
cache = /youtube|vimeo/i.test(strippedUrl) ? ASSETS_CACHE : cache;
Expand Down
2 changes: 1 addition & 1 deletion www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ document.getElementById('btnRefresh').addEventListener('click', function () {
}
});
document.getElementById('downloadTrigger').addEventListener('click', function () {
kiwixServe.requestXhttpData(params.kiwixDownloadLink);
kiwixServe.requestXhttpData(params.kiwixDownloadServer);
});
document.querySelectorAll('input[name="contentInjectionMode"][type="radio"]').forEach(function (element) {
element.addEventListener('change', function () {
Expand Down
5 changes: 3 additions & 2 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ params['cachedStartPages'] = {
wikivoyage_en_all_maxi: 'A/Main_Page'
};
params['win7ElectronVersion'] = '22.3'; // KEEP UP TO DATE!!! This is the last minor version to support Win 7/8/8.1. Auto-update is embargoed for values starting with this.
params['kiwixDownloadLink'] = 'https://download.kiwix.org/zim/'; // Include final slash
params['kiwixHiddenDownloadLink'] = 'https://master.download.kiwix.org/zim/';
params['kiwixDownloadServer'] = 'https://download.kiwix.org/zim/'; // Include final slash
params['kiwixDownloadMirrors'] = ['https://ftp.fau.de/kiwix/zim/', 'https://mirrors.dotsrc.org/kiwix/zim/', 'https://www.mirrorservice.org/sites/download.kiwix.org/zim/', 'https://md.mirrors.hacktegic.com/kiwix-md/zim/', 'https://library.kiwix.org'];
params['kiwixhiddenDownloadServer'] = 'https://master.download.kiwix.org/zim/';
/** ***** DEV: ENSURE SERVERS BELOW ARE LISTED IN package.appxmanifest ************/
params['PWAServer'] = 'https://pwa.kiwix.org/'; // Production server
// params['PWAServer'] = 'https://kiwix.github.io/kiwix-js-pwa/dist/'; // Test server
Expand Down
4 changes: 2 additions & 2 deletions www/js/lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,12 @@ function verifyPermission (fileHandle, withWrite) {
* Download an archive directly into the picked folder (primarily for use with the Origin Private File System)
*
* @param {String} archiveName The name of the archive to download (will be used as the filename)
* @param {String} archiveUrl An optional URL to download the archive from (if not supplied, will use params.kiwixDownloadLink)
* @param {String} archiveUrl An optional URL to download the archive from (if not supplied, will use params.kiwixDownloadServer)
* @param {Function} callback Callback function to report the progress of the download
* @returns {Promise<FileSystemFileHandle>} A Promise for a FileHandle object representing the downloaded file
*/
function downloadArchiveToPickedFolder (archiveName, archiveUrl, callback) {
archiveUrl = archiveUrl || params.kiwixDownloadLink + archiveName;
archiveUrl = archiveUrl || params.kiwixDownloadServer + archiveName;
if (params.pickedFolder && params.pickedFolder.getFileHandle) {
return verifyPermission(params.pickedFolder, true).then(function (permission) {
if (permission) {
Expand Down
17 changes: 10 additions & 7 deletions www/js/lib/kiwixServe.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function requestXhttpData (URL, lang, subj, kiwixDate) {
torrentURL = URL.replace(/\.meta4$/i, '.torrent');
var header = document.getElementById('dl-panel-heading');
var headerDoc = 'There is a server issue, but please try the following links to your file:';
if (~URL.indexOf(params.kiwixHiddenDownloadLink)) {
if (~URL.indexOf(params.kiwixhiddenDownloadServer)) {
headerDoc = 'This file is only available via browser-managed download:';
altURL = requestedURL.replace(/\/master\./i, '/mirror.');
}
Expand All @@ -490,7 +490,7 @@ function requestXhttpData (URL, lang, subj, kiwixDate) {
'<p><a href="' + requestedURL + '"' + target + ' class="download">' + requestedURL + '</a></p>' +
(altURL ? '<p><b>Possible mirror:</b></p>' +
'<p><a href="' + altURL + '"' + target + ' class="download">' + altURL + '</a></p>' : '') +
(~URL.indexOf(params.kiwixHiddenDownloadLink) ? ''
(~URL.indexOf(params.kiwixhiddenDownloadServer) ? ''
: '<p><b>Download with bittorrent:</b></p>' +
'<p><a href="' + torrentURL + '"' + target + '>' + torrentURL + '</a></p>');
body.outerHTML = body.outerHTML.replace(/<pre\b([^>]*)>[\s\S]*?<\/pre>/i, '<div$1>' + bodyDoc + '</div>');
Expand All @@ -515,8 +515,11 @@ function requestXhttpData (URL, lang, subj, kiwixDate) {
} else {
downloadLinks.innerHTML = '<span style="font-weight:bold;font-family:consolas,monospace;">' +
'<p style="color:salmon;">Unable to access the server. Please see message below for reason.</p>' +
'<p>You can either try again or else open this link in a new browser window:<br />' +
'<a href="' + params.kiwixDownloadLink + '"' + target + '>' + params.kiwixDownloadLink + '</a></p><br /></span>';
'<p>You can either try again or else open one of these mirror links in a new browser window:</p><ul>';
params.kiwixDownloadMirrors.forEach(function (mirror) {
downloadLinks.innerHTML += '<li><a href="' + mirror + '"' + target + '>' + mirror + '</a></li>';
});
downloadLinks.innerHTML += '</ul></span><br />';
}
downloadLinks.style.display = 'block';
}
Expand Down Expand Up @@ -938,14 +941,14 @@ function requestXhttpData (URL, lang, subj, kiwixDate) {
var dateID = dateSel ? dateSel.value : '';
var subjID = subjSel ? subjSel.value : '';
var replaceURL = URL + this.dataset.kiwixDl;
replaceURL = /(custom_apps|endless|dev)\//.test(this.text) ? params.kiwixHiddenDownloadLink + '.hidden/' + this.text : replaceURL;
replaceURL = /(archive)\//.test(this.text) ? params.kiwixDownloadLink.replace(/\/zim\//, '/archive/zim/') : replaceURL;
replaceURL = /(custom_apps|endless|dev)\//.test(this.text) ? params.kiwixhiddenDownloadServer + '.hidden/' + this.text : replaceURL;
replaceURL = /(archive)\//.test(this.text) ? params.kiwixDownloadServer.replace(/\/zim\//, '/archive/zim/') : replaceURL;
// Allow both zim and zip format
if (/\.zi[mp]$/i.test(this.dataset.kiwixDl)) {
replaceURL = replaceURL + '.meta4';
} else if (/parent\s*directory|\.\.\//i.test(this.text)) {
replaceURL = URL.replace(/\/[^/]*\/$/i, '/');
replaceURL = replaceURL.replace(params.kiwixHiddenDownloadLink, params.kiwixDownloadLink);
replaceURL = replaceURL.replace(params.kiwixhiddenDownloadServer, params.kiwixDownloadServer);
replaceURL = replaceURL.replace(/\.hidden\//, '');
replaceURL = replaceURL.replace(/\/archive\/$/, '/zim/');
} else if (/Name|Size|Last\smodified|Description/.test(this.text)) {
Expand Down

0 comments on commit 24f640f

Please sign in to comment.