diff --git a/www/index.html b/www/index.html index 763e3006..46974da8 100644 --- a/www/index.html +++ b/www/index.html @@ -734,9 +734,6 @@

Download library

-

- Get more ZIM archives here. Archives can be very large, do not attempt to download using mobile data! -

diff --git a/www/js/lib/kiwixServe.js b/www/js/lib/kiwixServe.js index 1f4194e2..3c83a0e7 100644 --- a/www/js/lib/kiwixServe.js +++ b/www/js/lib/kiwixServe.js @@ -366,7 +366,7 @@ define([], function () { } var xhttp = new XMLHttpRequest(); //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 timeout = /\.magnet$/i.test(URL) ? 3000 : /\.meta4$/i.test(URL) ? 6000 : 20000; var xhttpTimeout = setTimeout(ajaxTimeout, timeout); function ajaxTimeout() { xhttp.abort(); @@ -374,8 +374,9 @@ define([], function () { 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!"; + if (/\.magnet$/i.test(URL)) responseMessageBox.innerHTML = "Unable to get magnet link!"; document.getElementById('serverResponse').style.display = "inline"; - serverError(URL); + if (!/\.magnet$/i.test(URL)) serverError(URL); return; } xhttp.onreadystatechange = function () { @@ -390,6 +391,8 @@ define([], function () { URL = URL.replace(/\?.*/, ''); if (/\.meta4$/i.test(URL)) { processMetaLink(this.responseText); + } else if (/\.magnet$/i.test(URL)) { + processMagnetLink(this.responseText); } else { processXhttpData(this.responseText); } @@ -447,16 +450,8 @@ define([], function () { size = size.toString().split('').reverse().join('').replace(/(\d{3}(?!.*\.|$))/g, '$1,').split('').reverse().join(''); var megabytes$ = megabytes.toString().split('').reverse().join('').replace(/(\d{3}(?!.*\.|$))/g, '$1,').split('').reverse().join(''); doc = ""; - //var mirrorservice = false; for (var i = 1; i < linkArray.length; i++) { //NB we'ere intentionally discarding first link to kiwix.org (not to zim) - //DEV: Mirrorservice download bug now fixed [kiwix-js-windows #28] @TODO: remove this after period of stable downloads fully tested - //ZIP files work fine with mirrorservice, so test for ZIM type only - //if (/\.zim\.meta4$/i.test(URL) && /mirrorservice\.org/i.test(linkArray[i])) { - // mirrorservice = true; - // doc += linkArray[i].replace(/]*>([^<]*)<\/url>/i, '
  • *** Server has download bug, see note ***
    $1
  • \r\n'); - //} else { - doc += linkArray[i].replace(/]*>([^<]*)<\/url>/i, '
  • $1
  • \r\n'); - //} + doc += linkArray[i].replace(/]*>([^<]*)<\/url>/i, '
  • $1
  • \r\n'); } var headerDoc = 'We found the following links to your file:'; var bodyDoc = '

    << Back to list of files

    \r\n'; @@ -465,11 +460,14 @@ define([], function () { bodyDoc += " 2000 ? ' style="color:red;"> WARNING: ' : '>'; bodyDoc += 'File size is ' + (megabytes ? megabytes$ + 'MB' : 'unknown') + '' + (size ? ' (' + size + ' bytes)' : '') + '\r\n'; - if (megabytes > 200) bodyDoc += '

    Consider using BitTorrent to download file:

    \r\n' + - '

    BitTorrent link: ' + - URL.replace(/\.meta4$/, ".torrent") + '

    '; + bodyDoc += '

    New! Preview this archive in your browser before downloading it

    '; + if (megabytes > 200) bodyDoc += '

    Consider using BitTorrent to download file:

    \r\n\r\n'; if (megabytes > 4000 && /\.zim\.meta4$/i.test(URL)) { - bodyDoc += '

    This archive is larger than the maximum file size permitted on an SD card formatted as FAT32 (max size is approx. 4GB). On Windows, this is not normally a problem. However, if your card or other storage area is formatted in this way, you will need to download the file on a PC and split it into chunks less than 4GB: see Download a ZIM archive.

    \r\n'; + bodyDoc += '

    If you plan to store this archive on a drive/microSD formatted as FAT32 (most are not), then you will need to download the file on a PC and split it into chunks less than 4GB: see Download a ZIM archive.

    \r\n'; // bodyDoc += '

    To browse for a split version of this archive click here: ' + URL.replace(/\/zim\/([^/]+\/).*$/m, "/portable/$1") + // '.

    \r\n'; @@ -480,9 +478,10 @@ define([], function () { 'File Explorer. You will need to extract the contents of the folder > data > content,\r\n' + 'and transfer ALL of the files there to an accessible folder on your device. After that, you can search for the folder in this app (see above).

    \r\n'; } - bodyDoc += '

    Links will open in a new browser window

      \r\n' + doc + '
    \r\n'; - //if (mirrorservice) bodyDoc += '*** Note: mirrorservice.org currently has a download bug with ZIM archives: on some browsers it will download the ZIM file as plain text in browser window'; + bodyDoc += '

    Direct download: (links will open in a new browser window)

      \r\n' + doc + '
    \r\n'; bodyDoc += '

    '; + // Try to get magnet link + if (megabytes > 200) requestXhttpData(URL.replace(/\.meta4$/, ".magnet")); var header = document.getElementById('dl-panel-heading'); header.outerHTML = header.outerHTML.replace(/]*)>[\s\S]*?<\/pre>/i, '' + headerDoc + ''); var body = document.getElementById('dl-panel-body'); @@ -499,12 +498,32 @@ define([], function () { var dateID = dateSel ? dateSel.value === 'All' ? '' : dateSel.value : ''; requestXhttpData(this.dataset.kiwixDl, langID, subjID, dateID); }; - //Add event listener for click on return link, to go back to list of archives + // Add event listener for click on return link, to go back to list of archives document.getElementById('returnLink').addEventListener('click', submitSelectValues); - //Add event listener for split archive link, if necessary - if (megabytes > 4000 && /\.zim\.meta4$/i.test(URL)) { - document.getElementById('portable').addEventListener('click', submitSelectValues); - } + // Set up preview link + document.getElementById('preview').href = URL.replace(/^([^/]+\/\/[^/]+\/)(.+\/)([^/]+)\.zim.+$/i, function (m0, domain, path, file) { + domain = domain.replace(/download/, 'library'); + return domain + file; + }); + // Add event listener for split archive link, if necessary + // if (megabytes > 4000 && /\.zim\.meta4$/i.test(URL)) { + // document.getElementById('portable').addEventListener('click', submitSelectValues); + // } + } + + function processMagnetLink(link) { + var magnetLink = document.getElementById('magnet'); + // Set up backup link + var magnetLinkAlt = document.getElementById('magnetAlt'); + magnetLinkAlt.href = magnetLink.href; + // Now point main link to the magnet URL so torrent app will open if installed + magnetLink.href = link; + magnetLink.innerHTML = 'tap to launch link'; + magnetLink.removeAttribute('target'); + magnetLink.addEventListener('click', function (e) { + e.preventDefault(); + window.location = this.href; + }); } function processXhttpData(doc) {