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'
16 import { AttributesOnly } from '@shared/core-utils'
19 tableName: 'videoStreamingPlaylist',
25 fields: [ 'videoId', 'type' ],
29 fields: [ 'p2pMediaLoaderInfohashes' ],
34 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
43 type: VideoStreamingPlaylistType
46 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
47 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
51 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
52 @Column(DataType.ARRAY(DataType.STRING))
53 p2pMediaLoaderInfohashes: string[]
57 p2pMediaLoaderPeerVersion: number
60 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
62 segmentsSha256Url: string
64 @ForeignKey(() => VideoModel)
68 @BelongsTo(() => VideoModel, {
76 @HasMany(() => VideoFileModel, {
82 VideoFiles: VideoFileModel[]
84 @HasMany(() => VideoRedundancyModel, {
91 RedundancyVideos: VideoRedundancyModel[]
93 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
95 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
96 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
99 static doesInfohashExist (infoHash: string) {
100 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
102 type: QueryTypes.SELECT as QueryTypes.SELECT,
107 return VideoModel.sequelize.query<object>(query, options)
108 .then(results => results.length === 1)
111 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
112 const hashes: string[] = []
114 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
115 for (let i = 0; i < files.length; i++) {
116 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
122 static listByIncorrectPeerVersion () {
125 p2pMediaLoaderPeerVersion: {
126 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
131 return VideoStreamingPlaylistModel.findAll(query)
134 static loadWithVideo (id: number) {
138 model: VideoModel.unscoped(),
144 return VideoStreamingPlaylistModel.findByPk(id, options)
147 static loadHLSPlaylistByVideo (videoId: number) {
150 type: VideoStreamingPlaylistType.HLS,
155 return VideoStreamingPlaylistModel.findOne(options)
158 static getHlsPlaylistFilename (resolution: number) {
159 return resolution + '.m3u8'
162 static getMasterHlsPlaylistFilename () {
166 static getHlsSha256SegmentsFilename () {
167 return 'segments-sha256.json'
170 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
171 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
174 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
175 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
178 static getHlsSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
179 if (isLive) return join('/live', 'segments-sha256', videoUUID)
181 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
185 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
190 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
191 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
194 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
195 return this.type === other.type &&
196 this.videoId === other.videoId