]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
90a8bd30
C
1import * as memoizee from 'memoizee'
2import { join } from 'path'
3import { Op, QueryTypes } from 'sequelize'
453e83ea 4import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
90a8bd30
C
5import { VideoFileModel } from '@server/models/video/video-file'
6import { MStreamingPlaylist } from '@server/types/models'
09209296 7import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
09209296 8import { sha1 } from '../../helpers/core-utils'
90a8bd30 9import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
09209296 10import { isArrayOf } from '../../helpers/custom-validators/misc'
90a8bd30
C
11import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
12import { CONSTRAINTS_FIELDS, MEMOIZE_LENGTH, MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_PATHS } from '../../initializers/constants'
13import { VideoRedundancyModel } from '../redundancy/video-redundancy'
14import { throwIfNotValid } from '../utils'
15import { VideoModel } from './video'
09209296
C
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 }
3acc5084 31 ]
09209296 32})
b49f22d8 33export class VideoStreamingPlaylistModel extends Model {
09209296
C
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'))
3acc5084 51 @Column(DataType.ARRAY(DataType.STRING))
09209296
C
52 p2pMediaLoaderInfohashes: string[]
53
ae9bbed4
C
54 @AllowNull(false)
55 @Column
56 p2pMediaLoaderPeerVersion: number
57
09209296
C
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
d7a25329
C
75 @HasMany(() => VideoFileModel, {
76 foreignKey: {
77 allowNull: true
78 },
79 onDelete: 'CASCADE'
80 })
81 VideoFiles: VideoFileModel[]
82
09209296
C
83 @HasMany(() => VideoRedundancyModel, {
84 foreignKey: {
85 allowNull: false
86 },
87 onDelete: 'CASCADE',
88 hooks: true
89 })
90 RedundancyVideos: VideoRedundancyModel[]
91
35f28e94
C
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
09209296
C
98 static doesInfohashExist (infoHash: string) {
99 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
100 const options = {
1735c825 101 type: QueryTypes.SELECT as QueryTypes.SELECT,
09209296
C
102 bind: { infoHash },
103 raw: true
104 }
105
3acc5084 106 return VideoModel.sequelize.query<object>(query, options)
1735c825 107 .then(results => results.length === 1)
09209296
C
108 }
109
d7a25329 110 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
09209296
C
111 const hashes: string[] = []
112
ae9bbed4 113 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
d7a25329 114 for (let i = 0; i < files.length; i++) {
ae9bbed4 115 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
09209296
C
116 }
117
118 return hashes
119 }
120
ae9bbed4
C
121 static listByIncorrectPeerVersion () {
122 const query = {
123 where: {
124 p2pMediaLoaderPeerVersion: {
1735c825 125 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
ae9bbed4
C
126 }
127 }
128 }
129
130 return VideoStreamingPlaylistModel.findAll(query)
131 }
132
09209296
C
133 static loadWithVideo (id: number) {
134 const options = {
135 include: [
136 {
137 model: VideoModel.unscoped(),
138 required: true
139 }
140 ]
141 }
142
9b39106d 143 return VideoStreamingPlaylistModel.findByPk(id, options)
09209296
C
144 }
145
a5cf76af
C
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
09209296
C
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) {
9c6ca37f 170 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
09209296
C
171 }
172
173 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
9c6ca37f 174 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
09209296
C
175 }
176
c6c0fa6c
C
177 static getHlsSha256SegmentsStaticPath (videoUUID: string, isLive: boolean) {
178 if (isLive) return join('/live', 'segments-sha256', videoUUID)
179
9c6ca37f 180 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
09209296
C
181 }
182
183 getStringType () {
184 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
185
186 return 'unknown'
187 }
188
d7a25329
C
189 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
190 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
191 }
192
453e83ea 193 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
09209296
C
194 return this.type === other.type &&
195 this.videoId === other.videoId
196 }
197}