]>
Commit | Line | Data |
---|---|---|
90a8bd30 C |
1 | import { remove } from 'fs-extra' |
2 | import * as memoizee from 'memoizee' | |
3 | import { join } from 'path' | |
764b1a14 | 4 | import { FindOptions, Op, Transaction } from 'sequelize' |
c48e82b5 C |
5 | import { |
6 | AllowNull, | |
7 | BelongsTo, | |
8 | Column, | |
9 | CreatedAt, | |
10 | DataType, | |
11 | Default, | |
90a8bd30 | 12 | DefaultScope, |
c48e82b5 C |
13 | ForeignKey, |
14 | HasMany, | |
15 | Is, | |
16 | Model, | |
8319d6ae | 17 | Scopes, |
90a8bd30 C |
18 | Table, |
19 | UpdatedAt | |
c48e82b5 | 20 | } from 'sequelize-typescript' |
90a8bd30 C |
21 | import { Where } from 'sequelize/types/lib/utils' |
22 | import validator from 'validator' | |
23 | import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' | |
24 | import { logger } from '@server/helpers/logger' | |
25 | import { extractVideo } from '@server/helpers/video' | |
26 | import { getTorrentFilePath } from '@server/lib/video-paths' | |
27 | import { MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' | |
16c016e8 | 28 | import { AttributesOnly } from '@shared/core-utils' |
3a6f351b | 29 | import { |
14e2014a | 30 | isVideoFileExtnameValid, |
3a6f351b C |
31 | isVideoFileInfoHashValid, |
32 | isVideoFileResolutionValid, | |
33 | isVideoFileSizeValid, | |
34 | isVideoFPSResolutionValid | |
35 | } from '../../helpers/custom-validators/videos' | |
90a8bd30 C |
36 | import { |
37 | LAZY_STATIC_PATHS, | |
38 | MEMOIZE_LENGTH, | |
39 | MEMOIZE_TTL, | |
40 | MIMETYPES, | |
41 | STATIC_DOWNLOAD_PATHS, | |
42 | STATIC_PATHS, | |
43 | WEBSERVER | |
44 | } from '../../initializers/constants' | |
45 | import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file' | |
46 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' | |
fa47956e | 47 | import { doesExist } from '../shared' |
3acc5084 | 48 | import { parseAggregateResult, throwIfNotValid } from '../utils' |
3fd3ab2d | 49 | import { VideoModel } from './video' |
ae9bbed4 | 50 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' |
93e1258c | 51 | |
8319d6ae RK |
52 | export enum ScopeNames { |
53 | WITH_VIDEO = 'WITH_VIDEO', | |
90a8bd30 C |
54 | WITH_METADATA = 'WITH_METADATA', |
55 | WITH_VIDEO_OR_PLAYLIST = 'WITH_VIDEO_OR_PLAYLIST' | |
8319d6ae RK |
56 | } |
57 | ||
8319d6ae RK |
58 | @DefaultScope(() => ({ |
59 | attributes: { | |
7b81edc8 | 60 | exclude: [ 'metadata' ] |
8319d6ae RK |
61 | } |
62 | })) | |
63 | @Scopes(() => ({ | |
64 | [ScopeNames.WITH_VIDEO]: { | |
65 | include: [ | |
66 | { | |
67 | model: VideoModel.unscoped(), | |
68 | required: true | |
69 | } | |
70 | ] | |
71 | }, | |
90a8bd30 C |
72 | [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: Where } = {}) => { |
73 | return { | |
74 | include: [ | |
75 | { | |
76 | model: VideoModel.unscoped(), | |
77 | required: false, | |
78 | where: options.whereVideo | |
79 | }, | |
80 | { | |
81 | model: VideoStreamingPlaylistModel.unscoped(), | |
82 | required: false, | |
83 | include: [ | |
84 | { | |
85 | model: VideoModel.unscoped(), | |
86 | required: true, | |
87 | where: options.whereVideo | |
88 | } | |
89 | ] | |
90 | } | |
91 | ] | |
92 | } | |
93 | }, | |
8319d6ae RK |
94 | [ScopeNames.WITH_METADATA]: { |
95 | attributes: { | |
7b81edc8 | 96 | include: [ 'metadata' ] |
8319d6ae RK |
97 | } |
98 | } | |
99 | })) | |
3fd3ab2d C |
100 | @Table({ |
101 | tableName: 'videoFile', | |
102 | indexes: [ | |
93e1258c | 103 | { |
d7a25329 C |
104 | fields: [ 'videoId' ], |
105 | where: { | |
106 | videoId: { | |
107 | [Op.ne]: null | |
108 | } | |
109 | } | |
110 | }, | |
111 | { | |
112 | fields: [ 'videoStreamingPlaylistId' ], | |
113 | where: { | |
114 | videoStreamingPlaylistId: { | |
115 | [Op.ne]: null | |
116 | } | |
117 | } | |
93e1258c | 118 | }, |
d7a25329 | 119 | |
93e1258c | 120 | { |
3fd3ab2d | 121 | fields: [ 'infoHash' ] |
8cd72bd3 | 122 | }, |
d7a25329 | 123 | |
90a8bd30 C |
124 | { |
125 | fields: [ 'torrentFilename' ], | |
126 | unique: true | |
127 | }, | |
128 | ||
129 | { | |
130 | fields: [ 'filename' ], | |
131 | unique: true | |
132 | }, | |
133 | ||
8cd72bd3 C |
134 | { |
135 | fields: [ 'videoId', 'resolution', 'fps' ], | |
d7a25329 C |
136 | unique: true, |
137 | where: { | |
138 | videoId: { | |
139 | [Op.ne]: null | |
140 | } | |
141 | } | |
142 | }, | |
143 | { | |
144 | fields: [ 'videoStreamingPlaylistId', 'resolution', 'fps' ], | |
145 | unique: true, | |
146 | where: { | |
147 | videoStreamingPlaylistId: { | |
148 | [Op.ne]: null | |
149 | } | |
150 | } | |
93e1258c | 151 | } |
93e1258c | 152 | ] |
3fd3ab2d | 153 | }) |
16c016e8 | 154 | export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel>>> { |
3fd3ab2d C |
155 | @CreatedAt |
156 | createdAt: Date | |
157 | ||
158 | @UpdatedAt | |
159 | updatedAt: Date | |
160 | ||
161 | @AllowNull(false) | |
162 | @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution')) | |
163 | @Column | |
164 | resolution: number | |
165 | ||
166 | @AllowNull(false) | |
167 | @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size')) | |
168 | @Column(DataType.BIGINT) | |
169 | size: number | |
170 | ||
171 | @AllowNull(false) | |
14e2014a C |
172 | @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname')) |
173 | @Column | |
3fd3ab2d C |
174 | extname: string |
175 | ||
c6c0fa6c C |
176 | @AllowNull(true) |
177 | @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash', true)) | |
3fd3ab2d C |
178 | @Column |
179 | infoHash: string | |
180 | ||
2e7cf5ae C |
181 | @AllowNull(false) |
182 | @Default(-1) | |
3a6f351b C |
183 | @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps')) |
184 | @Column | |
185 | fps: number | |
186 | ||
8319d6ae RK |
187 | @AllowNull(true) |
188 | @Column(DataType.JSONB) | |
189 | metadata: any | |
190 | ||
191 | @AllowNull(true) | |
192 | @Column | |
193 | metadataUrl: string | |
194 | ||
90a8bd30 C |
195 | @AllowNull(true) |
196 | @Column | |
197 | fileUrl: string | |
198 | ||
199 | // Could be null for live files | |
200 | @AllowNull(true) | |
201 | @Column | |
202 | filename: string | |
203 | ||
204 | @AllowNull(true) | |
205 | @Column | |
206 | torrentUrl: string | |
207 | ||
208 | // Could be null for live files | |
209 | @AllowNull(true) | |
210 | @Column | |
211 | torrentFilename: string | |
212 | ||
3fd3ab2d C |
213 | @ForeignKey(() => VideoModel) |
214 | @Column | |
215 | videoId: number | |
216 | ||
217 | @BelongsTo(() => VideoModel, { | |
93e1258c | 218 | foreignKey: { |
d7a25329 | 219 | allowNull: true |
93e1258c C |
220 | }, |
221 | onDelete: 'CASCADE' | |
222 | }) | |
3fd3ab2d | 223 | Video: VideoModel |
cc43831a | 224 | |
d7a25329 C |
225 | @ForeignKey(() => VideoStreamingPlaylistModel) |
226 | @Column | |
227 | videoStreamingPlaylistId: number | |
228 | ||
229 | @BelongsTo(() => VideoStreamingPlaylistModel, { | |
230 | foreignKey: { | |
231 | allowNull: true | |
232 | }, | |
233 | onDelete: 'CASCADE' | |
234 | }) | |
235 | VideoStreamingPlaylist: VideoStreamingPlaylistModel | |
236 | ||
c48e82b5 C |
237 | @HasMany(() => VideoRedundancyModel, { |
238 | foreignKey: { | |
09209296 | 239 | allowNull: true |
c48e82b5 C |
240 | }, |
241 | onDelete: 'CASCADE', | |
242 | hooks: true | |
243 | }) | |
244 | RedundancyVideos: VideoRedundancyModel[] | |
245 | ||
35f28e94 C |
246 | static doesInfohashExistCached = memoizee(VideoFileModel.doesInfohashExist, { |
247 | promise: true, | |
248 | max: MEMOIZE_LENGTH.INFO_HASH_EXISTS, | |
249 | maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS | |
250 | }) | |
251 | ||
09209296 | 252 | static doesInfohashExist (infoHash: string) { |
cc43831a | 253 | const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1' |
cc43831a | 254 | |
764b1a14 | 255 | return doesExist(query, { infoHash }) |
cc43831a | 256 | } |
e5565833 | 257 | |
8319d6ae RK |
258 | static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) { |
259 | const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID) | |
7b81edc8 C |
260 | |
261 | return !!videoFile | |
8319d6ae RK |
262 | } |
263 | ||
764b1a14 C |
264 | static async doesOwnedTorrentFileExist (filename: string) { |
265 | const query = 'SELECT 1 FROM "videoFile" ' + | |
266 | 'LEFT JOIN "video" "webtorrent" ON "webtorrent"."id" = "videoFile"."videoId" AND "webtorrent"."remote" IS FALSE ' + | |
267 | 'LEFT JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' + | |
268 | 'LEFT JOIN "video" "hlsVideo" ON "hlsVideo"."id" = "videoStreamingPlaylist"."videoId" AND "hlsVideo"."remote" IS FALSE ' + | |
269 | 'WHERE "torrentFilename" = $filename AND ("hlsVideo"."id" IS NOT NULL OR "webtorrent"."id" IS NOT NULL) LIMIT 1' | |
270 | ||
271 | return doesExist(query, { filename }) | |
272 | } | |
273 | ||
274 | static async doesOwnedWebTorrentVideoFileExist (filename: string) { | |
275 | const query = 'SELECT 1 FROM "videoFile" INNER JOIN "video" ON "video"."id" = "videoFile"."videoId" AND "video"."remote" IS FALSE ' + | |
276 | 'WHERE "filename" = $filename LIMIT 1' | |
277 | ||
278 | return doesExist(query, { filename }) | |
279 | } | |
280 | ||
281 | static loadByFilename (filename: string) { | |
282 | const query = { | |
283 | where: { | |
284 | filename | |
285 | } | |
286 | } | |
287 | ||
288 | return VideoFileModel.findOne(query) | |
289 | } | |
290 | ||
90a8bd30 C |
291 | static loadWithVideoOrPlaylistByTorrentFilename (filename: string) { |
292 | const query = { | |
293 | where: { | |
294 | torrentFilename: filename | |
295 | } | |
296 | } | |
297 | ||
298 | return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) | |
299 | } | |
300 | ||
8319d6ae RK |
301 | static loadWithMetadata (id: number) { |
302 | return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id) | |
303 | } | |
304 | ||
25378bc8 | 305 | static loadWithVideo (id: number) { |
8319d6ae RK |
306 | return VideoFileModel.scope(ScopeNames.WITH_VIDEO).findByPk(id) |
307 | } | |
25378bc8 | 308 | |
8319d6ae | 309 | static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) { |
7b81edc8 C |
310 | const whereVideo = validator.isUUID(videoIdOrUUID + '') |
311 | ? { uuid: videoIdOrUUID } | |
312 | : { id: videoIdOrUUID } | |
313 | ||
314 | const options = { | |
315 | where: { | |
316 | id | |
90a8bd30 | 317 | } |
7b81edc8 C |
318 | } |
319 | ||
90a8bd30 C |
320 | return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, whereVideo ] }) |
321 | .findOne(options) | |
7b81edc8 C |
322 | .then(file => { |
323 | // We used `required: false` so check we have at least a video or a streaming playlist | |
324 | if (!file.Video && !file.VideoStreamingPlaylist) return null | |
325 | ||
326 | return file | |
327 | }) | |
25378bc8 C |
328 | } |
329 | ||
3acc5084 | 330 | static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) { |
ae9bbed4 C |
331 | const query = { |
332 | include: [ | |
333 | { | |
334 | model: VideoModel.unscoped(), | |
335 | required: true, | |
336 | include: [ | |
337 | { | |
338 | model: VideoStreamingPlaylistModel.unscoped(), | |
339 | required: true, | |
340 | where: { | |
341 | id: streamingPlaylistId | |
342 | } | |
343 | } | |
344 | ] | |
345 | } | |
346 | ], | |
347 | transaction | |
348 | } | |
349 | ||
350 | return VideoFileModel.findAll(query) | |
351 | } | |
352 | ||
3acc5084 | 353 | static getStats () { |
0b84383d | 354 | const webtorrentFilesQuery: FindOptions = { |
44b9c0ba C |
355 | include: [ |
356 | { | |
357 | attributes: [], | |
0b84383d | 358 | required: true, |
44b9c0ba C |
359 | model: VideoModel.unscoped(), |
360 | where: { | |
361 | remote: false | |
362 | } | |
363 | } | |
364 | ] | |
44b9c0ba | 365 | } |
3acc5084 | 366 | |
0b84383d C |
367 | const hlsFilesQuery: FindOptions = { |
368 | include: [ | |
369 | { | |
370 | attributes: [], | |
371 | required: true, | |
372 | model: VideoStreamingPlaylistModel.unscoped(), | |
373 | include: [ | |
374 | { | |
375 | attributes: [], | |
376 | model: VideoModel.unscoped(), | |
377 | required: true, | |
378 | where: { | |
379 | remote: false | |
380 | } | |
381 | } | |
382 | ] | |
383 | } | |
384 | ] | |
385 | } | |
386 | ||
387 | return Promise.all([ | |
388 | VideoFileModel.aggregate('size', 'SUM', webtorrentFilesQuery), | |
389 | VideoFileModel.aggregate('size', 'SUM', hlsFilesQuery) | |
390 | ]).then(([ webtorrentResult, hlsResult ]) => ({ | |
391 | totalLocalVideoFilesSize: parseAggregateResult(webtorrentResult) + parseAggregateResult(hlsResult) | |
392 | })) | |
44b9c0ba C |
393 | } |
394 | ||
d7a25329 C |
395 | // Redefine upsert because sequelize does not use an appropriate where clause in the update query with 2 unique indexes |
396 | static async customUpsert ( | |
397 | videoFile: MVideoFile, | |
398 | mode: 'streaming-playlist' | 'video', | |
399 | transaction: Transaction | |
400 | ) { | |
401 | const baseWhere = { | |
402 | fps: videoFile.fps, | |
403 | resolution: videoFile.resolution | |
404 | } | |
405 | ||
406 | if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId }) | |
407 | else Object.assign(baseWhere, { videoId: videoFile.videoId }) | |
408 | ||
409 | const element = await VideoFileModel.findOne({ where: baseWhere, transaction }) | |
410 | if (!element) return videoFile.save({ transaction }) | |
411 | ||
412 | for (const k of Object.keys(videoFile.toJSON())) { | |
413 | element[k] = videoFile[k] | |
414 | } | |
415 | ||
416 | return element.save({ transaction }) | |
417 | } | |
418 | ||
97969c4e C |
419 | static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) { |
420 | const options = { | |
421 | where: { videoStreamingPlaylistId } | |
422 | } | |
423 | ||
424 | return VideoFileModel.destroy(options) | |
425 | } | |
426 | ||
c4d12552 C |
427 | hasTorrent () { |
428 | return this.infoHash && this.torrentFilename | |
429 | } | |
430 | ||
d7a25329 C |
431 | getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo { |
432 | if (this.videoId) return (this as MVideoFileVideo).Video | |
433 | ||
434 | return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist | |
435 | } | |
436 | ||
90a8bd30 C |
437 | getVideo (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo { |
438 | return extractVideo(this.getVideoOrStreamingPlaylist()) | |
439 | } | |
440 | ||
536598cf C |
441 | isAudio () { |
442 | return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname] | |
443 | } | |
444 | ||
bd54ad19 C |
445 | isLive () { |
446 | return this.size === -1 | |
447 | } | |
448 | ||
053aed43 | 449 | isHLS () { |
9e2b2e76 | 450 | return !!this.videoStreamingPlaylistId |
053aed43 C |
451 | } |
452 | ||
8efc27bf | 453 | getFileUrl (video: MVideo) { |
90a8bd30 C |
454 | if (!this.Video) this.Video = video as VideoModel |
455 | ||
456 | if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video) | |
90a8bd30 | 457 | |
d9a2a031 | 458 | return this.fileUrl |
90a8bd30 C |
459 | } |
460 | ||
461 | getFileStaticPath (video: MVideo) { | |
462 | if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) | |
463 | ||
464 | return join(STATIC_PATHS.WEBSEED, this.filename) | |
465 | } | |
466 | ||
467 | getFileDownloadUrl (video: MVideoWithHost) { | |
764b1a14 C |
468 | const path = this.isHLS() |
469 | ? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`) | |
470 | : join(STATIC_DOWNLOAD_PATHS.VIDEOS, `${video.uuid}-${this.resolution}${this.extname}`) | |
90a8bd30 C |
471 | |
472 | if (video.isOwned()) return WEBSERVER.URL + path | |
473 | ||
474 | // FIXME: don't guess remote URL | |
475 | return buildRemoteVideoBaseUrl(video, path) | |
476 | } | |
477 | ||
8efc27bf | 478 | getRemoteTorrentUrl (video: MVideo) { |
90a8bd30 C |
479 | if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`) |
480 | ||
d9a2a031 | 481 | return this.torrentUrl |
90a8bd30 C |
482 | } |
483 | ||
484 | // We proxify torrent requests so use a local URL | |
485 | getTorrentUrl () { | |
d61893f7 C |
486 | if (!this.torrentFilename) return null |
487 | ||
90a8bd30 C |
488 | return WEBSERVER.URL + this.getTorrentStaticPath() |
489 | } | |
490 | ||
491 | getTorrentStaticPath () { | |
d61893f7 C |
492 | if (!this.torrentFilename) return null |
493 | ||
90a8bd30 C |
494 | return join(LAZY_STATIC_PATHS.TORRENTS, this.torrentFilename) |
495 | } | |
496 | ||
497 | getTorrentDownloadUrl () { | |
d61893f7 C |
498 | if (!this.torrentFilename) return null |
499 | ||
90a8bd30 C |
500 | return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.TORRENTS, this.torrentFilename) |
501 | } | |
502 | ||
503 | removeTorrent () { | |
d61893f7 C |
504 | if (!this.torrentFilename) return null |
505 | ||
90a8bd30 C |
506 | const torrentPath = getTorrentFilePath(this) |
507 | return remove(torrentPath) | |
508 | .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) | |
509 | } | |
510 | ||
453e83ea | 511 | hasSameUniqueKeysThan (other: MVideoFile) { |
e5565833 C |
512 | return this.fps === other.fps && |
513 | this.resolution === other.resolution && | |
d7a25329 C |
514 | ( |
515 | (this.videoId !== null && this.videoId === other.videoId) || | |
516 | (this.videoStreamingPlaylistId !== null && this.videoStreamingPlaylistId === other.videoStreamingPlaylistId) | |
517 | ) | |
e5565833 | 518 | } |
93e1258c | 519 | } |