1 import { readFile } from 'fs-extra'
2 import * as parseTorrent from 'parse-torrent'
3 import { basename, join } from 'path'
4 import * as WebTorrent from 'webtorrent'
5 import { VideoFile } from '@shared/models'
6 import { PeerTubeServer } from '../server'
8 let webtorrent: WebTorrent.Instance
10 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
11 const WebTorrent = require('webtorrent')
13 if (!webtorrent) webtorrent = new WebTorrent()
14 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
16 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
19 async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {
20 const torrentName = basename(file.torrentUrl)
21 const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
23 const data = await readFile(torrentPath)
25 return parseTorrent(data)