1 import { logger } from './logger'
2 import * as WebTorrent from 'webtorrent'
3 import { remove } from 'fs-extra'
4 import { CONFIG } from '../initializers'
5 import { join } from 'path'
7 function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
8 const id = target.magnetUri || target.torrentName
11 logger.info('Importing torrent video %s', id)
13 return new Promise<string>((res, rej) => {
14 const webtorrent = new WebTorrent()
15 let file: WebTorrent.TorrentFile
17 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
19 const options = { path: CONFIG.STORAGE.VIDEOS_DIR }
20 const torrent = webtorrent.add(torrentId, options, torrent => {
21 if (torrent.files.length !== 1) {
22 if (timer) clearTimeout(timer)
24 return safeWebtorrentDestroy(webtorrent, torrentId, file.name, target.torrentName)
25 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
28 torrent.on('done', () => {
29 // FIXME: Dirty fix, we need to wait the FS sync but webtorrent does not provide such method
30 setTimeout(() => res(join(CONFIG.STORAGE.VIDEOS_DIR, torrent.files[ 0 ].name)), 1000)
34 torrent.on('error', err => rej(err))
37 timer = setTimeout(async () => {
38 return safeWebtorrentDestroy(webtorrent, torrentId, file ? file.name : undefined, target.torrentName)
39 .then(() => rej(new Error('Webtorrent download timeout.')))
45 // ---------------------------------------------------------------------------
48 downloadWebTorrentVideo
51 // ---------------------------------------------------------------------------
53 function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filename?: string, torrentName?: string) {
54 return new Promise(res => {
55 webtorrent.destroy(err => {
56 // Delete torrent file
59 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
62 // Delete downloaded file
64 remove(join(CONFIG.STORAGE.VIDEOS_DIR, filename))
65 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filename, { err }))
69 logger.warn('Cannot destroy webtorrent in timeout.', { err })