]>
Commit | Line | Data |
---|---|---|
1 | import { readFile } from 'fs-extra' | |
2 | import * as parseTorrent from 'parse-torrent' | |
3 | import { basename, join } from 'path' | |
4 | import * as WebTorrent from 'webtorrent' | |
5 | import { VideoFile } from '@shared/models' | |
6 | import { PeerTubeServer } from '../server' | |
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 | ||
19 | async 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 | ||
28 | export { | |
29 | webtorrentAdd, | |
30 | parseTorrentVideo | |
31 | } |