Skip to content

Commit

Permalink
introducing makeURLSearchString
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Oct 6, 2023
1 parent 3f99ff7 commit a415c6e
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions static/skin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a415c6e

Please sign in to comment.