+ const writeStream = createWriteStream(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)
+ })