PeerTube/shared/extra-utils/miscs/webtorrent.ts

31 lines
911 B
TypeScript
Raw Normal View History

2021-07-15 10:02:54 +02:00
import { readFile } from 'fs-extra'
import * as parseTorrent from 'parse-torrent'
import { join } from 'path'
2021-07-13 09:43:59 +02:00
import * as WebTorrent from 'webtorrent'
2021-07-16 09:47:51 +02:00
import { PeerTubeServer } from '../server'
2021-07-13 09:43:59 +02:00
let webtorrent: WebTorrent.Instance
function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
const WebTorrent = require('webtorrent')
if (!webtorrent) webtorrent = new WebTorrent()
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
}
2021-07-16 09:47:51 +02:00
async function parseTorrentVideo (server: PeerTubeServer, videoUUID: string, resolution: number) {
2021-07-15 10:02:54 +02:00
const torrentName = videoUUID + '-' + resolution + '.torrent'
2021-07-16 09:04:35 +02:00
const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
2021-07-15 10:02:54 +02:00
const data = await readFile(torrentPath)
return parseTorrent(data)
}
2021-07-13 09:43:59 +02:00
export {
2021-07-15 10:02:54 +02:00
webtorrentAdd,
parseTorrentVideo
2021-07-13 09:43:59 +02:00
}