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