X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fwebtorrent.ts;h=6d87c74f7ec8b9a31dc8a5fcce653d017634acbf;hb=3545e72c686ff1725bbdfd8d16d693e2f4aa75a3;hp=b350c9718784f12c20cb2b282e118c2382d392b0;hpb=9b293cd6a2ce9ed1e1ccd41adbf7f2dbe2da8231;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index b350c9718..6d87c74f7 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts @@ -13,6 +13,7 @@ import { VideoPathManager } from '@server/lib/video-path-manager' import { MVideo } from '@server/types/models/video/video' import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' +import { sha1 } from '@shared/extra-utils' import { CONFIG } from '../initializers/config' import { promisify2 } from './core-utils' import { logger } from './logger' @@ -90,6 +91,16 @@ async function downloadWebTorrentVideo (target: { uri: string, torrentName?: str } function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { + return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(videoOrPlaylist), videoPath => { + return createTorrentAndSetInfoHashFromPath(videoOrPlaylist, videoFile, videoPath) + }) +} + +async function createTorrentAndSetInfoHashFromPath ( + videoOrPlaylist: MVideo | MStreamingPlaylistVideo, + videoFile: MVideoFile, + filePath: string +) { const video = extractVideo(videoOrPlaylist) const options = { @@ -100,24 +111,22 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli urlList: buildUrlList(video, videoFile) } - return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(videoOrPlaylist), async videoPath => { - const torrentContent = await createTorrentPromise(videoPath, options) + const torrentContent = await createTorrentPromise(filePath, options) - const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) - const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename) - logger.info('Creating torrent %s.', torrentPath) + const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) + const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename) + logger.info('Creating torrent %s.', torrentPath) - await writeFile(torrentPath, torrentContent) + await writeFile(torrentPath, torrentContent) - // Remove old torrent file if it existed - if (videoFile.hasTorrent()) { - await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) - } + // Remove old torrent file if it existed + if (videoFile.hasTorrent()) { + await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) + } - const parsedTorrent = parseTorrent(torrentContent) - videoFile.infoHash = parsedTorrent.infoHash - videoFile.torrentFilename = torrentFilename - }) + const parsedTorrent = parseTorrent(torrentContent) + videoFile.infoHash = parsedTorrent.infoHash + videoFile.torrentFilename = torrentFilename } async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { @@ -145,6 +154,7 @@ async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlayli await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) videoFile.torrentFilename = newTorrentFilename + videoFile.infoHash = sha1(encode(decoded.info)) } function generateMagnetUri ( @@ -154,7 +164,10 @@ function generateMagnetUri ( ) { const xs = videoFile.getTorrentUrl() const announce = trackerUrls - let urlList = [ videoFile.getFileUrl(video) ] + + let urlList = video.requiresAuth(video.uuid) + ? [] + : [ videoFile.getFileUrl(video) ] const redundancies = videoFile.RedundancyVideos if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) @@ -175,7 +188,10 @@ function generateMagnetUri ( export { createTorrentPromise, updateTorrentMetadata, + createTorrentAndSetInfoHash, + createTorrentAndSetInfoHashFromPath, + generateMagnetUri, downloadWebTorrentVideo } @@ -227,6 +243,8 @@ function buildAnnounceList () { } function buildUrlList (video: MVideo, videoFile: MVideoFile) { + if (video.requiresAuth(video.uuid)) return [] + return [ videoFile.getFileUrl(video) ] }