1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
3 import { throwIfNotValid } from '../utils'
4 import { VideoModel } from './video'
5 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
6 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
7 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
8 import { CONSTRAINTS_FIELDS, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_PATHS } from '../../initializers/constants'
9 import { join } from 'path'
10 import { sha1 } from '../../helpers/core-utils'
11 import { isArrayOf } from '../../helpers/custom-validators/misc'
12 import { Op, QueryTypes } from 'sequelize'
13 import { MStreamingPlaylist, MVideoFile } from '@server/typings/models'
16 tableName: 'videoStreamingPlaylist',
22 fields: [ 'videoId', 'type' ],
26 fields: [ 'p2pMediaLoaderInfohashes' ],
31 export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistModel> {
40 type: VideoStreamingPlaylistType
43 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
44 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
48 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
49 @Column(DataType.ARRAY(DataType.STRING))
50 p2pMediaLoaderInfohashes: string[]
54 p2pMediaLoaderPeerVersion: number
57 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
59 segmentsSha256Url: string
61 @ForeignKey(() => VideoModel)
65 @BelongsTo(() => VideoModel, {
73 @HasMany(() => VideoRedundancyModel, {
80 RedundancyVideos: VideoRedundancyModel[]
82 static doesInfohashExist (infoHash: string) {
83 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
85 type: QueryTypes.SELECT as QueryTypes.SELECT,
90 return VideoModel.sequelize.query<object>(query, options)
91 .then(results => results.length === 1)
94 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: MVideoFile[]) {
95 const hashes: string[] = []
97 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
98 for (let i = 0; i < videoFiles.length; i++) {
99 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
105 static listByIncorrectPeerVersion () {
108 p2pMediaLoaderPeerVersion: {
109 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
114 return VideoStreamingPlaylistModel.findAll(query)
117 static loadWithVideo (id: number) {
121 model: VideoModel.unscoped(),
127 return VideoStreamingPlaylistModel.findByPk(id, options)
130 static getHlsPlaylistFilename (resolution: number) {
131 return resolution + '.m3u8'
134 static getMasterHlsPlaylistFilename () {
138 static getHlsSha256SegmentsFilename () {
139 return 'segments-sha256.json'
142 static getHlsVideoName (uuid: string, resolution: number) {
143 return `${uuid}-${resolution}-fragmented.mp4`
146 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
147 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
150 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
151 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
154 static getHlsSha256SegmentsStaticPath (videoUUID: string) {
155 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
159 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
164 getVideoRedundancyUrl (baseUrlHttp: string) {
165 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
168 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
169 return this.type === other.type &&
170 this.videoId === other.videoId