diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-21 15:51:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-21 15:51:30 +0200 |
commit | a24bd1ed41b43790bab6ba789580bb4e85f07d85 (patch) | |
tree | a54b0f6c921ba83a6e909cd0ced325b2d4b8863c /shared/extra-utils/miscs/webtorrent.ts | |
parent | 5f26f13b3c16ac5ae0a3b0a7142d84a9528cf565 (diff) | |
parent | c63830f15403ac4e750829f27d8bbbdc9a59282c (diff) | |
download | PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.gz PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.zst PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.zip |
Merge branch 'next' into develop
Diffstat (limited to 'shared/extra-utils/miscs/webtorrent.ts')
-rw-r--r-- | shared/extra-utils/miscs/webtorrent.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/shared/extra-utils/miscs/webtorrent.ts b/shared/extra-utils/miscs/webtorrent.ts new file mode 100644 index 000000000..815ea3d56 --- /dev/null +++ b/shared/extra-utils/miscs/webtorrent.ts | |||
@@ -0,0 +1,30 @@ | |||
1 | import { readFile } from 'fs-extra' | ||
2 | import * as parseTorrent from 'parse-torrent' | ||
3 | import { join } from 'path' | ||
4 | import * as WebTorrent from 'webtorrent' | ||
5 | import { PeerTubeServer } from '../server' | ||
6 | |||
7 | let webtorrent: WebTorrent.Instance | ||
8 | |||
9 | function 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 | |||
18 | async function parseTorrentVideo (server: PeerTubeServer, videoUUID: string, resolution: number) { | ||
19 | const torrentName = videoUUID + '-' + resolution + '.torrent' | ||
20 | const torrentPath = server.servers.buildDirectory(join('torrents', torrentName)) | ||
21 | |||
22 | const data = await readFile(torrentPath) | ||
23 | |||
24 | return parseTorrent(data) | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | webtorrentAdd, | ||
29 | parseTorrentVideo | ||
30 | } | ||