X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fwebtorrent.ts;h=813ebc2361f77965c129df369acfc8b318e868d1;hb=e111a5a3a11a5b251b8989cd406c2dbba9254e28;hp=0129184687eb6993e09b20a67fa466661f988504;hpb=1f6125be8b6306ba34b5ad9df985df462ef9759c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 012918468..813ebc236 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts @@ -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(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 }