]>
Commit | Line | Data |
---|---|---|
d23dd9fb | 1 | import { readFile } from 'fs-extra' |
41fb13c3 | 2 | import parseTorrent from 'parse-torrent' |
764b1a14 | 3 | import { basename, join } from 'path' |
6c5065a0 | 4 | import * as WebTorrent from 'webtorrent' |
764b1a14 | 5 | import { VideoFile } from '@shared/models' |
254d3579 | 6 | import { PeerTubeServer } from '../server' |
6c5065a0 C |
7 | |
8 | let webtorrent: WebTorrent.Instance | |
9 | ||
fbcd44d2 | 10 | function webtorrentAdd (torrentId: string, refreshWebTorrent = false) { |
6c5065a0 C |
11 | const WebTorrent = require('webtorrent') |
12 | ||
17e2705f C |
13 | if (webtorrent && refreshWebTorrent) webtorrent.destroy() |
14 | if (!webtorrent || refreshWebTorrent) webtorrent = new WebTorrent() | |
6c5065a0 | 15 | |
fbcd44d2 C |
16 | webtorrent.on('error', err => console.error('Error in webtorrent', err)) |
17 | ||
18 | return new Promise<WebTorrent.Torrent>(res => { | |
19 | const torrent = webtorrent.add(torrentId, res) | |
20 | ||
21 | torrent.on('error', err => console.error('Error in webtorrent torrent', err)) | |
22 | torrent.on('warning', warn => { | |
23 | const msg = typeof warn === 'string' | |
24 | ? warn | |
25 | : warn.message | |
26 | ||
27 | if (msg.includes('Unsupported')) return | |
28 | ||
29 | console.error('Warning in webtorrent torrent', warn) | |
30 | }) | |
31 | }) | |
6c5065a0 C |
32 | } |
33 | ||
764b1a14 C |
34 | async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) { |
35 | const torrentName = basename(file.torrentUrl) | |
89d241a7 | 36 | const torrentPath = server.servers.buildDirectory(join('torrents', torrentName)) |
d23dd9fb C |
37 | |
38 | const data = await readFile(torrentPath) | |
39 | ||
40 | return parseTorrent(data) | |
41 | } | |
42 | ||
6c5065a0 | 43 | export { |
d23dd9fb C |
44 | webtorrentAdd, |
45 | parseTorrentVideo | |
6c5065a0 | 46 | } |