mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-10 04:40:27 -04:00
Add fallback for download.kiwix.org meta4 failure
Former-commit-id: 833a1f6def0a0a68aef360c545598dff5cd422b5 [formerly c8f9471995a9e555beb125fbf0469aef8a44a1f1 [formerly b64a11cec15c5f7e5ecc938f9752e9aeda1469f7]] Former-commit-id: 3a758d8f82144a0d7eb199c9f754aa6d85ea9822 Former-commit-id: 90d0d9db4cca83ebc72658dfce1687ed75cf8b09
This commit is contained in:
parent
93e8d92e2a
commit
3dfa7e94b1
@ -336,6 +336,9 @@ define([], function () {
|
|||||||
zea: 'Zeeuws'
|
zea: 'Zeeuws'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var downloadLinks = document.getElementById('downloadLinks');
|
||||||
|
var serverResponse = document.getElementById('serverResponse');
|
||||||
|
|
||||||
// DEV: If you support more packaged files, add to this list
|
// 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;
|
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;
|
regexpFilter = /wikivoyage/.test(params.packagedFile) ? /^(?!.+wikivoyage_)[^_\n\r]+_([^_\n\r]+)_.+\.zi[mp].+$\s+/mig : regexpFilter;
|
||||||
@ -352,20 +355,20 @@ define([], function () {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var xhttp = new XMLHttpRequest();
|
var xhttp = new XMLHttpRequest();
|
||||||
//DEV: timeout set here to 20s; if this isn't long enough for your target countries, increase
|
//DEV: timeout set here to 20s (except for meta4 links); if this isn't long enough for your target countries, increase
|
||||||
var xhttpTimeout = setTimeout(ajaxTimeout, 20000);
|
var timeout = /\.meta4$/i.test(URL) ? 6000 : 20000;
|
||||||
|
var xhttpTimeout = setTimeout(ajaxTimeout, timeout);
|
||||||
function ajaxTimeout() {
|
function ajaxTimeout() {
|
||||||
xhttp.abort();
|
xhttp.abort();
|
||||||
var responseMessageBox = document.getElementById('serverResponse');
|
var responseMessageBox = document.getElementById('serverResponse');
|
||||||
responseMessageBox.innerHTML = "Connection attempt timed out (failed)";
|
responseMessageBox.innerHTML = "Connection attempt timed out (failed)";
|
||||||
if (/https?:|file:/.test(window.location.protocol)) responseMessageBox.innerHTML = "Browser's CORS Policy disallowed access!";
|
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";
|
document.getElementById('serverResponse').style.display = "inline";
|
||||||
serverError();
|
serverError(URL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
xhttp.onreadystatechange = function () {
|
xhttp.onreadystatechange = function () {
|
||||||
var downloadLinks = document.getElementById('downloadLinks');
|
|
||||||
var serverResponse = document.getElementById('serverResponse');
|
|
||||||
serverResponse.innerHTML = "Server response: 0 Waiting...";
|
serverResponse.innerHTML = "Server response: 0 Waiting...";
|
||||||
serverResponse.style.display = "inline";
|
serverResponse.style.display = "inline";
|
||||||
if (this.readyState == 4) {
|
if (this.readyState == 4) {
|
||||||
@ -387,13 +390,33 @@ define([], function () {
|
|||||||
xhttp.open("GET", URL, true);
|
xhttp.open("GET", URL, true);
|
||||||
xhttp.send(null);
|
xhttp.send(null);
|
||||||
|
|
||||||
function serverError() {
|
function serverError(URL) {
|
||||||
var errormessage = document.getElementById('downloadLinks');
|
var requestedURL, altURL, torrentURL;
|
||||||
errormessage.innerHTML = '<span style="font-weight:bold;font-family:consolas,monospace;">' +
|
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(/<pre\b([^>]*)>[\s\S]*?<\/pre>/i, '<div$1>' + headerDoc + '</div>');
|
||||||
|
var body = document.getElementById('dl-panel-body');
|
||||||
|
var bodyDoc = '<p><b>Directly download ZIM archive:</b></p>' +
|
||||||
|
'<p><a href="' + requestedURL + '" target="_blank">' + requestedURL + '</a></p>' +
|
||||||
|
(altURL ? '<p><b>Possible mirror:</b></p>' +
|
||||||
|
'<p><a href="' + altURL + '" target="_blank">' + altURL + '</a></p>' : '') +
|
||||||
|
'<p><b>Download with bittorrent:</b></p>' +
|
||||||
|
'<p><a href="' + torrentURL + '" target="_blank">' + torrentURL + '</a></p>';
|
||||||
|
body.outerHTML = body.outerHTML.replace(/<pre\b([^>]*)>[\s\S]*?<\/pre>/i, '<pre$1>' + bodyDoc + '</pre>');
|
||||||
|
downloadLinks.innerHTML = downloadLinks.innerHTML.replace(/Index\s+of/ig, "File in");
|
||||||
|
downloadLinks.innerHTML = downloadLinks.innerHTML.replace(/panel-success/i, "panel-warning");
|
||||||
|
} 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 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 />' +
|
'<p>You can either try again or else open this link in a new browser window:<br />' +
|
||||||
'<a href="' + params.kiwixDownloadLink + '" target="_blank">' + params.kiwixDownloadLink + '</a></p><br />';
|
'<a href="' + params.kiwixDownloadLink + '" target="_blank">' + params.kiwixDownloadLink + '</a></p><br /></span>';
|
||||||
errormessage.style.display = "block";
|
}
|
||||||
|
downloadLinks.style.display = "block";
|
||||||
}
|
}
|
||||||
|
|
||||||
function processXhttpData(doc) {
|
function processXhttpData(doc) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user