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