]>
Commit | Line | Data |
---|---|---|
b49f22d8 | 1 | import { sample } from 'lodash' |
6949a1a1 | 2 | import { FindOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' |
c48e82b5 | 3 | import { |
c48e82b5 | 4 | AllowNull, |
25378bc8 | 5 | BeforeDestroy, |
c48e82b5 C |
6 | BelongsTo, |
7 | Column, | |
8 | CreatedAt, | |
9 | DataType, | |
10 | ForeignKey, | |
11 | Is, | |
12 | Model, | |
13 | Scopes, | |
c48e82b5 C |
14 | Table, |
15 | UpdatedAt | |
16 | } from 'sequelize-typescript' | |
b49f22d8 | 17 | import { getServerActor } from '@server/models/application/application' |
7448551f | 18 | import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models' |
b764380a C |
19 | import { VideoRedundanciesTarget } from '@shared/models/redundancy/video-redundancies-filters.model' |
20 | import { | |
21 | FileRedundancyInformation, | |
22 | StreamingPlaylistRedundancyInformation, | |
23 | VideoRedundancy | |
24 | } from '@shared/models/redundancy/video-redundancy.model' | |
b49f22d8 C |
25 | import { CacheFileObject, VideoPrivacy } from '../../../shared' |
26 | import { VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../shared/models/redundancy' | |
27 | import { isTestInstance } from '../../helpers/core-utils' | |
28 | import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
29 | import { logger } from '../../helpers/logger' | |
30 | import { CONFIG } from '../../initializers/config' | |
31 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' | |
32 | import { ActorModel } from '../activitypub/actor' | |
33 | import { ServerModel } from '../server/server' | |
34 | import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' | |
93544419 | 35 | import { ScheduleVideoUpdateModel } from '../video/schedule-video-update' |
b49f22d8 C |
36 | import { VideoModel } from '../video/video' |
37 | import { VideoChannelModel } from '../video/video-channel' | |
38 | import { VideoFileModel } from '../video/video-file' | |
39 | import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' | |
c48e82b5 C |
40 | |
41 | export enum ScopeNames { | |
42 | WITH_VIDEO = 'WITH_VIDEO' | |
43 | } | |
44 | ||
3acc5084 | 45 | @Scopes(() => ({ |
a1587156 | 46 | [ScopeNames.WITH_VIDEO]: { |
c48e82b5 C |
47 | include: [ |
48 | { | |
3acc5084 | 49 | model: VideoFileModel, |
09209296 C |
50 | required: false, |
51 | include: [ | |
52 | { | |
3acc5084 | 53 | model: VideoModel, |
09209296 C |
54 | required: true |
55 | } | |
56 | ] | |
57 | }, | |
58 | { | |
3acc5084 | 59 | model: VideoStreamingPlaylistModel, |
09209296 | 60 | required: false, |
c48e82b5 C |
61 | include: [ |
62 | { | |
3acc5084 | 63 | model: VideoModel, |
c48e82b5 C |
64 | required: true |
65 | } | |
66 | ] | |
67 | } | |
3acc5084 | 68 | ] |
c48e82b5 | 69 | } |
3acc5084 | 70 | })) |
c48e82b5 C |
71 | |
72 | @Table({ | |
73 | tableName: 'videoRedundancy', | |
74 | indexes: [ | |
75 | { | |
76 | fields: [ 'videoFileId' ] | |
77 | }, | |
78 | { | |
79 | fields: [ 'actorId' ] | |
80 | }, | |
81 | { | |
82 | fields: [ 'url' ], | |
83 | unique: true | |
84 | } | |
85 | ] | |
86 | }) | |
b49f22d8 | 87 | export class VideoRedundancyModel extends Model { |
c48e82b5 C |
88 | |
89 | @CreatedAt | |
90 | createdAt: Date | |
91 | ||
92 | @UpdatedAt | |
93 | updatedAt: Date | |
94 | ||
b764380a | 95 | @AllowNull(true) |
c48e82b5 C |
96 | @Column |
97 | expiresOn: Date | |
98 | ||
99 | @AllowNull(false) | |
100 | @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl')) | |
101 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
102 | fileUrl: string | |
103 | ||
104 | @AllowNull(false) | |
105 | @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
106 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
107 | url: string | |
108 | ||
109 | @AllowNull(true) | |
110 | @Column | |
111 | strategy: string // Only used by us | |
112 | ||
113 | @ForeignKey(() => VideoFileModel) | |
114 | @Column | |
115 | videoFileId: number | |
116 | ||
117 | @BelongsTo(() => VideoFileModel, { | |
118 | foreignKey: { | |
09209296 | 119 | allowNull: true |
c48e82b5 C |
120 | }, |
121 | onDelete: 'cascade' | |
122 | }) | |
123 | VideoFile: VideoFileModel | |
124 | ||
09209296 C |
125 | @ForeignKey(() => VideoStreamingPlaylistModel) |
126 | @Column | |
127 | videoStreamingPlaylistId: number | |
128 | ||
129 | @BelongsTo(() => VideoStreamingPlaylistModel, { | |
130 | foreignKey: { | |
131 | allowNull: true | |
132 | }, | |
133 | onDelete: 'cascade' | |
134 | }) | |
135 | VideoStreamingPlaylist: VideoStreamingPlaylistModel | |
136 | ||
c48e82b5 C |
137 | @ForeignKey(() => ActorModel) |
138 | @Column | |
139 | actorId: number | |
140 | ||
141 | @BelongsTo(() => ActorModel, { | |
142 | foreignKey: { | |
143 | allowNull: false | |
144 | }, | |
145 | onDelete: 'cascade' | |
146 | }) | |
147 | Actor: ActorModel | |
148 | ||
25378bc8 C |
149 | @BeforeDestroy |
150 | static async removeFile (instance: VideoRedundancyModel) { | |
8d1fa36a | 151 | if (!instance.isOwned()) return |
c48e82b5 | 152 | |
09209296 C |
153 | if (instance.videoFileId) { |
154 | const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId) | |
c48e82b5 | 155 | |
09209296 C |
156 | const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}` |
157 | logger.info('Removing duplicated video file %s.', logIdentifier) | |
25378bc8 | 158 | |
09209296 C |
159 | videoFile.Video.removeFile(videoFile, true) |
160 | .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) | |
161 | } | |
162 | ||
163 | if (instance.videoStreamingPlaylistId) { | |
164 | const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId) | |
165 | ||
166 | const videoUUID = videoStreamingPlaylist.Video.uuid | |
167 | logger.info('Removing duplicated video streaming playlist %s.', videoUUID) | |
168 | ||
ffc65cbd | 169 | videoStreamingPlaylist.Video.removeStreamingPlaylistFiles(videoStreamingPlaylist, true) |
a1587156 | 170 | .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err })) |
09209296 | 171 | } |
d0ae9490 C |
172 | |
173 | return undefined | |
c48e82b5 C |
174 | } |
175 | ||
453e83ea | 176 | static async loadLocalByFileId (videoFileId: number): Promise<MVideoRedundancyVideo> { |
46f8d69b C |
177 | const actor = await getServerActor() |
178 | ||
c48e82b5 C |
179 | const query = { |
180 | where: { | |
46f8d69b | 181 | actorId: actor.id, |
c48e82b5 C |
182 | videoFileId |
183 | } | |
184 | } | |
185 | ||
186 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
187 | } | |
188 | ||
89613cb4 C |
189 | static async listLocalByVideoId (videoId: number): Promise<MVideoRedundancyVideo[]> { |
190 | const actor = await getServerActor() | |
191 | ||
192 | const queryStreamingPlaylist = { | |
193 | where: { | |
194 | actorId: actor.id | |
195 | }, | |
196 | include: [ | |
197 | { | |
198 | model: VideoStreamingPlaylistModel.unscoped(), | |
199 | required: true, | |
200 | include: [ | |
201 | { | |
202 | model: VideoModel.unscoped(), | |
203 | required: true, | |
204 | where: { | |
205 | id: videoId | |
206 | } | |
207 | } | |
208 | ] | |
209 | } | |
210 | ] | |
211 | } | |
212 | ||
213 | const queryFiles = { | |
214 | where: { | |
215 | actorId: actor.id | |
216 | }, | |
217 | include: [ | |
218 | { | |
219 | model: VideoFileModel, | |
220 | required: true, | |
221 | include: [ | |
222 | { | |
223 | model: VideoModel, | |
224 | required: true, | |
225 | where: { | |
226 | id: videoId | |
227 | } | |
228 | } | |
229 | ] | |
230 | } | |
231 | ] | |
232 | } | |
233 | ||
234 | return Promise.all([ | |
235 | VideoRedundancyModel.findAll(queryStreamingPlaylist), | |
236 | VideoRedundancyModel.findAll(queryFiles) | |
237 | ]).then(([ r1, r2 ]) => r1.concat(r2)) | |
238 | } | |
239 | ||
453e83ea | 240 | static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo> { |
09209296 C |
241 | const actor = await getServerActor() |
242 | ||
243 | const query = { | |
244 | where: { | |
245 | actorId: actor.id, | |
246 | videoStreamingPlaylistId | |
247 | } | |
248 | } | |
249 | ||
250 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
251 | } | |
252 | ||
b49f22d8 | 253 | static loadByIdWithVideo (id: number, transaction?: Transaction): Promise<MVideoRedundancyVideo> { |
b764380a C |
254 | const query = { |
255 | where: { id }, | |
256 | transaction | |
257 | } | |
258 | ||
259 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
260 | } | |
261 | ||
b49f22d8 | 262 | static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoRedundancy> { |
c48e82b5 C |
263 | const query = { |
264 | where: { | |
265 | url | |
e5565833 C |
266 | }, |
267 | transaction | |
c48e82b5 C |
268 | } |
269 | ||
270 | return VideoRedundancyModel.findOne(query) | |
271 | } | |
272 | ||
5ce1208a C |
273 | static async isLocalByVideoUUIDExists (uuid: string) { |
274 | const actor = await getServerActor() | |
275 | ||
276 | const query = { | |
277 | raw: true, | |
278 | attributes: [ 'id' ], | |
279 | where: { | |
280 | actorId: actor.id | |
281 | }, | |
282 | include: [ | |
283 | { | |
a1587156 | 284 | attributes: [], |
5ce1208a C |
285 | model: VideoFileModel, |
286 | required: true, | |
287 | include: [ | |
288 | { | |
a1587156 | 289 | attributes: [], |
5ce1208a C |
290 | model: VideoModel, |
291 | required: true, | |
292 | where: { | |
293 | uuid | |
294 | } | |
295 | } | |
296 | ] | |
297 | } | |
298 | ] | |
299 | } | |
300 | ||
301 | return VideoRedundancyModel.findOne(query) | |
a1587156 | 302 | .then(r => !!r) |
5ce1208a C |
303 | } |
304 | ||
b49f22d8 | 305 | static async getVideoSample (p: Promise<VideoModel[]>) { |
3f6b6a56 | 306 | const rows = await p |
9e3e3617 C |
307 | if (rows.length === 0) return undefined |
308 | ||
b36f41ca C |
309 | const ids = rows.map(r => r.id) |
310 | const id = sample(ids) | |
311 | ||
09209296 | 312 | return VideoModel.loadWithFiles(id, undefined, !isTestInstance()) |
b36f41ca C |
313 | } |
314 | ||
c48e82b5 | 315 | static async findMostViewToDuplicate (randomizedFactor: number) { |
7448551f C |
316 | const peertubeActor = await getServerActor() |
317 | ||
c48e82b5 C |
318 | // On VideoModel! |
319 | const query = { | |
b36f41ca | 320 | attributes: [ 'id', 'views' ], |
c48e82b5 | 321 | limit: randomizedFactor, |
b36f41ca | 322 | order: getVideoSort('-views'), |
4a08f669 | 323 | where: { |
9e2b2e76 | 324 | privacy: VideoPrivacy.PUBLIC, |
7448551f C |
325 | isLive: false, |
326 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
4a08f669 | 327 | }, |
c48e82b5 | 328 | include: [ |
b36f41ca C |
329 | VideoRedundancyModel.buildServerRedundancyInclude() |
330 | ] | |
331 | } | |
332 | ||
3f6b6a56 | 333 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
b36f41ca C |
334 | } |
335 | ||
336 | static async findTrendingToDuplicate (randomizedFactor: number) { | |
7448551f C |
337 | const peertubeActor = await getServerActor() |
338 | ||
b36f41ca C |
339 | // On VideoModel! |
340 | const query = { | |
341 | attributes: [ 'id', 'views' ], | |
342 | subQuery: false, | |
b36f41ca C |
343 | group: 'VideoModel.id', |
344 | limit: randomizedFactor, | |
345 | order: getVideoSort('-trending'), | |
4a08f669 | 346 | where: { |
9e2b2e76 | 347 | privacy: VideoPrivacy.PUBLIC, |
7448551f C |
348 | isLive: false, |
349 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
4a08f669 | 350 | }, |
b36f41ca | 351 | include: [ |
b36f41ca C |
352 | VideoRedundancyModel.buildServerRedundancyInclude(), |
353 | ||
354 | VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS) | |
c48e82b5 C |
355 | ] |
356 | } | |
357 | ||
3f6b6a56 C |
358 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
359 | } | |
360 | ||
361 | static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) { | |
7448551f C |
362 | const peertubeActor = await getServerActor() |
363 | ||
3f6b6a56 C |
364 | // On VideoModel! |
365 | const query = { | |
366 | attributes: [ 'id', 'publishedAt' ], | |
3f6b6a56 C |
367 | limit: randomizedFactor, |
368 | order: getVideoSort('-publishedAt'), | |
369 | where: { | |
4a08f669 | 370 | privacy: VideoPrivacy.PUBLIC, |
9e2b2e76 | 371 | isLive: false, |
3f6b6a56 | 372 | views: { |
a1587156 | 373 | [Op.gte]: minViews |
7448551f C |
374 | }, |
375 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
3f6b6a56 C |
376 | }, |
377 | include: [ | |
93544419 C |
378 | VideoRedundancyModel.buildServerRedundancyInclude(), |
379 | ||
380 | // Required by publishedAt sort | |
381 | { | |
382 | model: ScheduleVideoUpdateModel.unscoped(), | |
383 | required: false | |
384 | } | |
3f6b6a56 C |
385 | ] |
386 | } | |
c48e82b5 | 387 | |
3f6b6a56 | 388 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
c48e82b5 C |
389 | } |
390 | ||
453e83ea | 391 | static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise<MVideoRedundancyVideo> { |
e5565833 C |
392 | const expiredDate = new Date() |
393 | expiredDate.setMilliseconds(expiredDate.getMilliseconds() - expiresAfterMs) | |
394 | ||
395 | const actor = await getServerActor() | |
396 | ||
397 | const query = { | |
398 | where: { | |
399 | actorId: actor.id, | |
400 | strategy, | |
401 | createdAt: { | |
a1587156 | 402 | [Op.lt]: expiredDate |
e5565833 C |
403 | } |
404 | } | |
405 | } | |
406 | ||
407 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query) | |
408 | } | |
409 | ||
e5565833 C |
410 | static async listLocalExpired () { |
411 | const actor = await getServerActor() | |
412 | ||
413 | const query = { | |
414 | where: { | |
415 | actorId: actor.id, | |
416 | expiresOn: { | |
a1587156 | 417 | [Op.lt]: new Date() |
e5565833 C |
418 | } |
419 | } | |
420 | } | |
421 | ||
422 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) | |
423 | } | |
424 | ||
425 | static async listRemoteExpired () { | |
426 | const actor = await getServerActor() | |
427 | ||
c48e82b5 | 428 | const query = { |
c48e82b5 | 429 | where: { |
e5565833 | 430 | actorId: { |
3acc5084 | 431 | [Op.ne]: actor.id |
e5565833 | 432 | }, |
c48e82b5 | 433 | expiresOn: { |
a1587156 C |
434 | [Op.lt]: new Date(), |
435 | [Op.ne]: null | |
c48e82b5 C |
436 | } |
437 | } | |
438 | } | |
439 | ||
e5565833 | 440 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) |
c48e82b5 C |
441 | } |
442 | ||
161b061d C |
443 | static async listLocalOfServer (serverId: number) { |
444 | const actor = await getServerActor() | |
09209296 C |
445 | const buildVideoInclude = () => ({ |
446 | model: VideoModel, | |
447 | required: true, | |
161b061d C |
448 | include: [ |
449 | { | |
09209296 C |
450 | attributes: [], |
451 | model: VideoChannelModel.unscoped(), | |
161b061d C |
452 | required: true, |
453 | include: [ | |
454 | { | |
09209296 C |
455 | attributes: [], |
456 | model: ActorModel.unscoped(), | |
161b061d | 457 | required: true, |
09209296 C |
458 | where: { |
459 | serverId | |
460 | } | |
161b061d C |
461 | } |
462 | ] | |
463 | } | |
464 | ] | |
09209296 C |
465 | }) |
466 | ||
467 | const query = { | |
468 | where: { | |
469 | actorId: actor.id | |
470 | }, | |
471 | include: [ | |
472 | { | |
473 | model: VideoFileModel, | |
474 | required: false, | |
475 | include: [ buildVideoInclude() ] | |
476 | }, | |
477 | { | |
478 | model: VideoStreamingPlaylistModel, | |
479 | required: false, | |
480 | include: [ buildVideoInclude() ] | |
481 | } | |
482 | ] | |
161b061d C |
483 | } |
484 | ||
485 | return VideoRedundancyModel.findAll(query) | |
486 | } | |
487 | ||
b764380a | 488 | static listForApi (options: { |
a1587156 C |
489 | start: number |
490 | count: number | |
491 | sort: string | |
492 | target: VideoRedundanciesTarget | |
b764380a C |
493 | strategy?: string |
494 | }) { | |
495 | const { start, count, sort, target, strategy } = options | |
a1587156 C |
496 | const redundancyWhere: WhereOptions = {} |
497 | const videosWhere: WhereOptions = {} | |
b764380a C |
498 | let redundancySqlSuffix = '' |
499 | ||
500 | if (target === 'my-videos') { | |
501 | Object.assign(videosWhere, { remote: false }) | |
502 | } else if (target === 'remote-videos') { | |
503 | Object.assign(videosWhere, { remote: true }) | |
504 | Object.assign(redundancyWhere, { strategy: { [Op.ne]: null } }) | |
505 | redundancySqlSuffix = ' AND "videoRedundancy"."strategy" IS NOT NULL' | |
506 | } | |
507 | ||
508 | if (strategy) { | |
509 | Object.assign(redundancyWhere, { strategy: strategy }) | |
510 | } | |
511 | ||
512 | const videoFilterWhere = { | |
513 | [Op.and]: [ | |
514 | { | |
a1587156 | 515 | [Op.or]: [ |
b764380a C |
516 | { |
517 | id: { | |
a1587156 | 518 | [Op.in]: literal( |
b764380a C |
519 | '(' + |
520 | 'SELECT "videoId" FROM "videoFile" ' + | |
521 | 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoFileId" = "videoFile".id' + | |
522 | redundancySqlSuffix + | |
523 | ')' | |
524 | ) | |
525 | } | |
526 | }, | |
527 | { | |
528 | id: { | |
a1587156 | 529 | [Op.in]: literal( |
b764380a C |
530 | '(' + |
531 | 'select "videoId" FROM "videoStreamingPlaylist" ' + | |
532 | 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' + | |
533 | redundancySqlSuffix + | |
534 | ')' | |
535 | ) | |
536 | } | |
537 | } | |
538 | ] | |
539 | }, | |
540 | ||
541 | videosWhere | |
542 | ] | |
543 | } | |
544 | ||
545 | // /!\ On video model /!\ | |
546 | const findOptions = { | |
547 | offset: start, | |
548 | limit: count, | |
549 | order: getSort(sort), | |
550 | include: [ | |
551 | { | |
552 | required: false, | |
8319d6ae | 553 | model: VideoFileModel, |
b764380a C |
554 | include: [ |
555 | { | |
556 | model: VideoRedundancyModel.unscoped(), | |
557 | required: false, | |
558 | where: redundancyWhere | |
559 | } | |
560 | ] | |
561 | }, | |
562 | { | |
563 | required: false, | |
564 | model: VideoStreamingPlaylistModel.unscoped(), | |
565 | include: [ | |
566 | { | |
567 | model: VideoRedundancyModel.unscoped(), | |
568 | required: false, | |
569 | where: redundancyWhere | |
570 | }, | |
571 | { | |
8319d6ae | 572 | model: VideoFileModel, |
b764380a C |
573 | required: false |
574 | } | |
575 | ] | |
576 | } | |
577 | ], | |
578 | where: videoFilterWhere | |
579 | } | |
580 | ||
581 | // /!\ On video model /!\ | |
582 | const countOptions = { | |
583 | where: videoFilterWhere | |
584 | } | |
585 | ||
586 | return Promise.all([ | |
587 | VideoModel.findAll(findOptions), | |
588 | ||
589 | VideoModel.count(countOptions) | |
590 | ]).then(([ data, total ]) => ({ total, data })) | |
591 | } | |
592 | ||
593 | static async getStats (strategy: VideoRedundancyStrategyWithManual) { | |
4b5384f6 C |
594 | const actor = await getServerActor() |
595 | ||
7448551f C |
596 | const sql = `WITH "tmp" AS ` + |
597 | `(` + | |
598 | `SELECT "videoFile"."size" AS "videoFileSize", "videoStreamingFile"."size" AS "videoStreamingFileSize", ` + | |
599 | `"videoFile"."videoId" AS "videoFileVideoId", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` + | |
600 | `FROM "videoRedundancy" AS "videoRedundancy" ` + | |
601 | `LEFT JOIN "videoFile" AS "videoFile" ON "videoRedundancy"."videoFileId" = "videoFile"."id" ` + | |
602 | `LEFT JOIN "videoStreamingPlaylist" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ` + | |
603 | `LEFT JOIN "videoFile" AS "videoStreamingFile" ` + | |
604 | `ON "videoStreamingPlaylist"."id" = "videoStreamingFile"."videoStreamingPlaylistId" ` + | |
605 | `WHERE "videoRedundancy"."strategy" = :strategy AND "videoRedundancy"."actorId" = :actorId` + | |
606 | `), ` + | |
607 | `"videoIds" AS (` + | |
608 | `SELECT "videoFileVideoId" AS "videoId" FROM "tmp" ` + | |
609 | `UNION SELECT "videoStreamingVideoId" AS "videoId" FROM "tmp" ` + | |
610 | `) ` + | |
611 | `SELECT ` + | |
612 | `COALESCE(SUM("videoFileSize"), '0') + COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` + | |
613 | `(SELECT COUNT("videoIds"."videoId") FROM "videoIds") AS "totalVideos", ` + | |
614 | `COUNT(*) AS "totalVideoFiles" ` + | |
615 | `FROM "tmp"` | |
616 | ||
617 | return VideoRedundancyModel.sequelize.query<any>(sql, { | |
618 | replacements: { strategy, actorId: actor.id }, | |
619 | type: QueryTypes.SELECT | |
620 | }).then(([ row ]) => ({ | |
621 | totalUsed: parseAggregateResult(row.totalUsed), | |
622 | totalVideos: row.totalVideos, | |
623 | totalVideoFiles: row.totalVideoFiles | |
624 | })) | |
4b5384f6 C |
625 | } |
626 | ||
b764380a | 627 | static toFormattedJSONStatic (video: MVideoForRedundancyAPI): VideoRedundancy { |
a1587156 C |
628 | const filesRedundancies: FileRedundancyInformation[] = [] |
629 | const streamingPlaylistsRedundancies: StreamingPlaylistRedundancyInformation[] = [] | |
b764380a C |
630 | |
631 | for (const file of video.VideoFiles) { | |
632 | for (const redundancy of file.RedundancyVideos) { | |
633 | filesRedundancies.push({ | |
634 | id: redundancy.id, | |
635 | fileUrl: redundancy.fileUrl, | |
636 | strategy: redundancy.strategy, | |
637 | createdAt: redundancy.createdAt, | |
638 | updatedAt: redundancy.updatedAt, | |
639 | expiresOn: redundancy.expiresOn, | |
640 | size: file.size | |
641 | }) | |
642 | } | |
643 | } | |
644 | ||
645 | for (const playlist of video.VideoStreamingPlaylists) { | |
646 | const size = playlist.VideoFiles.reduce((a, b) => a + b.size, 0) | |
647 | ||
648 | for (const redundancy of playlist.RedundancyVideos) { | |
649 | streamingPlaylistsRedundancies.push({ | |
650 | id: redundancy.id, | |
651 | fileUrl: redundancy.fileUrl, | |
652 | strategy: redundancy.strategy, | |
653 | createdAt: redundancy.createdAt, | |
654 | updatedAt: redundancy.updatedAt, | |
655 | expiresOn: redundancy.expiresOn, | |
656 | size | |
657 | }) | |
658 | } | |
659 | } | |
660 | ||
661 | return { | |
662 | id: video.id, | |
663 | name: video.name, | |
664 | url: video.url, | |
665 | uuid: video.uuid, | |
666 | ||
667 | redundancies: { | |
668 | files: filesRedundancies, | |
669 | streamingPlaylists: streamingPlaylistsRedundancies | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
09209296 | 674 | getVideo () { |
2e1e4af0 | 675 | if (this.VideoFile?.Video) return this.VideoFile.Video |
09209296 | 676 | |
2e1e4af0 | 677 | if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video |
9cfeb3cf C |
678 | |
679 | return undefined | |
09209296 C |
680 | } |
681 | ||
8d1fa36a C |
682 | isOwned () { |
683 | return !!this.strategy | |
684 | } | |
685 | ||
b5fecbf4 | 686 | toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject { |
09209296 C |
687 | if (this.VideoStreamingPlaylist) { |
688 | return { | |
689 | id: this.url, | |
690 | type: 'CacheFile' as 'CacheFile', | |
691 | object: this.VideoStreamingPlaylist.Video.url, | |
b764380a | 692 | expires: this.expiresOn ? this.expiresOn.toISOString() : null, |
09209296 C |
693 | url: { |
694 | type: 'Link', | |
09209296 C |
695 | mediaType: 'application/x-mpegURL', |
696 | href: this.fileUrl | |
697 | } | |
698 | } | |
699 | } | |
700 | ||
c48e82b5 C |
701 | return { |
702 | id: this.url, | |
703 | type: 'CacheFile' as 'CacheFile', | |
704 | object: this.VideoFile.Video.url, | |
b764380a | 705 | expires: this.expiresOn ? this.expiresOn.toISOString() : null, |
c48e82b5 C |
706 | url: { |
707 | type: 'Link', | |
a1587156 | 708 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[this.VideoFile.extname] as any, |
c48e82b5 C |
709 | href: this.fileUrl, |
710 | height: this.VideoFile.resolution, | |
711 | size: this.VideoFile.size, | |
712 | fps: this.VideoFile.fps | |
713 | } | |
714 | } | |
715 | } | |
716 | ||
b36f41ca | 717 | // Don't include video files we already duplicated |
7448551f | 718 | private static buildVideoIdsForDuplication (peertubeActor: MActor) { |
3acc5084 | 719 | const notIn = literal( |
c48e82b5 | 720 | '(' + |
7448551f C |
721 | `SELECT "videoFile"."videoId" AS "videoId" FROM "videoRedundancy" ` + |
722 | `INNER JOIN "videoFile" ON "videoFile"."id" = "videoRedundancy"."videoFileId" ` + | |
723 | `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` + | |
724 | `UNION ` + | |
725 | `SELECT "videoStreamingPlaylist"."videoId" AS "videoId" FROM "videoRedundancy" ` + | |
726 | `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoRedundancy"."videoStreamingPlaylistId" ` + | |
727 | `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` + | |
c48e82b5 C |
728 | ')' |
729 | ) | |
b36f41ca C |
730 | |
731 | return { | |
7448551f C |
732 | id: { |
733 | [Op.notIn]: notIn | |
b36f41ca C |
734 | } |
735 | } | |
736 | } | |
737 | ||
738 | private static buildServerRedundancyInclude () { | |
739 | return { | |
740 | attributes: [], | |
741 | model: VideoChannelModel.unscoped(), | |
742 | required: true, | |
743 | include: [ | |
744 | { | |
745 | attributes: [], | |
746 | model: ActorModel.unscoped(), | |
747 | required: true, | |
748 | include: [ | |
749 | { | |
750 | attributes: [], | |
751 | model: ServerModel.unscoped(), | |
752 | required: true, | |
753 | where: { | |
754 | redundancyAllowed: true | |
755 | } | |
756 | } | |
757 | ] | |
758 | } | |
759 | ] | |
760 | } | |
c48e82b5 C |
761 | } |
762 | } |