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