From 3dfa7e94b1c9bb71620aad575c788bc51a67ee19 Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Thu, 17 Dec 2020 17:55:08 +0000 Subject: [PATCH] Add fallback for download.kiwix.org meta4 failure Former-commit-id: 833a1f6def0a0a68aef360c545598dff5cd422b5 [formerly c8f9471995a9e555beb125fbf0469aef8a44a1f1 [formerly b64a11cec15c5f7e5ecc938f9752e9aeda1469f7]] Former-commit-id: 3a758d8f82144a0d7eb199c9f754aa6d85ea9822 Former-commit-id: 90d0d9db4cca83ebc72658dfce1687ed75cf8b09 --- www/js/lib/kiwixServe.js | 47 ++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/www/js/lib/kiwixServe.js b/www/js/lib/kiwixServe.js index 32ab7b9b..c3188bc7 100644 --- a/www/js/lib/kiwixServe.js +++ b/www/js/lib/kiwixServe.js @@ -336,6 +336,9 @@ define([], function () { zea: 'Zeeuws' }; + var downloadLinks = document.getElementById('downloadLinks'); + var serverResponse = document.getElementById('serverResponse'); + // DEV: If you support more packaged files, add to this list var regexpFilter = /_medicine/.test(params.packagedFile) ? /^(?!.+_medicine_)[^_\n\r]+_([^_\n\r]+)_.+\.zi[mp].+$\s+/mig : null; regexpFilter = /wikivoyage/.test(params.packagedFile) ? /^(?!.+wikivoyage_)[^_\n\r]+_([^_\n\r]+)_.+\.zi[mp].+$\s+/mig : regexpFilter; @@ -352,20 +355,20 @@ define([], function () { return; } var xhttp = new XMLHttpRequest(); - //DEV: timeout set here to 20s; if this isn't long enough for your target countries, increase - var xhttpTimeout = setTimeout(ajaxTimeout, 20000); + //DEV: timeout set here to 20s (except for meta4 links); if this isn't long enough for your target countries, increase + var timeout = /\.meta4$/i.test(URL) ? 6000 : 20000; + var xhttpTimeout = setTimeout(ajaxTimeout, timeout); function ajaxTimeout() { xhttp.abort(); var responseMessageBox = document.getElementById('serverResponse'); responseMessageBox.innerHTML = "Connection attempt timed out (failed)"; if (/https?:|file:/.test(window.location.protocol)) responseMessageBox.innerHTML = "Browser's CORS Policy disallowed access!"; + if (/\.meta4$/i.test(URL)) responseMessageBox.innerHTML = "Archive descriptor xml file (meta4) is missing!"; document.getElementById('serverResponse').style.display = "inline"; - serverError(); + serverError(URL); return; } xhttp.onreadystatechange = function () { - var downloadLinks = document.getElementById('downloadLinks'); - var serverResponse = document.getElementById('serverResponse'); serverResponse.innerHTML = "Server response: 0 Waiting..."; serverResponse.style.display = "inline"; if (this.readyState == 4) { @@ -387,13 +390,33 @@ define([], function () { xhttp.open("GET", URL, true); xhttp.send(null); - function serverError() { - var errormessage = document.getElementById('downloadLinks'); - errormessage.innerHTML = '' + - '

Unable to access the server. Please see message below for reason.

' + - '

You can either try again or else open this link in a new browser window:
' + - '' + params.kiwixDownloadLink + '


'; - errormessage.style.display = "block"; + function serverError(URL) { + var requestedURL, altURL, torrentURL; + if (/\.meta4$/i.test(URL)) { + requestedURL = URL.replace(/\.meta4$/i, ''); + altURL = /wikipedia|wikisource|wikivoyage|wiktionary/i.test(URL) ? + requestedURL.replace(/(download\.kiwix\.org)/i, 'www.mirrorservice.org/sites/$1') : ''; + 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:'; + header.outerHTML = header.outerHTML.replace(/]*)>[\s\S]*?<\/pre>/i, '' + headerDoc + ''); + var body = document.getElementById('dl-panel-body'); + var bodyDoc = '

Directly download ZIM archive:

' + + '

' + requestedURL + '

' + + (altURL ? '

Possible mirror:

' + + '

' + altURL + '

' : '') + + '

Download with bittorrent:

' + + '

' + torrentURL + '

'; + body.outerHTML = body.outerHTML.replace(/]*)>[\s\S]*?<\/pre>/i, '' + bodyDoc + ''); + downloadLinks.innerHTML = downloadLinks.innerHTML.replace(/Index\s+of/ig, "File in"); + downloadLinks.innerHTML = downloadLinks.innerHTML.replace(/panel-success/i, "panel-warning"); + } else { + downloadLinks.innerHTML = '' + + '

Unable to access the server. Please see message below for reason.

' + + '

You can either try again or else open this link in a new browser window:
' + + '' + params.kiwixDownloadLink + '


'; + } + downloadLinks.style.display = "block"; } function processXhttpData(doc) {