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, MStreamingPlaylistVideo, MVideoFile } from '@server/types/models'
21 import { VideoFileModel } from '@server/models/video/video-file'
22 import { getTorrentFileName, getTorrentFilePath, getVideoFilename } from '@server/lib/video-paths'
23 import * as memoizee from 'memoizee'
24 import { remove } from 'fs-extra'
25 import { logger } from '@server/helpers/logger'
28 tableName: 'videoStreamingPlaylist',
34 fields: [ 'videoId', 'type' ],
38 fields: [ 'p2pMediaLoaderInfohashes' ],
43 export class VideoStreamingPlaylistModel extends Model {
52 type: VideoStreamingPlaylistType
55 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
60 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
61 @Column(DataType.ARRAY(DataType.STRING))
62 p2pMediaLoaderInfohashes: string[]
66 p2pMediaLoaderPeerVersion: number
69 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
71 segmentsSha256Url: string
73 @ForeignKey(() => VideoModel)
77 @BelongsTo(() => VideoModel, {
85 @HasMany(() => VideoFileModel, {
91 VideoFiles: VideoFileModel[]
93 @HasMany(() => VideoRedundancyModel, {
100 RedundancyVideos: VideoRedundancyModel[]
102 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
104 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
105 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
108 static doesInfohashExist (infoHash: string) {
109 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
111 type: QueryTypes.SELECT as QueryTypes.SELECT,
116 return VideoModel.sequelize.query<object>(query, options)
117 .then(results => results.length === 1)
120 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
121 const hashes: string[] = []
123 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
124 for (let i = 0; i < files.length; i++) {
125 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
131 static listByIncorrectPeerVersion () {
134 p2pMediaLoaderPeerVersion: {
135 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
140 return VideoStreamingPlaylistModel.findAll(query)
143 static loadWithVideo (id: number) {
147 model: VideoModel.unscoped(),
153 return VideoStreamingPlaylistModel.findByPk(id, options)
156 static loadHLSPlaylistByVideo (videoId: number) {
159 type: VideoStreamingPlaylistType.HLS,
164 return VideoStreamingPlaylistModel.findOne(options)
167 static getHlsPlaylistFilename (resolution: number) {
168 return resolution + '.m3u8'
171 static getMasterHlsPlaylistFilename () {
175 static getHlsSha256SegmentsFilename () {
176 return 'segments-sha256.json'
179 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
180 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
183 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
184 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
187 static getHlsSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
188 if (isLive) return join('/live', 'segments-sha256', videoUUID)
190 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
194 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
199 getVideoRedundancyUrl (baseUrlHttp: string) {
200 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
203 getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
204 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + getTorrentFileName(this, videoFile)
207 getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
208 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + getVideoFilename(this, videoFile)
211 getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) {
212 return baseUrlHttp + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, this.Video.uuid, getVideoFilename(this, videoFile))
215 getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) {
216 return baseUrlHttp + join(STATIC_PATHS.TORRENTS, getTorrentFileName(this, videoFile))
219 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
220 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
223 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
224 return this.type === other.type &&
225 this.videoId === other.videoId
228 removeTorrent (this: MStreamingPlaylistVideo, videoFile: MVideoFile) {
229 const torrentPath = getTorrentFilePath(this, videoFile)
230 return remove(torrentPath)
231 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))