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