]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-streaming-playlist.ts
Put private videos under a specific subdirectory
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
1 import memoizee from 'memoizee'
2 import { join } from 'path'
3 import { Op, Transaction } from 'sequelize'
4 import {
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 Default,
11 ForeignKey,
12 HasMany,
13 Is,
14 Model,
15 Table,
16 UpdatedAt
17 } from 'sequelize-typescript'
18 import { getHLSPublicFileUrl } from '@server/lib/object-storage'
19 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '@server/lib/paths'
20 import { isVideoInPrivateDirectory } from '@server/lib/video-privacy'
21 import { VideoFileModel } from '@server/models/video/video-file'
22 import { MStreamingPlaylist, MStreamingPlaylistFilesVideo, MVideo } from '@server/types/models'
23 import { sha1 } from '@shared/extra-utils'
24 import { VideoStorage } from '@shared/models'
25 import { AttributesOnly } from '@shared/typescript-utils'
26 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
27 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
28 import { isArrayOf } from '../../helpers/custom-validators/misc'
29 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
30 import {
31 CONSTRAINTS_FIELDS,
32 MEMOIZE_LENGTH,
33 MEMOIZE_TTL,
34 P2P_MEDIA_LOADER_PEER_VERSION,
35 STATIC_PATHS,
36 WEBSERVER
37 } from '../../initializers/constants'
38 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
39 import { doesExist } from '../shared'
40 import { throwIfNotValid } from '../utils'
41 import { VideoModel } from './video'
42
43 @Table({
44 tableName: 'videoStreamingPlaylist',
45 indexes: [
46 {
47 fields: [ 'videoId' ]
48 },
49 {
50 fields: [ 'videoId', 'type' ],
51 unique: true
52 },
53 {
54 fields: [ 'p2pMediaLoaderInfohashes' ],
55 using: 'gin'
56 }
57 ]
58 })
59 export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<VideoStreamingPlaylistModel>>> {
60 @CreatedAt
61 createdAt: Date
62
63 @UpdatedAt
64 updatedAt: Date
65
66 @AllowNull(false)
67 @Column
68 type: VideoStreamingPlaylistType
69
70 @AllowNull(false)
71 @Column
72 playlistFilename: string
73
74 @AllowNull(true)
75 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url', true))
76 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
77 playlistUrl: string
78
79 @AllowNull(false)
80 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
81 @Column(DataType.ARRAY(DataType.STRING))
82 p2pMediaLoaderInfohashes: string[]
83
84 @AllowNull(false)
85 @Column
86 p2pMediaLoaderPeerVersion: number
87
88 @AllowNull(false)
89 @Column
90 segmentsSha256Filename: string
91
92 @AllowNull(true)
93 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url', true))
94 @Column
95 segmentsSha256Url: string
96
97 @ForeignKey(() => VideoModel)
98 @Column
99 videoId: number
100
101 @AllowNull(false)
102 @Default(VideoStorage.FILE_SYSTEM)
103 @Column
104 storage: VideoStorage
105
106 @BelongsTo(() => VideoModel, {
107 foreignKey: {
108 allowNull: false
109 },
110 onDelete: 'CASCADE'
111 })
112 Video: VideoModel
113
114 @HasMany(() => VideoFileModel, {
115 foreignKey: {
116 allowNull: true
117 },
118 onDelete: 'CASCADE'
119 })
120 VideoFiles: VideoFileModel[]
121
122 @HasMany(() => VideoRedundancyModel, {
123 foreignKey: {
124 allowNull: false
125 },
126 onDelete: 'CASCADE',
127 hooks: true
128 })
129 RedundancyVideos: VideoRedundancyModel[]
130
131 static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist, {
132 promise: true,
133 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
134 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
135 })
136
137 static doesInfohashExist (infoHash: string) {
138 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
139
140 return doesExist(query, { infoHash })
141 }
142
143 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
144 const hashes: string[] = []
145
146 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
147 for (let i = 0; i < files.length; i++) {
148 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
149 }
150
151 return hashes
152 }
153
154 static listByIncorrectPeerVersion () {
155 const query = {
156 where: {
157 p2pMediaLoaderPeerVersion: {
158 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
159 }
160 },
161 include: [
162 {
163 model: VideoModel.unscoped(),
164 required: true
165 }
166 ]
167 }
168
169 return VideoStreamingPlaylistModel.findAll(query)
170 }
171
172 static loadWithVideoAndFiles (id: number) {
173 const options = {
174 include: [
175 {
176 model: VideoModel.unscoped(),
177 required: true
178 },
179 {
180 model: VideoFileModel.unscoped()
181 }
182 ]
183 }
184
185 return VideoStreamingPlaylistModel.findByPk<MStreamingPlaylistFilesVideo>(id, options)
186 }
187
188 static loadWithVideo (id: number) {
189 const options = {
190 include: [
191 {
192 model: VideoModel.unscoped(),
193 required: true
194 }
195 ]
196 }
197
198 return VideoStreamingPlaylistModel.findByPk(id, options)
199 }
200
201 static loadHLSPlaylistByVideo (videoId: number, transaction?: Transaction): Promise<MStreamingPlaylist> {
202 const options = {
203 where: {
204 type: VideoStreamingPlaylistType.HLS,
205 videoId
206 },
207 transaction
208 }
209
210 return VideoStreamingPlaylistModel.findOne(options)
211 }
212
213 static async loadOrGenerate (video: MVideo, transaction?: Transaction) {
214 let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id, transaction)
215
216 if (!playlist) {
217 playlist = new VideoStreamingPlaylistModel({
218 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
219 type: VideoStreamingPlaylistType.HLS,
220 storage: VideoStorage.FILE_SYSTEM,
221 p2pMediaLoaderInfohashes: [],
222 playlistFilename: generateHLSMasterPlaylistFilename(video.isLive),
223 segmentsSha256Filename: generateHlsSha256SegmentsFilename(video.isLive),
224 videoId: video.id
225 })
226
227 await playlist.save({ transaction })
228 }
229
230 return Object.assign(playlist, { Video: video })
231 }
232
233 static doesOwnedHLSPlaylistExist (videoUUID: string) {
234 const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
235 `INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
236 `AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
237 `AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
238
239 return doesExist(query, { videoUUID })
240 }
241
242 assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
243 const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
244
245 this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
246 }
247
248 getMasterPlaylistUrl (video: MVideo) {
249 if (video.isOwned()) {
250 if (this.storage === VideoStorage.OBJECT_STORAGE) {
251 return getHLSPublicFileUrl(this.playlistUrl)
252 }
253
254 return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video)
255 }
256
257 return this.playlistUrl
258 }
259
260 getSha256SegmentsUrl (video: MVideo) {
261 if (video.isOwned()) {
262 if (this.storage === VideoStorage.OBJECT_STORAGE) {
263 return getHLSPublicFileUrl(this.segmentsSha256Url)
264 }
265
266 return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video)
267 }
268
269 return this.segmentsSha256Url
270 }
271
272 getStringType () {
273 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
274
275 return 'unknown'
276 }
277
278 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
279 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
280 }
281
282 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
283 return this.type === other.type &&
284 this.videoId === other.videoId
285 }
286
287 withVideo (video: MVideo) {
288 return Object.assign(this, { Video: video })
289 }
290
291 private getMasterPlaylistStaticPath (video: MVideo) {
292 if (isVideoInPrivateDirectory(video.privacy)) {
293 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.playlistFilename)
294 }
295
296 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.playlistFilename)
297 }
298
299 private getSha256SegmentsStaticPath (video: MVideo) {
300 if (isVideoInPrivateDirectory(video.privacy)) {
301 return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.segmentsSha256Filename)
302 }
303
304 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.segmentsSha256Filename)
305 }
306 }