]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - shared/extra-utils/miscs/webtorrent.ts
Custom markup container default to space between
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / webtorrent.ts
... / ...
CommitLineData
1import { readFile } from 'fs-extra'
2import * as parseTorrent from 'parse-torrent'
3import { basename, join } from 'path'
4import * as WebTorrent from 'webtorrent'
5import { VideoFile } from '@shared/models'
6import { PeerTubeServer } from '../server'
7
8let webtorrent: WebTorrent.Instance
9
10function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
11 const WebTorrent = require('webtorrent')
12
13 if (!webtorrent) webtorrent = new WebTorrent()
14 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
15
16 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
17}
18
19async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {
20 const torrentName = basename(file.torrentUrl)
21 const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
22
23 const data = await readFile(torrentPath)
24
25 return parseTorrent(data)
26}
27
28export {
29 webtorrentAdd,
30 parseTorrentVideo
31}