]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/webtorrent.ts
Don't throw when there is no error
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
index 0129184687eb6993e09b20a67fa466661f988504..813ebc2361f77965c129df369acfc8b318e868d1 100644 (file)
@@ -17,6 +17,7 @@ import { promisify2 } from './core-utils'
 import { logger } from './logger'
 import { generateVideoImportTmpPath } from './utils'
 import { extractVideo } from './video'
+import { pipeline } from 'stream'
 
 const createTorrentPromise = promisify2<string, any, any>(createTorrent)
 
@@ -49,6 +50,8 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
           .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
       }
 
+      logger.debug('Got torrent from webtorrent %s.', id, { infoHash: torrent.infoHash })
+
       file = torrent.files[0]
 
       // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed
@@ -61,7 +64,13 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
           .catch(err => logger.error('Cannot destroy webtorrent.', { err }))
       })
 
-      file.createReadStream().pipe(writeStream)
+      pipeline(
+        file.createReadStream(),
+        writeStream,
+        err => {
+          if (err) rej(err)
+        }
+      )
     })
 
     torrent.on('error', err => rej(err))
@@ -127,14 +136,10 @@ async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVi
   const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
   const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
 
-  logger.info('Updating torrent URLs %s.', newTorrentPath)
+  logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath)
 
   await writeFile(newTorrentPath, bencode.encode(decoded))
-
-  // Remove old torrent file if it existed
-  if (videoFile.hasTorrent()) {
-    await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
-  }
+  await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
 
   videoFile.torrentFilename = newTorrentFilename
 }