]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import { |
c48e82b5 | 2 | AllowNull, |
25378bc8 | 3 | BeforeDestroy, |
c48e82b5 C |
4 | BelongsTo, |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | ForeignKey, | |
9 | Is, | |
10 | Model, | |
11 | Scopes, | |
c48e82b5 C |
12 | Table, |
13 | UpdatedAt | |
14 | } from 'sequelize-typescript' | |
15 | import { ActorModel } from '../activitypub/actor' | |
3acc5084 | 16 | import { getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' |
c48e82b5 | 17 | import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
74dc3bca | 18 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' |
c48e82b5 | 19 | import { VideoFileModel } from '../video/video-file' |
c48e82b5 C |
20 | import { getServerActor } from '../../helpers/utils' |
21 | import { VideoModel } from '../video/video' | |
22 | import { VideoRedundancyStrategy } from '../../../shared/models/redundancy' | |
23 | import { logger } from '../../helpers/logger' | |
4a08f669 | 24 | import { CacheFileObject, VideoPrivacy } from '../../../shared' |
c48e82b5 C |
25 | import { VideoChannelModel } from '../video/video-channel' |
26 | import { ServerModel } from '../server/server' | |
27 | import { sample } from 'lodash' | |
28 | import { isTestInstance } from '../../helpers/core-utils' | |
3f6b6a56 | 29 | import * as Bluebird from 'bluebird' |
3acc5084 | 30 | import { col, FindOptions, fn, literal, Op, Transaction } from 'sequelize' |
09209296 | 31 | import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' |
6dd9de95 | 32 | import { CONFIG } from '../../initializers/config' |
c48e82b5 C |
33 | |
34 | export enum ScopeNames { | |
35 | WITH_VIDEO = 'WITH_VIDEO' | |
36 | } | |
37 | ||
3acc5084 | 38 | @Scopes(() => ({ |
c48e82b5 C |
39 | [ ScopeNames.WITH_VIDEO ]: { |
40 | include: [ | |
41 | { | |
3acc5084 | 42 | model: VideoFileModel, |
09209296 C |
43 | required: false, |
44 | include: [ | |
45 | { | |
3acc5084 | 46 | model: VideoModel, |
09209296 C |
47 | required: true |
48 | } | |
49 | ] | |
50 | }, | |
51 | { | |
3acc5084 | 52 | model: VideoStreamingPlaylistModel, |
09209296 | 53 | required: false, |
c48e82b5 C |
54 | include: [ |
55 | { | |
3acc5084 | 56 | model: VideoModel, |
c48e82b5 C |
57 | required: true |
58 | } | |
59 | ] | |
60 | } | |
3acc5084 | 61 | ] |
c48e82b5 | 62 | } |
3acc5084 | 63 | })) |
c48e82b5 C |
64 | |
65 | @Table({ | |
66 | tableName: 'videoRedundancy', | |
67 | indexes: [ | |
68 | { | |
69 | fields: [ 'videoFileId' ] | |
70 | }, | |
71 | { | |
72 | fields: [ 'actorId' ] | |
73 | }, | |
74 | { | |
75 | fields: [ 'url' ], | |
76 | unique: true | |
77 | } | |
78 | ] | |
79 | }) | |
80 | export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |
81 | ||
82 | @CreatedAt | |
83 | createdAt: Date | |
84 | ||
85 | @UpdatedAt | |
86 | updatedAt: Date | |
87 | ||
88 | @AllowNull(false) | |
89 | @Column | |
90 | expiresOn: Date | |
91 | ||
92 | @AllowNull(false) | |
93 | @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl')) | |
94 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
95 | fileUrl: string | |
96 | ||
97 | @AllowNull(false) | |
98 | @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
99 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
100 | url: string | |
101 | ||
102 | @AllowNull(true) | |
103 | @Column | |
104 | strategy: string // Only used by us | |
105 | ||
106 | @ForeignKey(() => VideoFileModel) | |
107 | @Column | |
108 | videoFileId: number | |
109 | ||
110 | @BelongsTo(() => VideoFileModel, { | |
111 | foreignKey: { | |
09209296 | 112 | allowNull: true |
c48e82b5 C |
113 | }, |
114 | onDelete: 'cascade' | |
115 | }) | |
116 | VideoFile: VideoFileModel | |
117 | ||
09209296 C |
118 | @ForeignKey(() => VideoStreamingPlaylistModel) |
119 | @Column | |
120 | videoStreamingPlaylistId: number | |
121 | ||
122 | @BelongsTo(() => VideoStreamingPlaylistModel, { | |
123 | foreignKey: { | |
124 | allowNull: true | |
125 | }, | |
126 | onDelete: 'cascade' | |
127 | }) | |
128 | VideoStreamingPlaylist: VideoStreamingPlaylistModel | |
129 | ||
c48e82b5 C |
130 | @ForeignKey(() => ActorModel) |
131 | @Column | |
132 | actorId: number | |
133 | ||
134 | @BelongsTo(() => ActorModel, { | |
135 | foreignKey: { | |
136 | allowNull: false | |
137 | }, | |
138 | onDelete: 'cascade' | |
139 | }) | |
140 | Actor: ActorModel | |
141 | ||
25378bc8 C |
142 | @BeforeDestroy |
143 | static async removeFile (instance: VideoRedundancyModel) { | |
8d1fa36a | 144 | if (!instance.isOwned()) return |
c48e82b5 | 145 | |
09209296 C |
146 | if (instance.videoFileId) { |
147 | const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId) | |
c48e82b5 | 148 | |
09209296 C |
149 | const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}` |
150 | logger.info('Removing duplicated video file %s.', logIdentifier) | |
25378bc8 | 151 | |
09209296 C |
152 | videoFile.Video.removeFile(videoFile, true) |
153 | .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) | |
154 | } | |
155 | ||
156 | if (instance.videoStreamingPlaylistId) { | |
157 | const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId) | |
158 | ||
159 | const videoUUID = videoStreamingPlaylist.Video.uuid | |
160 | logger.info('Removing duplicated video streaming playlist %s.', videoUUID) | |
161 | ||
162 | videoStreamingPlaylist.Video.removeStreamingPlaylist(true) | |
163 | .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err })) | |
164 | } | |
d0ae9490 C |
165 | |
166 | return undefined | |
c48e82b5 C |
167 | } |
168 | ||
46f8d69b C |
169 | static async loadLocalByFileId (videoFileId: number) { |
170 | const actor = await getServerActor() | |
171 | ||
c48e82b5 C |
172 | const query = { |
173 | where: { | |
46f8d69b | 174 | actorId: actor.id, |
c48e82b5 C |
175 | videoFileId |
176 | } | |
177 | } | |
178 | ||
179 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
180 | } | |
181 | ||
09209296 C |
182 | static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number) { |
183 | const actor = await getServerActor() | |
184 | ||
185 | const query = { | |
186 | where: { | |
187 | actorId: actor.id, | |
188 | videoStreamingPlaylistId | |
189 | } | |
190 | } | |
191 | ||
192 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
193 | } | |
194 | ||
3acc5084 | 195 | static loadByUrl (url: string, transaction?: Transaction) { |
c48e82b5 C |
196 | const query = { |
197 | where: { | |
198 | url | |
e5565833 C |
199 | }, |
200 | transaction | |
c48e82b5 C |
201 | } |
202 | ||
203 | return VideoRedundancyModel.findOne(query) | |
204 | } | |
205 | ||
5ce1208a C |
206 | static async isLocalByVideoUUIDExists (uuid: string) { |
207 | const actor = await getServerActor() | |
208 | ||
209 | const query = { | |
210 | raw: true, | |
211 | attributes: [ 'id' ], | |
212 | where: { | |
213 | actorId: actor.id | |
214 | }, | |
215 | include: [ | |
216 | { | |
217 | attributes: [ ], | |
218 | model: VideoFileModel, | |
219 | required: true, | |
220 | include: [ | |
221 | { | |
222 | attributes: [ ], | |
223 | model: VideoModel, | |
224 | required: true, | |
225 | where: { | |
226 | uuid | |
227 | } | |
228 | } | |
229 | ] | |
230 | } | |
231 | ] | |
232 | } | |
233 | ||
234 | return VideoRedundancyModel.findOne(query) | |
235 | .then(r => !!r) | |
236 | } | |
237 | ||
3f6b6a56 C |
238 | static async getVideoSample (p: Bluebird<VideoModel[]>) { |
239 | const rows = await p | |
b36f41ca C |
240 | const ids = rows.map(r => r.id) |
241 | const id = sample(ids) | |
242 | ||
09209296 | 243 | return VideoModel.loadWithFiles(id, undefined, !isTestInstance()) |
b36f41ca C |
244 | } |
245 | ||
c48e82b5 C |
246 | static async findMostViewToDuplicate (randomizedFactor: number) { |
247 | // On VideoModel! | |
248 | const query = { | |
b36f41ca | 249 | attributes: [ 'id', 'views' ], |
c48e82b5 | 250 | limit: randomizedFactor, |
b36f41ca | 251 | order: getVideoSort('-views'), |
4a08f669 C |
252 | where: { |
253 | privacy: VideoPrivacy.PUBLIC | |
254 | }, | |
c48e82b5 | 255 | include: [ |
b36f41ca C |
256 | await VideoRedundancyModel.buildVideoFileForDuplication(), |
257 | VideoRedundancyModel.buildServerRedundancyInclude() | |
258 | ] | |
259 | } | |
260 | ||
3f6b6a56 | 261 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
b36f41ca C |
262 | } |
263 | ||
264 | static async findTrendingToDuplicate (randomizedFactor: number) { | |
265 | // On VideoModel! | |
266 | const query = { | |
267 | attributes: [ 'id', 'views' ], | |
268 | subQuery: false, | |
b36f41ca C |
269 | group: 'VideoModel.id', |
270 | limit: randomizedFactor, | |
271 | order: getVideoSort('-trending'), | |
4a08f669 C |
272 | where: { |
273 | privacy: VideoPrivacy.PUBLIC | |
274 | }, | |
b36f41ca C |
275 | include: [ |
276 | await VideoRedundancyModel.buildVideoFileForDuplication(), | |
277 | VideoRedundancyModel.buildServerRedundancyInclude(), | |
278 | ||
279 | VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS) | |
c48e82b5 C |
280 | ] |
281 | } | |
282 | ||
3f6b6a56 C |
283 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
284 | } | |
285 | ||
286 | static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) { | |
287 | // On VideoModel! | |
288 | const query = { | |
289 | attributes: [ 'id', 'publishedAt' ], | |
3f6b6a56 C |
290 | limit: randomizedFactor, |
291 | order: getVideoSort('-publishedAt'), | |
292 | where: { | |
4a08f669 | 293 | privacy: VideoPrivacy.PUBLIC, |
3f6b6a56 | 294 | views: { |
3acc5084 | 295 | [ Op.gte ]: minViews |
3f6b6a56 C |
296 | } |
297 | }, | |
298 | include: [ | |
299 | await VideoRedundancyModel.buildVideoFileForDuplication(), | |
300 | VideoRedundancyModel.buildServerRedundancyInclude() | |
301 | ] | |
302 | } | |
c48e82b5 | 303 | |
3f6b6a56 | 304 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
c48e82b5 C |
305 | } |
306 | ||
e5565833 C |
307 | static async loadOldestLocalThatAlreadyExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number) { |
308 | const expiredDate = new Date() | |
309 | expiredDate.setMilliseconds(expiredDate.getMilliseconds() - expiresAfterMs) | |
310 | ||
311 | const actor = await getServerActor() | |
312 | ||
313 | const query = { | |
314 | where: { | |
315 | actorId: actor.id, | |
316 | strategy, | |
317 | createdAt: { | |
3acc5084 | 318 | [ Op.lt ]: expiredDate |
e5565833 C |
319 | } |
320 | } | |
321 | } | |
322 | ||
323 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query) | |
324 | } | |
325 | ||
3f6b6a56 | 326 | static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { |
c48e82b5 | 327 | const actor = await getServerActor() |
0b353d1d C |
328 | const redundancyInclude = { |
329 | attributes: [], | |
330 | model: VideoRedundancyModel, | |
331 | required: true, | |
332 | where: { | |
333 | actorId: actor.id, | |
334 | strategy | |
335 | } | |
336 | } | |
337 | ||
338 | const queryFiles: FindOptions = { | |
339 | include: [ redundancyInclude ] | |
340 | } | |
c48e82b5 | 341 | |
0b353d1d | 342 | const queryStreamingPlaylists: FindOptions = { |
3f6b6a56 C |
343 | include: [ |
344 | { | |
345 | attributes: [], | |
0b353d1d | 346 | model: VideoModel.unscoped(), |
3f6b6a56 | 347 | required: true, |
0b353d1d C |
348 | include: [ |
349 | { | |
350 | attributes: [], | |
351 | model: VideoStreamingPlaylistModel.unscoped(), | |
352 | include: [ | |
353 | redundancyInclude | |
354 | ] | |
355 | } | |
356 | ] | |
3f6b6a56 C |
357 | } |
358 | ] | |
c48e82b5 C |
359 | } |
360 | ||
0b353d1d C |
361 | return Promise.all([ |
362 | VideoFileModel.aggregate('size', 'SUM', queryFiles), | |
363 | VideoFileModel.aggregate('size', 'SUM', queryStreamingPlaylists) | |
364 | ]).then(([ r1, r2 ]) => { | |
365 | return parseAggregateResult(r1) + parseAggregateResult(r2) | |
366 | }) | |
c48e82b5 C |
367 | } |
368 | ||
e5565833 C |
369 | static async listLocalExpired () { |
370 | const actor = await getServerActor() | |
371 | ||
372 | const query = { | |
373 | where: { | |
374 | actorId: actor.id, | |
375 | expiresOn: { | |
3acc5084 | 376 | [ Op.lt ]: new Date() |
e5565833 C |
377 | } |
378 | } | |
379 | } | |
380 | ||
381 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) | |
382 | } | |
383 | ||
384 | static async listRemoteExpired () { | |
385 | const actor = await getServerActor() | |
386 | ||
c48e82b5 | 387 | const query = { |
c48e82b5 | 388 | where: { |
e5565833 | 389 | actorId: { |
3acc5084 | 390 | [Op.ne]: actor.id |
e5565833 | 391 | }, |
c48e82b5 | 392 | expiresOn: { |
3acc5084 | 393 | [ Op.lt ]: new Date() |
c48e82b5 C |
394 | } |
395 | } | |
396 | } | |
397 | ||
e5565833 | 398 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) |
c48e82b5 C |
399 | } |
400 | ||
161b061d C |
401 | static async listLocalOfServer (serverId: number) { |
402 | const actor = await getServerActor() | |
09209296 C |
403 | const buildVideoInclude = () => ({ |
404 | model: VideoModel, | |
405 | required: true, | |
161b061d C |
406 | include: [ |
407 | { | |
09209296 C |
408 | attributes: [], |
409 | model: VideoChannelModel.unscoped(), | |
161b061d C |
410 | required: true, |
411 | include: [ | |
412 | { | |
09209296 C |
413 | attributes: [], |
414 | model: ActorModel.unscoped(), | |
161b061d | 415 | required: true, |
09209296 C |
416 | where: { |
417 | serverId | |
418 | } | |
161b061d C |
419 | } |
420 | ] | |
421 | } | |
422 | ] | |
09209296 C |
423 | }) |
424 | ||
425 | const query = { | |
426 | where: { | |
427 | actorId: actor.id | |
428 | }, | |
429 | include: [ | |
430 | { | |
431 | model: VideoFileModel, | |
432 | required: false, | |
433 | include: [ buildVideoInclude() ] | |
434 | }, | |
435 | { | |
436 | model: VideoStreamingPlaylistModel, | |
437 | required: false, | |
438 | include: [ buildVideoInclude() ] | |
439 | } | |
440 | ] | |
161b061d C |
441 | } |
442 | ||
443 | return VideoRedundancyModel.findAll(query) | |
444 | } | |
445 | ||
4b5384f6 C |
446 | static async getStats (strategy: VideoRedundancyStrategy) { |
447 | const actor = await getServerActor() | |
448 | ||
3acc5084 | 449 | const query: FindOptions = { |
4b5384f6 C |
450 | raw: true, |
451 | attributes: [ | |
3acc5084 C |
452 | [ fn('COALESCE', fn('SUM', col('VideoFile.size')), '0'), 'totalUsed' ], |
453 | [ fn('COUNT', fn('DISTINCT', col('videoId'))), 'totalVideos' ], | |
454 | [ fn('COUNT', col('videoFileId')), 'totalVideoFiles' ] | |
4b5384f6 C |
455 | ], |
456 | where: { | |
457 | strategy, | |
458 | actorId: actor.id | |
459 | }, | |
460 | include: [ | |
461 | { | |
462 | attributes: [], | |
463 | model: VideoFileModel, | |
464 | required: true | |
465 | } | |
466 | ] | |
467 | } | |
468 | ||
3acc5084 | 469 | return VideoRedundancyModel.findOne(query) |
4b5384f6 | 470 | .then((r: any) => ({ |
3acc5084 | 471 | totalUsed: parseAggregateResult(r.totalUsed), |
4b5384f6 C |
472 | totalVideos: r.totalVideos, |
473 | totalVideoFiles: r.totalVideoFiles | |
474 | })) | |
475 | } | |
476 | ||
09209296 C |
477 | getVideo () { |
478 | if (this.VideoFile) return this.VideoFile.Video | |
479 | ||
480 | return this.VideoStreamingPlaylist.Video | |
481 | } | |
482 | ||
8d1fa36a C |
483 | isOwned () { |
484 | return !!this.strategy | |
485 | } | |
486 | ||
c48e82b5 | 487 | toActivityPubObject (): CacheFileObject { |
09209296 C |
488 | if (this.VideoStreamingPlaylist) { |
489 | return { | |
490 | id: this.url, | |
491 | type: 'CacheFile' as 'CacheFile', | |
492 | object: this.VideoStreamingPlaylist.Video.url, | |
493 | expires: this.expiresOn.toISOString(), | |
494 | url: { | |
495 | type: 'Link', | |
496 | mimeType: 'application/x-mpegURL', | |
497 | mediaType: 'application/x-mpegURL', | |
498 | href: this.fileUrl | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
c48e82b5 C |
503 | return { |
504 | id: this.url, | |
505 | type: 'CacheFile' as 'CacheFile', | |
506 | object: this.VideoFile.Video.url, | |
507 | expires: this.expiresOn.toISOString(), | |
508 | url: { | |
509 | type: 'Link', | |
14e2014a C |
510 | mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ this.VideoFile.extname ] as any, |
511 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ this.VideoFile.extname ] as any, | |
c48e82b5 C |
512 | href: this.fileUrl, |
513 | height: this.VideoFile.resolution, | |
514 | size: this.VideoFile.size, | |
515 | fps: this.VideoFile.fps | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
b36f41ca C |
520 | // Don't include video files we already duplicated |
521 | private static async buildVideoFileForDuplication () { | |
c48e82b5 C |
522 | const actor = await getServerActor() |
523 | ||
3acc5084 | 524 | const notIn = literal( |
c48e82b5 | 525 | '(' + |
09209296 | 526 | `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` + |
c48e82b5 C |
527 | ')' |
528 | ) | |
b36f41ca C |
529 | |
530 | return { | |
531 | attributes: [], | |
532 | model: VideoFileModel.unscoped(), | |
533 | required: true, | |
534 | where: { | |
535 | id: { | |
3acc5084 | 536 | [ Op.notIn ]: notIn |
b36f41ca C |
537 | } |
538 | } | |
539 | } | |
540 | } | |
541 | ||
542 | private static buildServerRedundancyInclude () { | |
543 | return { | |
544 | attributes: [], | |
545 | model: VideoChannelModel.unscoped(), | |
546 | required: true, | |
547 | include: [ | |
548 | { | |
549 | attributes: [], | |
550 | model: ActorModel.unscoped(), | |
551 | required: true, | |
552 | include: [ | |
553 | { | |
554 | attributes: [], | |
555 | model: ServerModel.unscoped(), | |
556 | required: true, | |
557 | where: { | |
558 | redundancyAllowed: true | |
559 | } | |
560 | } | |
561 | ] | |
562 | } | |
563 | ] | |
564 | } | |
c48e82b5 C |
565 | } |
566 | } |