]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/miscs/webtorrent.ts
Rewrite youtube-dl import
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / webtorrent.ts
index a1097effe7396dea1ad12dfa5b1aaeaec962b23f..b610fdda8a3bb80e8ef667c5401c86314d6ac4b9 100644 (file)
@@ -1,5 +1,5 @@
 import { readFile } from 'fs-extra'
-import * as parseTorrent from 'parse-torrent'
+import parseTorrent from 'parse-torrent'
 import { basename, join } from 'path'
 import * as WebTorrent from 'webtorrent'
 import { VideoFile } from '@shared/models'
@@ -7,13 +7,28 @@ import { PeerTubeServer } from '../server'
 
 let webtorrent: WebTorrent.Instance
 
-function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
+function webtorrentAdd (torrentId: string, refreshWebTorrent = false) {
   const WebTorrent = require('webtorrent')
 
   if (!webtorrent) webtorrent = new WebTorrent()
   if (refreshWebTorrent === true) webtorrent = new WebTorrent()
 
-  return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
+  webtorrent.on('error', err => console.error('Error in webtorrent', err))
+
+  return new Promise<WebTorrent.Torrent>(res => {
+    const torrent = webtorrent.add(torrentId, res)
+
+    torrent.on('error', err => console.error('Error in webtorrent torrent', err))
+    torrent.on('warning', warn => {
+      const msg = typeof warn === 'string'
+        ? warn
+        : warn.message
+
+      if (msg.includes('Unsupported')) return
+
+      console.error('Warning in webtorrent torrent', warn)
+    })
+  })
 }
 
 async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {