]>
Commit | Line | Data |
---|---|---|
d23dd9fb C |
1 | import { readFile } from 'fs-extra' |
2 | import * as 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 | ||
10 | function 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 |
19 | async 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 | 28 | export { |
d23dd9fb C |
29 | webtorrentAdd, |
30 | parseTorrentVideo | |
6c5065a0 | 31 | } |