]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-streaming-playlist.ts
Add ability to filter my videos by live
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
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
17 @Table({
18 tableName: 'videoStreamingPlaylist',
19 indexes: [
20 {
21 fields: [ 'videoId' ]
22 },
23 {
24 fields: [ 'videoId', 'type' ],
25 unique: true
26 },
27 {
28 fields: [ 'p2pMediaLoaderInfohashes' ],
29 using: 'gin'
30 }
31 ]
32 })
33 export class VideoStreamingPlaylistModel extends Model {
34 @CreatedAt
35 createdAt: Date
36
37 @UpdatedAt
38 updatedAt: Date
39
40 @AllowNull(false)
41 @Column
42 type: VideoStreamingPlaylistType
43
44 @AllowNull(false)
45 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
47 playlistUrl: string
48
49 @AllowNull(false)
50 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
51 @Column(DataType.ARRAY(DataType.STRING))
52 p2pMediaLoaderInfohashes: string[]
53
54 @AllowNull(false)
55 @Column
56 p2pMediaLoaderPeerVersion: number
57
58 @AllowNull(false)
59 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
60 @Column
61 segmentsSha256Url: string
62
63 @ForeignKey(() => VideoModel)
64 @Column
65 videoId: number
66
67 @BelongsTo(() => VideoModel, {
68 foreignKey: {
69 allowNull: false
70 },
71 onDelete: 'CASCADE'
72 })
73 Video: VideoModel
74
75 @HasMany(() => VideoFileModel, {
76 foreignKey: {
77 allowNull: true
78 },
79 onDelete: 'CASCADE'
80 })
81 VideoFiles: VideoFileModel[]
82
83 @HasMany(() => VideoRedundancyModel, {
84 foreignKey: {
85 allowNull: false
86 },
87 onDelete: 'CASCADE',
88 hooks: true
89 })
90 RedundancyVideos: VideoRedundancyModel[]
91
92 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
93 promise: true,
94 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
95 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
96 })
97
98 static doesInfohashExist (infoHash: string) {
99 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
100 const options = {
101 type: QueryTypes.SELECT as QueryTypes.SELECT,
102 bind: { infoHash },
103 raw: true
104 }
105
106 return VideoModel.sequelize.query<object>(query, options)
107 .then(results => results.length === 1)
108 }
109
110 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
111 const hashes: string[] = []
112
113 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
114 for (let i = 0; i < files.length; i++) {
115 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
116 }
117
118 return hashes
119 }
120
121 static listByIncorrectPeerVersion () {
122 const query = {
123 where: {
124 p2pMediaLoaderPeerVersion: {
125 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
126 }
127 }
128 }
129
130 return VideoStreamingPlaylistModel.findAll(query)
131 }
132
133 static loadWithVideo (id: number) {
134 const options = {
135 include: [
136 {
137 model: VideoModel.unscoped(),
138 required: true
139 }
140 ]
141 }
142
143 return VideoStreamingPlaylistModel.findByPk(id, options)
144 }
145
146 static loadHLSPlaylistByVideo (videoId: number) {
147 const options = {
148 where: {
149 type: VideoStreamingPlaylistType.HLS,
150 videoId
151 }
152 }
153
154 return VideoStreamingPlaylistModel.findOne(options)
155 }
156
157 static getHlsPlaylistFilename (resolution: number) {
158 return resolution + '.m3u8'
159 }
160
161 static getMasterHlsPlaylistFilename () {
162 return 'master.m3u8'
163 }
164
165 static getHlsSha256SegmentsFilename () {
166 return 'segments-sha256.json'
167 }
168
169 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
170 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
171 }
172
173 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
174 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
175 }
176
177 static getHlsSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
178 if (isLive) return join('/live', 'segments-sha256', videoUUID)
179
180 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
181 }
182
183 getStringType () {
184 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
185
186 return 'unknown'
187 }
188
189 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
190 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
191 }
192
193 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
194 return this.type === other.type &&
195 this.videoId === other.videoId
196 }
197 }