From a415c6efe1370486d379570107170f8aa40fd671 Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Fri, 6 Oct 2023 09:06:11 +0000 Subject: [PATCH] introducing makeURLSearchString --- static/skin/index.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/static/skin/index.js b/static/skin/index.js index 798de7855..c8c346003 100644 --- a/static/skin/index.js +++ b/static/skin/index.js @@ -197,6 +197,15 @@ } } + function makeURLSearchString(params, keysToURIEncode) { + let output = ''; + for (const [key, value] of params.entries()) { + let finalValue = (keysToURIEncode.indexOf(key) >= 0) ? encodeURIComponent(value) : value; + output += `&${key}=${finalValue}`; + } + return output; + } + /* hack for library.kiwix.org magnet links (created by MirrorBrain) See https://github.com/kiwix/container-images/issues/242 */ async function getFixedMirrorbrainMagnet(magnetLink) { @@ -227,29 +236,17 @@ } // set webseed (ws=) URL to primary download URL (redirects to mirror) - params.set('ws', encodeURIComponent(zimUrl)); + params.set('ws', zimUrl); // if we got metalink mirror URLs, append them all if (mirrorUrls) { mirrorUrls.forEach((url) => { - params.append('ws', encodeURIComponent(url)); + params.append('ws', url); }); } - params.set('as', encodeURIComponent(zimUrl)); - params.set('dn', encodeURIComponent(params.get('dn'))); - params.set('xs', encodeURIComponent(`${zimUrl}.torrent`)); // adding xs= to point to torrent URL - const trackers = params.getAll('tr'); - params.delete('tr'); - trackers.forEach((tracker) => { - params.append('tr', encodeURIComponent(tracker)); - }); + params.set('xs', `${zimUrl}.torrent`); // adding xs= to point to torrent URL - magnetLink = 'magnet:?'; - // we don't want params.ToString()'s urlencoding - for (const [key, value] of params.entries()) { - magnetLink += `&${key}=${value}`; - } - return magnetLink; + return 'magnet:?' + makeURLSearchString(params, ['ws', 'as', 'dn', 'xs', 'tr']); } async function getMagnetLink(downloadLink) {