1
0
Fork 0
peertube/shared/extra-utils/miscs/webtorrent.ts

32 lines
926 B
TypeScript
Raw Normal View History

2021-07-15 08:02:54 +00:00
import { readFile } from 'fs-extra'
import * as parseTorrent from 'parse-torrent'
2021-07-23 09:20:00 +00:00
import { basename, join } from 'path'
2021-07-13 07:43:59 +00:00
import * as WebTorrent from 'webtorrent'
2021-07-23 09:20:00 +00:00
import { VideoFile } from '@shared/models'
2021-07-16 07:47:51 +00:00
import { PeerTubeServer } from '../server'
2021-07-13 07:43:59 +00: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-23 09:20:00 +00:00
async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {
const torrentName = basename(file.torrentUrl)
2021-07-16 07:04:35 +00:00
const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
2021-07-15 08:02:54 +00:00
const data = await readFile(torrentPath)
return parseTorrent(data)
}
2021-07-13 07:43:59 +00:00
export {
2021-07-15 08:02:54 +00:00
webtorrentAdd,
parseTorrentVideo
2021-07-13 07:43:59 +00:00
}