mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-03 11:22:25 +00:00
Fix duplicate magnet validator definition
This commit is contained in:
1 parent
cf34e28a47
commit
21b095e45f
1 file changed
-54
@@ -87,60 +87,6 @@ const BITVID_WEBSITE_URL = "https://bitvid.network/";
|
||||
const MAX_DISCUSSION_COUNT_VIDEOS = 24;
|
||||
const VIDEO_EVENT_KIND = 30078;
|
||||
const HEX64_REGEX = /^[0-9a-f]{64}$/i;
|
||||
/**
|
||||
* Basic validation for BitTorrent magnet URIs.
|
||||
*
|
||||
* Returns `true` only when the value looks like a magnet link that WebTorrent
|
||||
* understands (`magnet:` scheme with at least one `xt=urn:btih:<info-hash>`
|
||||
* entry, where `<info-hash>` is either a 40-character hex digest or a
|
||||
* 32-character base32 digest). Magnets that only contain BitTorrent v2 hashes
|
||||
* (e.g. `btmh`) are treated as unsupported.
|
||||
*/
|
||||
function isValidMagnetUri(magnet) {
|
||||
const trimmed = typeof magnet === "string" ? magnet.trim() : "";
|
||||
if (!trimmed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const decoded = safeDecodeMagnet(trimmed);
|
||||
const candidate = decoded || trimmed;
|
||||
|
||||
if (/^[0-9a-f]{40}$/i.test(candidate)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = new URL(candidate);
|
||||
if (parsed.protocol.toLowerCase() !== "magnet:") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const xtValues = parsed.searchParams.getAll("xt");
|
||||
if (!xtValues.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hexPattern = /^[0-9a-f]{40}$/i;
|
||||
const base32Pattern = /^[A-Z2-7]{32}$/;
|
||||
|
||||
return xtValues.some((value) => {
|
||||
if (typeof value !== "string") return false;
|
||||
const match = value.trim().match(/^urn:btih:([a-z0-9]+)$/i);
|
||||
if (!match) return false;
|
||||
|
||||
const infoHash = match[1];
|
||||
if (hexPattern.test(infoHash)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const upperHash = infoHash.toUpperCase();
|
||||
return infoHash.length === 32 && base32Pattern.test(upperHash);
|
||||
});
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple IntersectionObserver-based lazy loader for images (or videos).
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user