1 import { logger } from './logger'
2 import { generateVideoTmpPath } from './utils'
3 import * as WebTorrent from 'webtorrent'
4 import { createWriteStream } from 'fs'
6 function downloadWebTorrentVideo (target: string) {
7 const path = generateVideoTmpPath(target)
9 logger.info('Importing torrent video %s', target)
11 return new Promise<string>((res, rej) => {
12 const webtorrent = new WebTorrent()
14 const torrent = webtorrent.add(target, torrent => {
15 if (torrent.files.length !== 1) throw new Error('The number of files is not equal to 1 for ' + target)
17 const file = torrent.files[ 0 ]
18 file.createReadStream().pipe(createWriteStream(path))
21 torrent.on('done', () => res(path))
23 torrent.on('error', err => rej(err))
27 // ---------------------------------------------------------------------------
30 downloadWebTorrentVideo