File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed
Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments