1 import { readFile } from 'fs-extra'
2 import * as parseTorrent from 'parse-torrent'
3 import { join } from 'path'
4 import * as WebTorrent from 'webtorrent'
5 import { ServerInfo } from '../server'
7 let webtorrent: WebTorrent.Instance
9 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
10 const WebTorrent = require('webtorrent')
12 if (!webtorrent) webtorrent = new WebTorrent()
13 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
15 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
18 async function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
19 const torrentName = videoUUID + '-' + resolution + '.torrent'
20 const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
22 const data = await readFile(torrentPath)
24 return parseTorrent(data)