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