]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/redundancy/video-redundancy.ts
Try to fix bad timestamps in .srt
[github/Chocobozzz/PeerTube.git] / server / models / redundancy / video-redundancy.ts
1 import { sample } from 'lodash'
2 import { FindOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
3 import {
4 AllowNull,
5 BeforeDestroy,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 ForeignKey,
11 Is,
12 Model,
13 Scopes,
14 Table,
15 UpdatedAt
16 } from 'sequelize-typescript'
17 import { getServerActor } from '@server/models/application/application'
18 import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models'
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'
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'
35 import { ScheduleVideoUpdateModel } from '../video/schedule-video-update'
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'
40
41 export enum ScopeNames {
42 WITH_VIDEO = 'WITH_VIDEO'
43 }
44
45 @Scopes(() => ({
46 [ScopeNames.WITH_VIDEO]: {
47 include: [
48 {
49 model: VideoFileModel,
50 required: false,
51 include: [
52 {
53 model: VideoModel,
54 required: true
55 }
56 ]
57 },
58 {
59 model: VideoStreamingPlaylistModel,
60 required: false,
61 include: [
62 {
63 model: VideoModel,
64 required: true
65 }
66 ]
67 }
68 ]
69 }
70 }))
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 })
87 export class VideoRedundancyModel extends Model {
88
89 @CreatedAt
90 createdAt: Date
91
92 @UpdatedAt
93 updatedAt: Date
94
95 @AllowNull(true)
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: {
119 allowNull: true
120 },
121 onDelete: 'cascade'
122 })
123 VideoFile: VideoFileModel
124
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
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
149 @BeforeDestroy
150 static async removeFile (instance: VideoRedundancyModel) {
151 if (!instance.isOwned()) return
152
153 if (instance.videoFileId) {
154 const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId)
155
156 const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}`
157 logger.info('Removing duplicated video file %s.', logIdentifier)
158
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
169 videoStreamingPlaylist.Video.removeStreamingPlaylistFiles(videoStreamingPlaylist, true)
170 .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err }))
171 }
172
173 return undefined
174 }
175
176 static async loadLocalByFileId (videoFileId: number): Promise<MVideoRedundancyVideo> {
177 const actor = await getServerActor()
178
179 const query = {
180 where: {
181 actorId: actor.id,
182 videoFileId
183 }
184 }
185
186 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
187 }
188
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
240 static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo> {
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
253 static loadByIdWithVideo (id: number, transaction?: Transaction): Promise<MVideoRedundancyVideo> {
254 const query = {
255 where: { id },
256 transaction
257 }
258
259 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
260 }
261
262 static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoRedundancy> {
263 const query = {
264 where: {
265 url
266 },
267 transaction
268 }
269
270 return VideoRedundancyModel.findOne(query)
271 }
272
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 {
284 attributes: [],
285 model: VideoFileModel,
286 required: true,
287 include: [
288 {
289 attributes: [],
290 model: VideoModel,
291 required: true,
292 where: {
293 uuid
294 }
295 }
296 ]
297 }
298 ]
299 }
300
301 return VideoRedundancyModel.findOne(query)
302 .then(r => !!r)
303 }
304
305 static async getVideoSample (p: Promise<VideoModel[]>) {
306 const rows = await p
307 if (rows.length === 0) return undefined
308
309 const ids = rows.map(r => r.id)
310 const id = sample(ids)
311
312 return VideoModel.loadWithFiles(id, undefined, !isTestInstance())
313 }
314
315 static async findMostViewToDuplicate (randomizedFactor: number) {
316 const peertubeActor = await getServerActor()
317
318 // On VideoModel!
319 const query = {
320 attributes: [ 'id', 'views' ],
321 limit: randomizedFactor,
322 order: getVideoSort('-views'),
323 where: {
324 privacy: VideoPrivacy.PUBLIC,
325 isLive: false,
326 ...this.buildVideoIdsForDuplication(peertubeActor)
327 },
328 include: [
329 VideoRedundancyModel.buildServerRedundancyInclude()
330 ]
331 }
332
333 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
334 }
335
336 static async findTrendingToDuplicate (randomizedFactor: number) {
337 const peertubeActor = await getServerActor()
338
339 // On VideoModel!
340 const query = {
341 attributes: [ 'id', 'views' ],
342 subQuery: false,
343 group: 'VideoModel.id',
344 limit: randomizedFactor,
345 order: getVideoSort('-trending'),
346 where: {
347 privacy: VideoPrivacy.PUBLIC,
348 isLive: false,
349 ...this.buildVideoIdsForDuplication(peertubeActor)
350 },
351 include: [
352 VideoRedundancyModel.buildServerRedundancyInclude(),
353
354 VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS)
355 ]
356 }
357
358 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
359 }
360
361 static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) {
362 const peertubeActor = await getServerActor()
363
364 // On VideoModel!
365 const query = {
366 attributes: [ 'id', 'publishedAt' ],
367 limit: randomizedFactor,
368 order: getVideoSort('-publishedAt'),
369 where: {
370 privacy: VideoPrivacy.PUBLIC,
371 isLive: false,
372 views: {
373 [Op.gte]: minViews
374 },
375 ...this.buildVideoIdsForDuplication(peertubeActor)
376 },
377 include: [
378 VideoRedundancyModel.buildServerRedundancyInclude(),
379
380 // Required by publishedAt sort
381 {
382 model: ScheduleVideoUpdateModel.unscoped(),
383 required: false
384 }
385 ]
386 }
387
388 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
389 }
390
391 static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise<MVideoRedundancyVideo> {
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: {
402 [Op.lt]: expiredDate
403 }
404 }
405 }
406
407 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query)
408 }
409
410 static async getTotalDuplicated (strategy: VideoRedundancyStrategy) {
411 const actor = await getServerActor()
412 const redundancyInclude = {
413 attributes: [],
414 model: VideoRedundancyModel,
415 required: true,
416 where: {
417 actorId: actor.id,
418 strategy
419 }
420 }
421
422 const queryFiles: FindOptions = {
423 include: [ redundancyInclude ]
424 }
425
426 const queryStreamingPlaylists: FindOptions = {
427 include: [
428 {
429 attributes: [],
430 model: VideoModel.unscoped(),
431 required: true,
432 include: [
433 {
434 required: true,
435 attributes: [],
436 model: VideoStreamingPlaylistModel.unscoped(),
437 include: [
438 redundancyInclude
439 ]
440 }
441 ]
442 }
443 ]
444 }
445
446 return Promise.all([
447 VideoFileModel.aggregate('size', 'SUM', queryFiles),
448 VideoFileModel.aggregate('size', 'SUM', queryStreamingPlaylists)
449 ]).then(([ r1, r2 ]) => {
450 return parseAggregateResult(r1) + parseAggregateResult(r2)
451 })
452 }
453
454 static async listLocalExpired () {
455 const actor = await getServerActor()
456
457 const query = {
458 where: {
459 actorId: actor.id,
460 expiresOn: {
461 [Op.lt]: new Date()
462 }
463 }
464 }
465
466 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
467 }
468
469 static async listRemoteExpired () {
470 const actor = await getServerActor()
471
472 const query = {
473 where: {
474 actorId: {
475 [Op.ne]: actor.id
476 },
477 expiresOn: {
478 [Op.lt]: new Date(),
479 [Op.ne]: null
480 }
481 }
482 }
483
484 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
485 }
486
487 static async listLocalOfServer (serverId: number) {
488 const actor = await getServerActor()
489 const buildVideoInclude = () => ({
490 model: VideoModel,
491 required: true,
492 include: [
493 {
494 attributes: [],
495 model: VideoChannelModel.unscoped(),
496 required: true,
497 include: [
498 {
499 attributes: [],
500 model: ActorModel.unscoped(),
501 required: true,
502 where: {
503 serverId
504 }
505 }
506 ]
507 }
508 ]
509 })
510
511 const query = {
512 where: {
513 actorId: actor.id
514 },
515 include: [
516 {
517 model: VideoFileModel,
518 required: false,
519 include: [ buildVideoInclude() ]
520 },
521 {
522 model: VideoStreamingPlaylistModel,
523 required: false,
524 include: [ buildVideoInclude() ]
525 }
526 ]
527 }
528
529 return VideoRedundancyModel.findAll(query)
530 }
531
532 static listForApi (options: {
533 start: number
534 count: number
535 sort: string
536 target: VideoRedundanciesTarget
537 strategy?: string
538 }) {
539 const { start, count, sort, target, strategy } = options
540 const redundancyWhere: WhereOptions = {}
541 const videosWhere: WhereOptions = {}
542 let redundancySqlSuffix = ''
543
544 if (target === 'my-videos') {
545 Object.assign(videosWhere, { remote: false })
546 } else if (target === 'remote-videos') {
547 Object.assign(videosWhere, { remote: true })
548 Object.assign(redundancyWhere, { strategy: { [Op.ne]: null } })
549 redundancySqlSuffix = ' AND "videoRedundancy"."strategy" IS NOT NULL'
550 }
551
552 if (strategy) {
553 Object.assign(redundancyWhere, { strategy: strategy })
554 }
555
556 const videoFilterWhere = {
557 [Op.and]: [
558 {
559 [Op.or]: [
560 {
561 id: {
562 [Op.in]: literal(
563 '(' +
564 'SELECT "videoId" FROM "videoFile" ' +
565 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoFileId" = "videoFile".id' +
566 redundancySqlSuffix +
567 ')'
568 )
569 }
570 },
571 {
572 id: {
573 [Op.in]: literal(
574 '(' +
575 'select "videoId" FROM "videoStreamingPlaylist" ' +
576 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' +
577 redundancySqlSuffix +
578 ')'
579 )
580 }
581 }
582 ]
583 },
584
585 videosWhere
586 ]
587 }
588
589 // /!\ On video model /!\
590 const findOptions = {
591 offset: start,
592 limit: count,
593 order: getSort(sort),
594 include: [
595 {
596 required: false,
597 model: VideoFileModel,
598 include: [
599 {
600 model: VideoRedundancyModel.unscoped(),
601 required: false,
602 where: redundancyWhere
603 }
604 ]
605 },
606 {
607 required: false,
608 model: VideoStreamingPlaylistModel.unscoped(),
609 include: [
610 {
611 model: VideoRedundancyModel.unscoped(),
612 required: false,
613 where: redundancyWhere
614 },
615 {
616 model: VideoFileModel,
617 required: false
618 }
619 ]
620 }
621 ],
622 where: videoFilterWhere
623 }
624
625 // /!\ On video model /!\
626 const countOptions = {
627 where: videoFilterWhere
628 }
629
630 return Promise.all([
631 VideoModel.findAll(findOptions),
632
633 VideoModel.count(countOptions)
634 ]).then(([ data, total ]) => ({ total, data }))
635 }
636
637 static async getStats (strategy: VideoRedundancyStrategyWithManual) {
638 const actor = await getServerActor()
639
640 const sql = `WITH "tmp" AS ` +
641 `(` +
642 `SELECT "videoFile"."size" AS "videoFileSize", "videoStreamingFile"."size" AS "videoStreamingFileSize", ` +
643 `"videoFile"."videoId" AS "videoFileVideoId", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` +
644 `FROM "videoRedundancy" AS "videoRedundancy" ` +
645 `LEFT JOIN "videoFile" AS "videoFile" ON "videoRedundancy"."videoFileId" = "videoFile"."id" ` +
646 `LEFT JOIN "videoStreamingPlaylist" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ` +
647 `LEFT JOIN "videoFile" AS "videoStreamingFile" ` +
648 `ON "videoStreamingPlaylist"."id" = "videoStreamingFile"."videoStreamingPlaylistId" ` +
649 `WHERE "videoRedundancy"."strategy" = :strategy AND "videoRedundancy"."actorId" = :actorId` +
650 `), ` +
651 `"videoIds" AS (` +
652 `SELECT "videoFileVideoId" AS "videoId" FROM "tmp" ` +
653 `UNION SELECT "videoStreamingVideoId" AS "videoId" FROM "tmp" ` +
654 `) ` +
655 `SELECT ` +
656 `COALESCE(SUM("videoFileSize"), '0') + COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` +
657 `(SELECT COUNT("videoIds"."videoId") FROM "videoIds") AS "totalVideos", ` +
658 `COUNT(*) AS "totalVideoFiles" ` +
659 `FROM "tmp"`
660
661 return VideoRedundancyModel.sequelize.query<any>(sql, {
662 replacements: { strategy, actorId: actor.id },
663 type: QueryTypes.SELECT
664 }).then(([ row ]) => ({
665 totalUsed: parseAggregateResult(row.totalUsed),
666 totalVideos: row.totalVideos,
667 totalVideoFiles: row.totalVideoFiles
668 }))
669 }
670
671 static toFormattedJSONStatic (video: MVideoForRedundancyAPI): VideoRedundancy {
672 const filesRedundancies: FileRedundancyInformation[] = []
673 const streamingPlaylistsRedundancies: StreamingPlaylistRedundancyInformation[] = []
674
675 for (const file of video.VideoFiles) {
676 for (const redundancy of file.RedundancyVideos) {
677 filesRedundancies.push({
678 id: redundancy.id,
679 fileUrl: redundancy.fileUrl,
680 strategy: redundancy.strategy,
681 createdAt: redundancy.createdAt,
682 updatedAt: redundancy.updatedAt,
683 expiresOn: redundancy.expiresOn,
684 size: file.size
685 })
686 }
687 }
688
689 for (const playlist of video.VideoStreamingPlaylists) {
690 const size = playlist.VideoFiles.reduce((a, b) => a + b.size, 0)
691
692 for (const redundancy of playlist.RedundancyVideos) {
693 streamingPlaylistsRedundancies.push({
694 id: redundancy.id,
695 fileUrl: redundancy.fileUrl,
696 strategy: redundancy.strategy,
697 createdAt: redundancy.createdAt,
698 updatedAt: redundancy.updatedAt,
699 expiresOn: redundancy.expiresOn,
700 size
701 })
702 }
703 }
704
705 return {
706 id: video.id,
707 name: video.name,
708 url: video.url,
709 uuid: video.uuid,
710
711 redundancies: {
712 files: filesRedundancies,
713 streamingPlaylists: streamingPlaylistsRedundancies
714 }
715 }
716 }
717
718 getVideo () {
719 if (this.VideoFile?.Video) return this.VideoFile.Video
720
721 if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video
722
723 return undefined
724 }
725
726 isOwned () {
727 return !!this.strategy
728 }
729
730 toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject {
731 if (this.VideoStreamingPlaylist) {
732 return {
733 id: this.url,
734 type: 'CacheFile' as 'CacheFile',
735 object: this.VideoStreamingPlaylist.Video.url,
736 expires: this.expiresOn ? this.expiresOn.toISOString() : null,
737 url: {
738 type: 'Link',
739 mediaType: 'application/x-mpegURL',
740 href: this.fileUrl
741 }
742 }
743 }
744
745 return {
746 id: this.url,
747 type: 'CacheFile' as 'CacheFile',
748 object: this.VideoFile.Video.url,
749 expires: this.expiresOn ? this.expiresOn.toISOString() : null,
750 url: {
751 type: 'Link',
752 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[this.VideoFile.extname] as any,
753 href: this.fileUrl,
754 height: this.VideoFile.resolution,
755 size: this.VideoFile.size,
756 fps: this.VideoFile.fps
757 }
758 }
759 }
760
761 // Don't include video files we already duplicated
762 private static buildVideoIdsForDuplication (peertubeActor: MActor) {
763 const notIn = literal(
764 '(' +
765 `SELECT "videoFile"."videoId" AS "videoId" FROM "videoRedundancy" ` +
766 `INNER JOIN "videoFile" ON "videoFile"."id" = "videoRedundancy"."videoFileId" ` +
767 `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
768 `UNION ` +
769 `SELECT "videoStreamingPlaylist"."videoId" AS "videoId" FROM "videoRedundancy" ` +
770 `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoRedundancy"."videoStreamingPlaylistId" ` +
771 `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
772 ')'
773 )
774
775 return {
776 id: {
777 [Op.notIn]: notIn
778 }
779 }
780 }
781
782 private static buildServerRedundancyInclude () {
783 return {
784 attributes: [],
785 model: VideoChannelModel.unscoped(),
786 required: true,
787 include: [
788 {
789 attributes: [],
790 model: ActorModel.unscoped(),
791 required: true,
792 include: [
793 {
794 attributes: [],
795 model: ServerModel.unscoped(),
796 required: true,
797 where: {
798 redundancyAllowed: true
799 }
800 }
801 ]
802 }
803 ]
804 }
805 }
806 }