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_DOWNLOAD_PATHS, 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'
14 import { VideoFileModel } from '@server/models/video/video-file'
15 import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths'
18 tableName: 'videoStreamingPlaylist',
24 fields: [ 'videoId', 'type' ],
28 fields: [ 'p2pMediaLoaderInfohashes' ],
33 export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistModel> {
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 doesInfohashExist (infoHash: string) {
93 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
95 type: QueryTypes.SELECT as QueryTypes.SELECT,
100 return VideoModel.sequelize.query<object>(query, options)
101 .then(results => results.length === 1)
104 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
105 const hashes: string[] = []
107 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
108 for (let i = 0; i < files.length; i++) {
109 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
115 static listByIncorrectPeerVersion () {
118 p2pMediaLoaderPeerVersion: {
119 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
124 return VideoStreamingPlaylistModel.findAll(query)
127 static loadWithVideo (id: number) {
131 model: VideoModel.unscoped(),
137 return VideoStreamingPlaylistModel.findByPk(id, options)
140 static getHlsPlaylistFilename (resolution: number) {
141 return resolution + '.m3u8'
144 static getMasterHlsPlaylistFilename () {
148 static getHlsSha256SegmentsFilename () {
149 return 'segments-sha256.json'
152 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
153 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
156 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
157 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
160 static getHlsSha256SegmentsStaticPath (videoUUID: string) {
161 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
165 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
170 getVideoRedundancyUrl (baseUrlHttp: string) {
171 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
174 getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
175 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + getTorrentFileName(this, videoFile)
178 getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
179 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + getVideoFilename(this, videoFile)
182 getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) {
183 return baseUrlHttp + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, this.Video.uuid, getVideoFilename(this, videoFile))
186 getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) {
187 return baseUrlHttp + join(STATIC_PATHS.TORRENTS, getTorrentFileName(this, videoFile))
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