Skip to content

Commit 2d22533

Browse files
authored
Merge pull request #412 from bogenpirat/410-specific-site-links-fail-with-received-application-x-bittorrent-content-instead-of-a-torrent-file
410 specific site links fail with received application x bittorrent content instead of a torrent file
2 parents 062a5ca + da83d7d commit 2d22533

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/util/download.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export async function downloadTorrent(url: string): Promise<Torrent> {
2121

2222
const torrentBlob: Blob = await response.blob();
2323
const torrentData: string = await convertBlobToString(torrentBlob);
24+
let decodedTorrentData: any;
2425
try {
25-
validateTorrentData(response, torrentData);
26+
decodedTorrentData = decodeTorrentDataAndValidate(response, torrentData);
2627
} catch (error) {
2728
reject(error);
2829
return;
29-
}
30-
const decodedTorrentData = bencode.decode(Buffer.from(torrentData, 'ascii'));
30+
};
3131

3232
resolve({
3333
data: torrentBlob,
@@ -41,15 +41,20 @@ export async function downloadTorrent(url: string): Promise<Torrent> {
4141
});
4242
}
4343

44-
function validateTorrentData(response: Response, data: string): void {
45-
if (!data || data.length < 10 || !data.startsWith("d8:announce")) {
44+
function decodeTorrentDataAndValidate(response: Response, torrentData: string): any {
45+
try {
46+
return bencode.decode(Buffer.from(torrentData, 'ascii'));
47+
} catch (error) {
4648
let contentType = response.headers.get("Content-Type");
4749
if (contentType) {
4850
const semicolonPos = contentType.indexOf(";");
4951
contentType = contentType.slice(0, semicolonPos).trim();
5052
} else {
5153
contentType = "unknown"
5254
}
53-
throw new Error("Received " + contentType + " content instead of a .torrent file");
55+
56+
console.error("Invalid torrent data received", torrentData);
57+
58+
throw new Error("Received " + contentType + " instead of a torrent file. Please check the devtools view for details.");
5459
}
5560
}

0 commit comments

Comments
 (0)