X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fwebtorrent.ts;h=1c0cc7058a4389ed31c3749f24310b5a422f963a;hb=a651038487faa838bda3ce04695b08bc65baff70;hp=121cd0b41747903f8c1f9b27a46cbc4800a46318;hpb=3e17515e2996b79e23f569c296051a91af3fcbe4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 121cd0b41..1c0cc7058 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts @@ -1,7 +1,7 @@ import { logger } from './logger' import { generateVideoTmpPath } from './utils' import * as WebTorrent from 'webtorrent' -import { createWriteStream } from 'fs' +import { createWriteStream, remove } from 'fs-extra' import { CONFIG } from '../initializers' import { join } from 'path' @@ -15,13 +15,29 @@ function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: stri const webtorrent = new WebTorrent() const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName) - const torrent = webtorrent.add(torrentId, torrent => { + + const options = { path: CONFIG.STORAGE.VIDEOS_DIR } + const torrent = webtorrent.add(torrentId, options, torrent => { if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId)) const file = torrent.files[ 0 ] const writeStream = createWriteStream(path) - writeStream.on('finish', () => res(path)) + writeStream.on('finish', () => { + webtorrent.destroy(async err => { + if (err) return rej(err) + + if (target.torrentName) { + remove(torrentId) + .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err })) + } + + remove(join(CONFIG.STORAGE.VIDEOS_DIR, file.name)) + .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', file.name, { err })) + + res(path) + }) + }) file.createReadStream().pipe(writeStream) })