X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fextra-utils%2Fmiscs%2Fwebtorrent.ts;h=b610fdda8a3bb80e8ef667c5401c86314d6ac4b9;hb=62549e6c9818f422698f030e0b242609115493ed;hp=63e648309e6275aed88029cfbab810116b2e992e;hpb=d23dd9fbfc4d26026352c10f81d2795ceaf2908a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/extra-utils/miscs/webtorrent.ts b/shared/extra-utils/miscs/webtorrent.ts index 63e648309..b610fdda8 100644 --- a/shared/extra-utils/miscs/webtorrent.ts +++ b/shared/extra-utils/miscs/webtorrent.ts @@ -1,23 +1,39 @@ import { readFile } from 'fs-extra' -import * as parseTorrent from 'parse-torrent' -import { join } from 'path' +import parseTorrent from 'parse-torrent' +import { basename, join } from 'path' import * as WebTorrent from 'webtorrent' -import { ServerInfo } from '../server' +import { VideoFile } from '@shared/models' +import { PeerTubeServer } from '../server' let webtorrent: WebTorrent.Instance -function webtorrentAdd (torrent: string, refreshWebTorrent = false) { +function webtorrentAdd (torrentId: string, refreshWebTorrent = false) { const WebTorrent = require('webtorrent') if (!webtorrent) webtorrent = new WebTorrent() if (refreshWebTorrent === true) webtorrent = new WebTorrent() - return new Promise(res => webtorrent.add(torrent, res)) + webtorrent.on('error', err => console.error('Error in webtorrent', err)) + + return new Promise(res => { + const torrent = webtorrent.add(torrentId, res) + + torrent.on('error', err => console.error('Error in webtorrent torrent', err)) + torrent.on('warning', warn => { + const msg = typeof warn === 'string' + ? warn + : warn.message + + if (msg.includes('Unsupported')) return + + console.error('Warning in webtorrent torrent', warn) + }) + }) } -async function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) { - const torrentName = videoUUID + '-' + resolution + '.torrent' - const torrentPath = server.serversCommand.buildDirectory(join('torrents', torrentName)) +async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) { + const torrentName = basename(file.torrentUrl) + const torrentPath = server.servers.buildDirectory(join('torrents', torrentName)) const data = await readFile(torrentPath)