X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmodels%2Fvideo%2Fvideo-streaming-playlist.ts;h=a85c79c9f1d96c64c2542926aa964954bcfba0b8;hb=8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa;hp=3e9fd97c72ed401a96280634b6f7bde813baed37;hpb=0305db28c98fd6cf43a3c50ba92c76215e99d512;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-streaming-playlist.ts b/server/models/video/video-streaming-playlist.ts index 3e9fd97c7..a85c79c9f 100644 --- a/server/models/video/video-streaming-playlist.ts +++ b/server/models/video/video-streaming-playlist.ts @@ -1,6 +1,6 @@ -import * as memoizee from 'memoizee' +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/core-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 '../../helpers/core-utils' 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,45 @@ 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) + + 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) { + const query = `SELECT 1 FROM "videoStreamingPlaylist" ` + + `INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` + + `AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` + + `AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1` - return Object.assign(playlist, { videoId: video.id, Video: video }) + return doesExist(this.sequelize, query, { videoUUID }) } assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) { @@ -204,26 +245,52 @@ export class VideoStreamingPlaylistModel extends Model