From ae9bbed46dbc8d9870c9feb66bbada484c1c7582 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Apr 2019 11:13:49 +0200 Subject: Update P2P media loader peer version --- server/lib/activitypub/videos.ts | 22 +++++++++------------- server/lib/hls.ts | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 16 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index d935e3f90..339f8e797 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -290,7 +290,11 @@ async function updateVideoFromAP (options: { } { - const streamingPlaylistAttributes = streamingPlaylistActivityUrlToDBAttributes(options.video, options.videoObject) + const streamingPlaylistAttributes = streamingPlaylistActivityUrlToDBAttributes( + options.video, + options.videoObject, + options.video.VideoFiles + ) const newStreamingPlaylists = streamingPlaylistAttributes.map(a => new VideoStreamingPlaylistModel(a)) // Remove video files that do not exist anymore @@ -449,9 +453,9 @@ async function createVideo (videoObject: VideoTorrentObject, channelActor: Actor } const videoFilePromises = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) - await Promise.all(videoFilePromises) + const videoFiles = await Promise.all(videoFilePromises) - const videoStreamingPlaylists = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject) + const videoStreamingPlaylists = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles) const playlistPromises = videoStreamingPlaylists.map(p => VideoStreamingPlaylistModel.create(p, { transaction: t })) await Promise.all(playlistPromises) @@ -575,20 +579,12 @@ function videoFileActivityUrlToDBAttributes (video: VideoModel, videoObject: Vid return attributes } -function streamingPlaylistActivityUrlToDBAttributes (video: VideoModel, videoObject: VideoTorrentObject) { +function streamingPlaylistActivityUrlToDBAttributes (video: VideoModel, videoObject: VideoTorrentObject, videoFiles: VideoFileModel[]) { const playlistUrls = videoObject.url.filter(u => isAPStreamingPlaylistUrlObject(u)) as ActivityPlaylistUrlObject[] if (playlistUrls.length === 0) return [] const attributes: FilteredModelAttributes[] = [] for (const playlistUrlObject of playlistUrls) { - const p2pMediaLoaderInfohashes = playlistUrlObject.tag - .filter(t => t.type === 'Infohash') - .map(t => t.name) - if (p2pMediaLoaderInfohashes.length === 0) { - logger.warn('No infohashes found in AP playlist object.', { playlistUrl: playlistUrlObject }) - continue - } - const segmentsSha256UrlObject = playlistUrlObject.tag .find(t => { return isAPPlaylistSegmentHashesUrlObject(t) @@ -602,7 +598,7 @@ function streamingPlaylistActivityUrlToDBAttributes (video: VideoModel, videoObj type: VideoStreamingPlaylistType.HLS, playlistUrl: playlistUrlObject.href, segmentsSha256Url: segmentsSha256UrlObject.href, - p2pMediaLoaderInfohashes, + p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, videoFiles), videoId: video.id } diff --git a/server/lib/hls.ts b/server/lib/hls.ts index 74ed25183..5a7d61dee 100644 --- a/server/lib/hls.ts +++ b/server/lib/hls.ts @@ -1,6 +1,6 @@ import { VideoModel } from '../models/video/video' -import { basename, join, dirname } from 'path' -import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY } from '../initializers' +import { basename, dirname, join } from 'path' +import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY, sequelizeTypescript } from '../initializers' import { close, ensureDir, move, open, outputJSON, pathExists, read, readFile, remove, writeFile } from 'fs-extra' import { getVideoFileSize } from '../helpers/ffmpeg-utils' import { sha256 } from '../helpers/core-utils' @@ -9,6 +9,21 @@ import { logger } from '../helpers/logger' import { doRequest, doRequestAndSaveToFile } from '../helpers/requests' import { generateRandomString } from '../helpers/utils' import { flatten, uniq } from 'lodash' +import { VideoFileModel } from '../models/video/video-file' + +async function updateStreamingPlaylistsInfohashesIfNeeded () { + const playlistsToUpdate = await VideoStreamingPlaylistModel.listByIncorrectPeerVersion() + + // Use separate SQL queries, because we could have many videos to update + for (const playlist of playlistsToUpdate) { + await sequelizeTypescript.transaction(async t => { + const videoFiles = await VideoFileModel.listByStreamingPlaylist(playlist.id, t) + + playlist.p2pMediaLoaderInfohashes = await VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlist.playlistUrl, videoFiles) + await playlist.save({ transaction: t }) + }) + } +} async function updateMasterHLSPlaylist (video: VideoModel) { const directory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) @@ -159,7 +174,8 @@ function downloadPlaylistSegments (playlistUrl: string, destinationDir: string, export { updateMasterHLSPlaylist, updateSha256Segments, - downloadPlaylistSegments + downloadPlaylistSegments, + updateStreamingPlaylistsInfohashesIfNeeded } // --------------------------------------------------------------------------- -- cgit v1.2.3