]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/miscs/webtorrent.ts
Custom markup container default to space between
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / webtorrent.ts
CommitLineData
d23dd9fb
C
1import { readFile } from 'fs-extra'
2import * as parseTorrent from 'parse-torrent'
764b1a14 3import { basename, join } from 'path'
6c5065a0 4import * as WebTorrent from 'webtorrent'
764b1a14 5import { VideoFile } from '@shared/models'
254d3579 6import { PeerTubeServer } from '../server'
6c5065a0
C
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
764b1a14
C
19async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {
20 const torrentName = basename(file.torrentUrl)
89d241a7 21 const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
d23dd9fb
C
22
23 const data = await readFile(torrentPath)
24
25 return parseTorrent(data)
26}
27
6c5065a0 28export {
d23dd9fb
C
29 webtorrentAdd,
30 parseTorrentVideo
6c5065a0 31}