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 * as Sequelize from 'sequelize'
6 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
7 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
8 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
9 import { CONSTRAINTS_FIELDS, STATIC_PATHS } from '../../initializers'
10 import { VideoFileModel } from './video-file'
11 import { join } from 'path'
12 import { sha1 } from '../../helpers/core-utils'
13 import { isArrayOf } from '../../helpers/custom-validators/misc'
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[]
53 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
55 segmentsSha256Url: string
57 @ForeignKey(() => VideoModel)
61 @BelongsTo(() => VideoModel, {
69 @HasMany(() => VideoRedundancyModel, {
76 RedundancyVideos: VideoRedundancyModel[]
78 static doesInfohashExist (infoHash: string) {
79 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
81 type: Sequelize.QueryTypes.SELECT,
86 return VideoModel.sequelize.query(query, options)
88 return results.length === 1
92 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: VideoFileModel[]) {
93 const hashes: string[] = []
95 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L97
96 for (let i = 0; i < videoFiles.length; i++) {
97 hashes.push(sha1(`1${playlistUrl}+V${i}`))
103 static loadWithVideo (id: number) {
107 model: VideoModel.unscoped(),
113 return VideoStreamingPlaylistModel.findById(id, options)
116 static getHlsPlaylistFilename (resolution: number) {
117 return resolution + '.m3u8'
120 static getMasterHlsPlaylistFilename () {
124 static getHlsSha256SegmentsFilename () {
125 return 'segments-sha256.json'
128 static getHlsVideoName (uuid: string, resolution: number) {
129 return `${uuid}-${resolution}-fragmented.mp4`
132 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
133 return join(STATIC_PATHS.PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
136 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
137 return join(STATIC_PATHS.PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
140 static getHlsSha256SegmentsStaticPath (videoUUID: string) {
141 return join(STATIC_PATHS.PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
145 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
150 getVideoRedundancyUrl (baseUrlHttp: string) {
151 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
154 hasSameUniqueKeysThan (other: VideoStreamingPlaylistModel) {
155 return this.type === other.type &&
156 this.videoId === other.videoId