1 import * as memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op, QueryTypes } from 'sequelize'
4 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
5 import { VideoFileModel } from '@server/models/video/video-file'
6 import { MStreamingPlaylist } from '@server/types/models'
7 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
8 import { sha1 } from '../../helpers/core-utils'
9 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
10 import { isArrayOf } from '../../helpers/custom-validators/misc'
11 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
12 import { CONSTRAINTS_FIELDS, MEMOIZE_LENGTH, MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_PATHS } from '../../initializers/constants'
13 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
14 import { throwIfNotValid } from '../utils'
15 import { VideoModel } from './video'
18 tableName: 'videoStreamingPlaylist',
24 fields: [ 'videoId', 'type' ],
28 fields: [ 'p2pMediaLoaderInfohashes' ],
33 export class VideoStreamingPlaylistModel extends Model {
42 type: VideoStreamingPlaylistType
45 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
50 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
51 @Column(DataType.ARRAY(DataType.STRING))
52 p2pMediaLoaderInfohashes: string[]
56 p2pMediaLoaderPeerVersion: number
59 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
61 segmentsSha256Url: string
63 @ForeignKey(() => VideoModel)
67 @BelongsTo(() => VideoModel, {
75 @HasMany(() => VideoFileModel, {
81 VideoFiles: VideoFileModel[]
83 @HasMany(() => VideoRedundancyModel, {
90 RedundancyVideos: VideoRedundancyModel[]
92 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
94 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
95 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
98 static doesInfohashExist (infoHash: string) {
99 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
101 type: QueryTypes.SELECT as QueryTypes.SELECT,
106 return VideoModel.sequelize.query<object>(query, options)
107 .then(results => results.length === 1)
110 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
111 const hashes: string[] = []
113 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
114 for (let i = 0; i < files.length; i++) {
115 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
121 static listByIncorrectPeerVersion () {
124 p2pMediaLoaderPeerVersion: {
125 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
130 return VideoStreamingPlaylistModel.findAll(query)
133 static loadWithVideo (id: number) {
137 model: VideoModel.unscoped(),
143 return VideoStreamingPlaylistModel.findByPk(id, options)
146 static loadHLSPlaylistByVideo (videoId: number) {
149 type: VideoStreamingPlaylistType.HLS,
154 return VideoStreamingPlaylistModel.findOne(options)
157 static getHlsPlaylistFilename (resolution: number) {
158 return resolution + '.m3u8'
161 static getMasterHlsPlaylistFilename () {
165 static getHlsSha256SegmentsFilename () {
166 return 'segments-sha256.json'
169 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
170 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
173 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
174 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
177 static getHlsSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
178 if (isLive) return join('/live', 'segments-sha256', videoUUID)
180 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
184 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
189 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
190 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
193 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
194 return this.type === other.type &&
195 this.videoId === other.videoId