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'
12 P2P_MEDIA_LOADER_PEER_VERSION,
13 STATIC_DOWNLOAD_PATHS,
15 } from '../../initializers/constants'
16 import { join } from 'path'
17 import { sha1 } from '../../helpers/core-utils'
18 import { isArrayOf } from '../../helpers/custom-validators/misc'
19 import { Op, QueryTypes } from 'sequelize'
20 import { MStreamingPlaylist, MVideoFile } from '@server/typings/models'
21 import { VideoFileModel } from '@server/models/video/video-file'
22 import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths'
23 import * as memoizee from 'memoizee'
26 tableName: 'videoStreamingPlaylist',
32 fields: [ 'videoId', 'type' ],
36 fields: [ 'p2pMediaLoaderInfohashes' ],
41 export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistModel> {
50 type: VideoStreamingPlaylistType
53 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
54 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
58 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
59 @Column(DataType.ARRAY(DataType.STRING))
60 p2pMediaLoaderInfohashes: string[]
64 p2pMediaLoaderPeerVersion: number
67 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
69 segmentsSha256Url: string
71 @ForeignKey(() => VideoModel)
75 @BelongsTo(() => VideoModel, {
83 @HasMany(() => VideoFileModel, {
89 VideoFiles: VideoFileModel[]
91 @HasMany(() => VideoRedundancyModel, {
98 RedundancyVideos: VideoRedundancyModel[]
100 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
102 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
103 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
106 static doesInfohashExist (infoHash: string) {
107 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
109 type: QueryTypes.SELECT as QueryTypes.SELECT,
114 return VideoModel.sequelize.query<object>(query, options)
115 .then(results => results.length === 1)
118 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
119 const hashes: string[] = []
121 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
122 for (let i = 0; i < files.length; i++) {
123 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
129 static listByIncorrectPeerVersion () {
132 p2pMediaLoaderPeerVersion: {
133 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
138 return VideoStreamingPlaylistModel.findAll(query)
141 static loadWithVideo (id: number) {
145 model: VideoModel.unscoped(),
151 return VideoStreamingPlaylistModel.findByPk(id, options)
154 static getHlsPlaylistFilename (resolution: number) {
155 return resolution + '.m3u8'
158 static getMasterHlsPlaylistFilename () {
162 static getHlsSha256SegmentsFilename () {
163 return 'segments-sha256.json'
166 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
167 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
170 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
171 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
174 static getHlsSha256SegmentsStaticPath (videoUUID: string) {
175 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
179 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
184 getVideoRedundancyUrl (baseUrlHttp: string) {
185 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
188 getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
189 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + getTorrentFileName(this, videoFile)
192 getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
193 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + getVideoFilename(this, videoFile)
196 getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) {
197 return baseUrlHttp + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, this.Video.uuid, getVideoFilename(this, videoFile))
200 getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) {
201 return baseUrlHttp + join(STATIC_PATHS.TORRENTS, getTorrentFileName(this, videoFile))
204 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
205 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
208 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
209 return this.type === other.type &&
210 this.videoId === other.videoId