]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/miscs/webtorrent.ts
Introduce videos command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / webtorrent.ts
CommitLineData
d23dd9fb
C
1import { readFile } from 'fs-extra'
2import * as parseTorrent from 'parse-torrent'
3import { join } from 'path'
6c5065a0 4import * as WebTorrent from 'webtorrent'
d23dd9fb 5import { ServerInfo } from '../server'
6c5065a0
C
6
7let webtorrent: WebTorrent.Instance
8
9function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
10 const WebTorrent = require('webtorrent')
11
12 if (!webtorrent) webtorrent = new WebTorrent()
13 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
14
15 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
16}
17
d23dd9fb
C
18async function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
19 const torrentName = videoUUID + '-' + resolution + '.torrent'
20 const torrentPath = server.serversCommand.buildDirectory(join('torrents', torrentName))
21
22 const data = await readFile(torrentPath)
23
24 return parseTorrent(data)
25}
26
6c5065a0 27export {
d23dd9fb
C
28 webtorrentAdd,
29 parseTorrentVideo
6c5065a0 30}