X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-streaming-playlist.ts;h=a85c79c9f1d96c64c2542926aa964954bcfba0b8;hb=2f63f629add5d24f8c01f309c7cae43b667b0c2a;hp=7b410f8eea4ac00b26d18935ffead63d11b25ac5;hpb=c55e3d7227fe1453869e309025996b9d75256d5d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index 7b410f8ee..a85c79c9f 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -1,6 +1,6 @@ import memoizee from 'memoizee' import { join } from 'path' -import { Op } from 'sequelize' +import { Op, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -15,13 +15,16 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { getHLSPublicFileUrl } from '@server/lib/object-storage' +import { CONFIG } from '@server/initializers/config' +import { getHLSPrivateFileUrl, getHLSPublicFileUrl } from '@server/lib/object-storage' +import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '@server/lib/paths' +import { isVideoInPrivateDirectory } from '@server/lib/video-privacy' import { VideoFileModel } from '@server/models/video/video-file' -import { MStreamingPlaylist, MVideo } from '@server/types/models' -import { AttributesOnly } from '@shared/typescript-utils' +import { MStreamingPlaylist, MStreamingPlaylistFilesVideo, MVideo } from '@server/types/models' +import { sha1 } from '@shared/extra-utils' import { VideoStorage } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' -import { sha1 } from '@shared/core-utils/common/crypto' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { isArrayOf } from '../../helpers/custom-validators/misc' import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' @@ -34,8 +37,7 @@ import { WEBSERVER } from '../../initializers/constants' import { VideoRedundancyModel } from '../redundancy/video-redundancy' -import { doesExist } from '../shared' -import { throwIfNotValid } from '../utils' +import { doesExist, throwIfNotValid } from '../shared' import { VideoModel } from './video' @Table({ @@ -135,7 +137,7 @@ export class VideoStreamingPlaylistModel extends Model(id, options) + } + static loadWithVideo (id: number) { const options = { include: [ @@ -180,22 +198,36 @@ export class VideoStreamingPlaylistModel extends Model { + static loadHLSPlaylistByVideo (videoId: number, transaction?: Transaction): Promise { const options = { where: { type: VideoStreamingPlaylistType.HLS, videoId - } + }, + transaction } return VideoStreamingPlaylistModel.findOne(options) } - static async loadOrGenerate (video: MVideo) { - let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) - if (!playlist) playlist = new VideoStreamingPlaylistModel() + static async loadOrGenerate (video: MVideo, transaction?: Transaction) { + let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id, transaction) - return Object.assign(playlist, { videoId: video.id, Video: video }) + if (!playlist) { + playlist = new VideoStreamingPlaylistModel({ + p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, + type: VideoStreamingPlaylistType.HLS, + storage: VideoStorage.FILE_SYSTEM, + p2pMediaLoaderInfohashes: [], + playlistFilename: generateHLSMasterPlaylistFilename(video.isLive), + segmentsSha256Filename: generateHlsSha256SegmentsFilename(video.isLive), + videoId: video.id + }) + + await playlist.save({ transaction }) + } + + return Object.assign(playlist, { Video: video }) } static doesOwnedHLSPlaylistExist (videoUUID: string) { @@ -204,7 +236,7 @@ export class VideoStreamingPlaylistModel extends Model