]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video.ts
Move enabled download/comments in advanced tab
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
1 import * as Bluebird from 'bluebird'
2 import { maxBy } from 'lodash'
3 import * as magnetUtil from 'magnet-uri'
4 import * as parseTorrent from 'parse-torrent'
5 import { join } from 'path'
6 import * as Sequelize from 'sequelize'
7 import {
8 AllowNull,
9 BeforeDestroy,
10 BelongsTo,
11 BelongsToMany,
12 Column,
13 CreatedAt,
14 DataType,
15 Default,
16 ForeignKey,
17 HasMany,
18 HasOne,
19 IFindOptions,
20 IIncludeOptions,
21 Is,
22 IsInt,
23 IsUUID,
24 Min,
25 Model,
26 Scopes,
27 Table,
28 UpdatedAt
29 } from 'sequelize-typescript'
30 import { UserRight, VideoPrivacy, VideoState } from '../../../shared'
31 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
32 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
33 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
34 import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils'
35 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
36 import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc'
37 import {
38 isVideoCategoryValid,
39 isVideoDescriptionValid,
40 isVideoDurationValid,
41 isVideoLanguageValid,
42 isVideoLicenceValid,
43 isVideoNameValid,
44 isVideoPrivacyValid,
45 isVideoStateValid,
46 isVideoSupportValid
47 } from '../../helpers/custom-validators/videos'
48 import { generateImageFromVideoFile, getVideoFileResolution } from '../../helpers/ffmpeg-utils'
49 import { logger } from '../../helpers/logger'
50 import { getServerActor } from '../../helpers/utils'
51 import {
52 ACTIVITY_PUB,
53 API_VERSION,
54 CONFIG,
55 CONSTRAINTS_FIELDS, HLS_PLAYLIST_DIRECTORY, HLS_REDUNDANCY_DIRECTORY,
56 PREVIEWS_SIZE,
57 REMOTE_SCHEME,
58 STATIC_DOWNLOAD_PATHS,
59 STATIC_PATHS,
60 THUMBNAILS_SIZE,
61 VIDEO_CATEGORIES,
62 VIDEO_LANGUAGES,
63 VIDEO_LICENCES,
64 VIDEO_PRIVACIES,
65 VIDEO_STATES
66 } from '../../initializers'
67 import { sendDeleteVideo } from '../../lib/activitypub/send'
68 import { AccountModel } from '../account/account'
69 import { AccountVideoRateModel } from '../account/account-video-rate'
70 import { ActorModel } from '../activitypub/actor'
71 import { AvatarModel } from '../avatar/avatar'
72 import { ServerModel } from '../server/server'
73 import { buildBlockedAccountSQL, buildTrigramSearchIndex, createSimilarityAttribute, getVideoSort, throwIfNotValid } from '../utils'
74 import { TagModel } from './tag'
75 import { VideoAbuseModel } from './video-abuse'
76 import { VideoChannelModel } from './video-channel'
77 import { VideoCommentModel } from './video-comment'
78 import { VideoFileModel } from './video-file'
79 import { VideoShareModel } from './video-share'
80 import { VideoTagModel } from './video-tag'
81 import { ScheduleVideoUpdateModel } from './schedule-video-update'
82 import { VideoCaptionModel } from './video-caption'
83 import { VideoBlacklistModel } from './video-blacklist'
84 import { remove, writeFile } from 'fs-extra'
85 import { VideoViewModel } from './video-views'
86 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
87 import {
88 videoFilesModelToFormattedJSON,
89 VideoFormattingJSONOptions,
90 videoModelToActivityPubObject,
91 videoModelToFormattedDetailsJSON,
92 videoModelToFormattedJSON
93 } from './video-format-utils'
94 import * as validator from 'validator'
95 import { UserVideoHistoryModel } from '../account/user-video-history'
96 import { UserModel } from '../account/user'
97 import { VideoImportModel } from './video-import'
98 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
99
100 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
101 const indexes: Sequelize.DefineIndexesOptions[] = [
102 buildTrigramSearchIndex('video_name_trigram', 'name'),
103
104 { fields: [ 'createdAt' ] },
105 { fields: [ 'publishedAt' ] },
106 { fields: [ 'duration' ] },
107 { fields: [ 'views' ] },
108 { fields: [ 'channelId' ] },
109 {
110 fields: [ 'category' ], // We don't care videos with an unknown category
111 where: {
112 category: {
113 [Sequelize.Op.ne]: null
114 }
115 }
116 },
117 {
118 fields: [ 'licence' ], // We don't care videos with an unknown licence
119 where: {
120 licence: {
121 [Sequelize.Op.ne]: null
122 }
123 }
124 },
125 {
126 fields: [ 'language' ], // We don't care videos with an unknown language
127 where: {
128 language: {
129 [Sequelize.Op.ne]: null
130 }
131 }
132 },
133 {
134 fields: [ 'nsfw' ], // Most of the videos are not NSFW
135 where: {
136 nsfw: true
137 }
138 },
139 {
140 fields: [ 'remote' ], // Only index local videos
141 where: {
142 remote: false
143 }
144 },
145 {
146 fields: [ 'uuid' ],
147 unique: true
148 },
149 {
150 fields: [ 'url' ],
151 unique: true
152 }
153 ]
154
155 export enum ScopeNames {
156 AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
157 FOR_API = 'FOR_API',
158 WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
159 WITH_TAGS = 'WITH_TAGS',
160 WITH_FILES = 'WITH_FILES',
161 WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE',
162 WITH_BLACKLISTED = 'WITH_BLACKLISTED',
163 WITH_USER_HISTORY = 'WITH_USER_HISTORY',
164 WITH_STREAMING_PLAYLISTS = 'WITH_STREAMING_PLAYLISTS',
165 WITH_USER_ID = 'WITH_USER_ID'
166 }
167
168 type ForAPIOptions = {
169 ids: number[]
170 withFiles?: boolean
171 }
172
173 type AvailableForListIDsOptions = {
174 serverAccountId: number
175 followerActorId: number
176 includeLocalVideos: boolean
177 filter?: VideoFilter
178 categoryOneOf?: number[]
179 nsfw?: boolean
180 licenceOneOf?: number[]
181 languageOneOf?: string[]
182 tagsOneOf?: string[]
183 tagsAllOf?: string[]
184 withFiles?: boolean
185 accountId?: number
186 videoChannelId?: number
187 trendingDays?: number
188 user?: UserModel,
189 historyOfUser?: UserModel
190 }
191
192 @Scopes({
193 [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => {
194 const accountInclude = {
195 attributes: [ 'id', 'name' ],
196 model: AccountModel.unscoped(),
197 required: true,
198 include: [
199 {
200 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
201 model: ActorModel.unscoped(),
202 required: true,
203 include: [
204 {
205 attributes: [ 'host' ],
206 model: ServerModel.unscoped(),
207 required: false
208 },
209 {
210 model: AvatarModel.unscoped(),
211 required: false
212 }
213 ]
214 }
215 ]
216 }
217
218 const videoChannelInclude = {
219 attributes: [ 'name', 'description', 'id' ],
220 model: VideoChannelModel.unscoped(),
221 required: true,
222 include: [
223 {
224 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
225 model: ActorModel.unscoped(),
226 required: true,
227 include: [
228 {
229 attributes: [ 'host' ],
230 model: ServerModel.unscoped(),
231 required: false
232 },
233 {
234 model: AvatarModel.unscoped(),
235 required: false
236 }
237 ]
238 },
239 accountInclude
240 ]
241 }
242
243 const query: IFindOptions<VideoModel> = {
244 where: {
245 id: {
246 [ Sequelize.Op.any ]: options.ids
247 }
248 },
249 include: [ videoChannelInclude ]
250 }
251
252 if (options.withFiles === true) {
253 query.include.push({
254 model: VideoFileModel.unscoped(),
255 required: true
256 })
257 }
258
259 return query
260 },
261 [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => {
262 const query: IFindOptions<VideoModel> = {
263 raw: true,
264 attributes: [ 'id' ],
265 where: {
266 id: {
267 [ Sequelize.Op.and ]: [
268 {
269 [ Sequelize.Op.notIn ]: Sequelize.literal(
270 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
271 )
272 }
273 ]
274 },
275 channelId: {
276 [ Sequelize.Op.notIn ]: Sequelize.literal(
277 '(' +
278 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' +
279 buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) +
280 ')' +
281 ')'
282 )
283 }
284 },
285 include: []
286 }
287
288 // Only list public/published videos
289 if (!options.filter || options.filter !== 'all-local') {
290 const privacyWhere = {
291 // Always list public videos
292 privacy: VideoPrivacy.PUBLIC,
293 // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
294 [ Sequelize.Op.or ]: [
295 {
296 state: VideoState.PUBLISHED
297 },
298 {
299 [ Sequelize.Op.and ]: {
300 state: VideoState.TO_TRANSCODE,
301 waitTranscoding: false
302 }
303 }
304 ]
305 }
306
307 Object.assign(query.where, privacyWhere)
308 }
309
310 if (options.filter || options.accountId || options.videoChannelId) {
311 const videoChannelInclude: IIncludeOptions = {
312 attributes: [],
313 model: VideoChannelModel.unscoped(),
314 required: true
315 }
316
317 if (options.videoChannelId) {
318 videoChannelInclude.where = {
319 id: options.videoChannelId
320 }
321 }
322
323 if (options.filter || options.accountId) {
324 const accountInclude: IIncludeOptions = {
325 attributes: [],
326 model: AccountModel.unscoped(),
327 required: true
328 }
329
330 if (options.filter) {
331 accountInclude.include = [
332 {
333 attributes: [],
334 model: ActorModel.unscoped(),
335 required: true,
336 where: VideoModel.buildActorWhereWithFilter(options.filter)
337 }
338 ]
339 }
340
341 if (options.accountId) {
342 accountInclude.where = { id: options.accountId }
343 }
344
345 videoChannelInclude.include = [ accountInclude ]
346 }
347
348 query.include.push(videoChannelInclude)
349 }
350
351 if (options.followerActorId) {
352 let localVideosReq = ''
353 if (options.includeLocalVideos === true) {
354 localVideosReq = ' UNION ALL ' +
355 'SELECT "video"."id" AS "id" FROM "video" ' +
356 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
357 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
358 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
359 'WHERE "actor"."serverId" IS NULL'
360 }
361
362 // Force actorId to be a number to avoid SQL injections
363 const actorIdNumber = parseInt(options.followerActorId.toString(), 10)
364 query.where[ 'id' ][ Sequelize.Op.and ].push({
365 [ Sequelize.Op.in ]: Sequelize.literal(
366 '(' +
367 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
368 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
369 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
370 ' UNION ALL ' +
371 'SELECT "video"."id" AS "id" FROM "video" ' +
372 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
373 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
374 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
375 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
376 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
377 localVideosReq +
378 ')'
379 )
380 })
381 }
382
383 if (options.withFiles === true) {
384 query.where[ 'id' ][ Sequelize.Op.and ].push({
385 [ Sequelize.Op.in ]: Sequelize.literal(
386 '(SELECT "videoId" FROM "videoFile")'
387 )
388 })
389 }
390
391 // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
392 if (options.tagsAllOf || options.tagsOneOf) {
393 const createTagsIn = (tags: string[]) => {
394 return tags.map(t => VideoModel.sequelize.escape(t))
395 .join(', ')
396 }
397
398 if (options.tagsOneOf) {
399 query.where[ 'id' ][ Sequelize.Op.and ].push({
400 [ Sequelize.Op.in ]: Sequelize.literal(
401 '(' +
402 'SELECT "videoId" FROM "videoTag" ' +
403 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
404 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
405 ')'
406 )
407 })
408 }
409
410 if (options.tagsAllOf) {
411 query.where[ 'id' ][ Sequelize.Op.and ].push({
412 [ Sequelize.Op.in ]: Sequelize.literal(
413 '(' +
414 'SELECT "videoId" FROM "videoTag" ' +
415 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
416 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
417 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
418 ')'
419 )
420 })
421 }
422 }
423
424 if (options.nsfw === true || options.nsfw === false) {
425 query.where[ 'nsfw' ] = options.nsfw
426 }
427
428 if (options.categoryOneOf) {
429 query.where[ 'category' ] = {
430 [ Sequelize.Op.or ]: options.categoryOneOf
431 }
432 }
433
434 if (options.licenceOneOf) {
435 query.where[ 'licence' ] = {
436 [ Sequelize.Op.or ]: options.licenceOneOf
437 }
438 }
439
440 if (options.languageOneOf) {
441 query.where[ 'language' ] = {
442 [ Sequelize.Op.or ]: options.languageOneOf
443 }
444 }
445
446 if (options.trendingDays) {
447 query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
448
449 query.subQuery = false
450 }
451
452 if (options.historyOfUser) {
453 query.include.push({
454 model: UserVideoHistoryModel,
455 required: true,
456 where: {
457 userId: options.historyOfUser.id
458 }
459 })
460
461 // Even if the relation is n:m, we know that a user only have 0..1 video history
462 // So we won't have multiple rows for the same video
463 // Without this, we would not be able to sort on "updatedAt" column of UserVideoHistoryModel
464 query.subQuery = false
465 }
466
467 return query
468 },
469 [ ScopeNames.WITH_USER_ID ]: {
470 include: [
471 {
472 attributes: [ 'accountId' ],
473 model: () => VideoChannelModel.unscoped(),
474 required: true,
475 include: [
476 {
477 attributes: [ 'userId' ],
478 model: () => AccountModel.unscoped(),
479 required: true
480 }
481 ]
482 }
483 ]
484 },
485 [ ScopeNames.WITH_ACCOUNT_DETAILS ]: {
486 include: [
487 {
488 model: () => VideoChannelModel.unscoped(),
489 required: true,
490 include: [
491 {
492 attributes: {
493 exclude: [ 'privateKey', 'publicKey' ]
494 },
495 model: () => ActorModel.unscoped(),
496 required: true,
497 include: [
498 {
499 attributes: [ 'host' ],
500 model: () => ServerModel.unscoped(),
501 required: false
502 },
503 {
504 model: () => AvatarModel.unscoped(),
505 required: false
506 }
507 ]
508 },
509 {
510 model: () => AccountModel.unscoped(),
511 required: true,
512 include: [
513 {
514 model: () => ActorModel.unscoped(),
515 attributes: {
516 exclude: [ 'privateKey', 'publicKey' ]
517 },
518 required: true,
519 include: [
520 {
521 attributes: [ 'host' ],
522 model: () => ServerModel.unscoped(),
523 required: false
524 },
525 {
526 model: () => AvatarModel.unscoped(),
527 required: false
528 }
529 ]
530 }
531 ]
532 }
533 ]
534 }
535 ]
536 },
537 [ ScopeNames.WITH_TAGS ]: {
538 include: [ () => TagModel ]
539 },
540 [ ScopeNames.WITH_BLACKLISTED ]: {
541 include: [
542 {
543 attributes: [ 'id', 'reason' ],
544 model: () => VideoBlacklistModel,
545 required: false
546 }
547 ]
548 },
549 [ ScopeNames.WITH_FILES ]: (withRedundancies = false) => {
550 let subInclude: any[] = []
551
552 if (withRedundancies === true) {
553 subInclude = [
554 {
555 attributes: [ 'fileUrl' ],
556 model: VideoRedundancyModel.unscoped(),
557 required: false
558 }
559 ]
560 }
561
562 return {
563 include: [
564 {
565 model: VideoFileModel.unscoped(),
566 // FIXME: typings
567 [ 'separate' as any ]: true, // We may have multiple files, having multiple redundancies so let's separate this join
568 required: false,
569 include: subInclude
570 }
571 ]
572 }
573 },
574 [ ScopeNames.WITH_STREAMING_PLAYLISTS ]: (withRedundancies = false) => {
575 let subInclude: any[] = []
576
577 if (withRedundancies === true) {
578 subInclude = [
579 {
580 attributes: [ 'fileUrl' ],
581 model: VideoRedundancyModel.unscoped(),
582 required: false
583 }
584 ]
585 }
586
587 return {
588 include: [
589 {
590 model: VideoStreamingPlaylistModel.unscoped(),
591 // FIXME: typings
592 [ 'separate' as any ]: true, // We may have multiple streaming playlists, having multiple redundancies so let's separate this join
593 required: false,
594 include: subInclude
595 }
596 ]
597 }
598 },
599 [ ScopeNames.WITH_SCHEDULED_UPDATE ]: {
600 include: [
601 {
602 model: () => ScheduleVideoUpdateModel.unscoped(),
603 required: false
604 }
605 ]
606 },
607 [ ScopeNames.WITH_USER_HISTORY ]: (userId: number) => {
608 return {
609 include: [
610 {
611 attributes: [ 'currentTime' ],
612 model: UserVideoHistoryModel.unscoped(),
613 required: false,
614 where: {
615 userId
616 }
617 }
618 ]
619 }
620 }
621 })
622 @Table({
623 tableName: 'video',
624 indexes
625 })
626 export class VideoModel extends Model<VideoModel> {
627
628 @AllowNull(false)
629 @Default(DataType.UUIDV4)
630 @IsUUID(4)
631 @Column(DataType.UUID)
632 uuid: string
633
634 @AllowNull(false)
635 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
636 @Column
637 name: string
638
639 @AllowNull(true)
640 @Default(null)
641 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
642 @Column
643 category: number
644
645 @AllowNull(true)
646 @Default(null)
647 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
648 @Column
649 licence: number
650
651 @AllowNull(true)
652 @Default(null)
653 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
654 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
655 language: string
656
657 @AllowNull(false)
658 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
659 @Column
660 privacy: number
661
662 @AllowNull(false)
663 @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
664 @Column
665 nsfw: boolean
666
667 @AllowNull(true)
668 @Default(null)
669 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
670 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
671 description: string
672
673 @AllowNull(true)
674 @Default(null)
675 @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
676 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
677 support: string
678
679 @AllowNull(false)
680 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
681 @Column
682 duration: number
683
684 @AllowNull(false)
685 @Default(0)
686 @IsInt
687 @Min(0)
688 @Column
689 views: number
690
691 @AllowNull(false)
692 @Default(0)
693 @IsInt
694 @Min(0)
695 @Column
696 likes: number
697
698 @AllowNull(false)
699 @Default(0)
700 @IsInt
701 @Min(0)
702 @Column
703 dislikes: number
704
705 @AllowNull(false)
706 @Column
707 remote: boolean
708
709 @AllowNull(false)
710 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
711 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
712 url: string
713
714 @AllowNull(false)
715 @Column
716 commentsEnabled: boolean
717
718 @AllowNull(false)
719 @Column
720 downloadEnabled: boolean
721
722 @AllowNull(false)
723 @Column
724 waitTranscoding: boolean
725
726 @AllowNull(false)
727 @Default(null)
728 @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
729 @Column
730 state: VideoState
731
732 @CreatedAt
733 createdAt: Date
734
735 @UpdatedAt
736 updatedAt: Date
737
738 @AllowNull(false)
739 @Default(Sequelize.NOW)
740 @Column
741 publishedAt: Date
742
743 @ForeignKey(() => VideoChannelModel)
744 @Column
745 channelId: number
746
747 @BelongsTo(() => VideoChannelModel, {
748 foreignKey: {
749 allowNull: true
750 },
751 hooks: true
752 })
753 VideoChannel: VideoChannelModel
754
755 @BelongsToMany(() => TagModel, {
756 foreignKey: 'videoId',
757 through: () => VideoTagModel,
758 onDelete: 'CASCADE'
759 })
760 Tags: TagModel[]
761
762 @HasMany(() => VideoAbuseModel, {
763 foreignKey: {
764 name: 'videoId',
765 allowNull: false
766 },
767 onDelete: 'cascade'
768 })
769 VideoAbuses: VideoAbuseModel[]
770
771 @HasMany(() => VideoFileModel, {
772 foreignKey: {
773 name: 'videoId',
774 allowNull: false
775 },
776 hooks: true,
777 onDelete: 'cascade'
778 })
779 VideoFiles: VideoFileModel[]
780
781 @HasMany(() => VideoStreamingPlaylistModel, {
782 foreignKey: {
783 name: 'videoId',
784 allowNull: false
785 },
786 hooks: true,
787 onDelete: 'cascade'
788 })
789 VideoStreamingPlaylists: VideoStreamingPlaylistModel[]
790
791 @HasMany(() => VideoShareModel, {
792 foreignKey: {
793 name: 'videoId',
794 allowNull: false
795 },
796 onDelete: 'cascade'
797 })
798 VideoShares: VideoShareModel[]
799
800 @HasMany(() => AccountVideoRateModel, {
801 foreignKey: {
802 name: 'videoId',
803 allowNull: false
804 },
805 onDelete: 'cascade'
806 })
807 AccountVideoRates: AccountVideoRateModel[]
808
809 @HasMany(() => VideoCommentModel, {
810 foreignKey: {
811 name: 'videoId',
812 allowNull: false
813 },
814 onDelete: 'cascade',
815 hooks: true
816 })
817 VideoComments: VideoCommentModel[]
818
819 @HasMany(() => VideoViewModel, {
820 foreignKey: {
821 name: 'videoId',
822 allowNull: false
823 },
824 onDelete: 'cascade'
825 })
826 VideoViews: VideoViewModel[]
827
828 @HasMany(() => UserVideoHistoryModel, {
829 foreignKey: {
830 name: 'videoId',
831 allowNull: false
832 },
833 onDelete: 'cascade'
834 })
835 UserVideoHistories: UserVideoHistoryModel[]
836
837 @HasOne(() => ScheduleVideoUpdateModel, {
838 foreignKey: {
839 name: 'videoId',
840 allowNull: false
841 },
842 onDelete: 'cascade'
843 })
844 ScheduleVideoUpdate: ScheduleVideoUpdateModel
845
846 @HasOne(() => VideoBlacklistModel, {
847 foreignKey: {
848 name: 'videoId',
849 allowNull: false
850 },
851 onDelete: 'cascade'
852 })
853 VideoBlacklist: VideoBlacklistModel
854
855 @HasOne(() => VideoImportModel, {
856 foreignKey: {
857 name: 'videoId',
858 allowNull: true
859 },
860 onDelete: 'set null'
861 })
862 VideoImport: VideoImportModel
863
864 @HasMany(() => VideoCaptionModel, {
865 foreignKey: {
866 name: 'videoId',
867 allowNull: false
868 },
869 onDelete: 'cascade',
870 hooks: true,
871 [ 'separate' as any ]: true
872 })
873 VideoCaptions: VideoCaptionModel[]
874
875 @BeforeDestroy
876 static async sendDelete (instance: VideoModel, options) {
877 if (instance.isOwned()) {
878 if (!instance.VideoChannel) {
879 instance.VideoChannel = await instance.$get('VideoChannel', {
880 include: [
881 {
882 model: AccountModel,
883 include: [ ActorModel ]
884 }
885 ],
886 transaction: options.transaction
887 }) as VideoChannelModel
888 }
889
890 return sendDeleteVideo(instance, options.transaction)
891 }
892
893 return undefined
894 }
895
896 @BeforeDestroy
897 static async removeFiles (instance: VideoModel) {
898 const tasks: Promise<any>[] = []
899
900 logger.info('Removing files of video %s.', instance.url)
901
902 tasks.push(instance.removeThumbnail())
903
904 if (instance.isOwned()) {
905 if (!Array.isArray(instance.VideoFiles)) {
906 instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
907 }
908
909 tasks.push(instance.removePreview())
910
911 // Remove physical files and torrents
912 instance.VideoFiles.forEach(file => {
913 tasks.push(instance.removeFile(file))
914 tasks.push(instance.removeTorrent(file))
915 })
916
917 // Remove playlists file
918 tasks.push(instance.removeStreamingPlaylist())
919 }
920
921 // Do not wait video deletion because we could be in a transaction
922 Promise.all(tasks)
923 .catch(err => {
924 logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err })
925 })
926
927 return undefined
928 }
929
930 static listLocal () {
931 const query = {
932 where: {
933 remote: false
934 }
935 }
936
937 return VideoModel.scope([ ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS ]).findAll(query)
938 }
939
940 static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
941 function getRawQuery (select: string) {
942 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
943 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
944 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
945 'WHERE "Account"."actorId" = ' + actorId
946 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
947 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
948 'WHERE "VideoShare"."actorId" = ' + actorId
949
950 return `(${queryVideo}) UNION (${queryVideoShare})`
951 }
952
953 const rawQuery = getRawQuery('"Video"."id"')
954 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
955
956 const query = {
957 distinct: true,
958 offset: start,
959 limit: count,
960 order: getVideoSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
961 where: {
962 id: {
963 [ Sequelize.Op.in ]: Sequelize.literal('(' + rawQuery + ')')
964 },
965 [ Sequelize.Op.or ]: [
966 { privacy: VideoPrivacy.PUBLIC },
967 { privacy: VideoPrivacy.UNLISTED }
968 ]
969 },
970 include: [
971 {
972 attributes: [ 'language' ],
973 model: VideoCaptionModel.unscoped(),
974 required: false
975 },
976 {
977 attributes: [ 'id', 'url' ],
978 model: VideoShareModel.unscoped(),
979 required: false,
980 // We only want videos shared by this actor
981 where: {
982 [ Sequelize.Op.and ]: [
983 {
984 id: {
985 [ Sequelize.Op.not ]: null
986 }
987 },
988 {
989 actorId
990 }
991 ]
992 },
993 include: [
994 {
995 attributes: [ 'id', 'url' ],
996 model: ActorModel.unscoped()
997 }
998 ]
999 },
1000 {
1001 model: VideoChannelModel.unscoped(),
1002 required: true,
1003 include: [
1004 {
1005 attributes: [ 'name' ],
1006 model: AccountModel.unscoped(),
1007 required: true,
1008 include: [
1009 {
1010 attributes: [ 'id', 'url', 'followersUrl' ],
1011 model: ActorModel.unscoped(),
1012 required: true
1013 }
1014 ]
1015 },
1016 {
1017 attributes: [ 'id', 'url', 'followersUrl' ],
1018 model: ActorModel.unscoped(),
1019 required: true
1020 }
1021 ]
1022 },
1023 VideoFileModel,
1024 TagModel
1025 ]
1026 }
1027
1028 return Bluebird.all([
1029 // FIXME: typing issue
1030 VideoModel.findAll(query as any),
1031 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
1032 ]).then(([ rows, totals ]) => {
1033 // totals: totalVideos + totalVideoShares
1034 let totalVideos = 0
1035 let totalVideoShares = 0
1036 if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10)
1037 if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10)
1038
1039 const total = totalVideos + totalVideoShares
1040 return {
1041 data: rows,
1042 total: total
1043 }
1044 })
1045 }
1046
1047 static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) {
1048 const query: IFindOptions<VideoModel> = {
1049 offset: start,
1050 limit: count,
1051 order: getVideoSort(sort),
1052 include: [
1053 {
1054 model: VideoChannelModel,
1055 required: true,
1056 include: [
1057 {
1058 model: AccountModel,
1059 where: {
1060 id: accountId
1061 },
1062 required: true
1063 }
1064 ]
1065 },
1066 {
1067 model: ScheduleVideoUpdateModel,
1068 required: false
1069 },
1070 {
1071 model: VideoBlacklistModel,
1072 required: false
1073 }
1074 ]
1075 }
1076
1077 if (withFiles === true) {
1078 query.include.push({
1079 model: VideoFileModel.unscoped(),
1080 required: true
1081 })
1082 }
1083
1084 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
1085 return {
1086 data: rows,
1087 total: count
1088 }
1089 })
1090 }
1091
1092 static async listForApi (options: {
1093 start: number,
1094 count: number,
1095 sort: string,
1096 nsfw: boolean,
1097 includeLocalVideos: boolean,
1098 withFiles: boolean,
1099 categoryOneOf?: number[],
1100 licenceOneOf?: number[],
1101 languageOneOf?: string[],
1102 tagsOneOf?: string[],
1103 tagsAllOf?: string[],
1104 filter?: VideoFilter,
1105 accountId?: number,
1106 videoChannelId?: number,
1107 followerActorId?: number
1108 trendingDays?: number,
1109 user?: UserModel,
1110 historyOfUser?: UserModel
1111 }, countVideos = true) {
1112 if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) {
1113 throw new Error('Try to filter all-local but no user has not the see all videos right')
1114 }
1115
1116 const query: IFindOptions<VideoModel> = {
1117 offset: options.start,
1118 limit: options.count,
1119 order: getVideoSort(options.sort)
1120 }
1121
1122 let trendingDays: number
1123 if (options.sort.endsWith('trending')) {
1124 trendingDays = CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
1125
1126 query.group = 'VideoModel.id'
1127 }
1128
1129 const serverActor = await getServerActor()
1130
1131 // followerActorId === null has a meaning, so just check undefined
1132 const followerActorId = options.followerActorId !== undefined ? options.followerActorId : serverActor.id
1133
1134 const queryOptions = {
1135 followerActorId,
1136 serverAccountId: serverActor.Account.id,
1137 nsfw: options.nsfw,
1138 categoryOneOf: options.categoryOneOf,
1139 licenceOneOf: options.licenceOneOf,
1140 languageOneOf: options.languageOneOf,
1141 tagsOneOf: options.tagsOneOf,
1142 tagsAllOf: options.tagsAllOf,
1143 filter: options.filter,
1144 withFiles: options.withFiles,
1145 accountId: options.accountId,
1146 videoChannelId: options.videoChannelId,
1147 includeLocalVideos: options.includeLocalVideos,
1148 user: options.user,
1149 historyOfUser: options.historyOfUser,
1150 trendingDays
1151 }
1152
1153 return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
1154 }
1155
1156 static async searchAndPopulateAccountAndServer (options: {
1157 includeLocalVideos: boolean
1158 search?: string
1159 start?: number
1160 count?: number
1161 sort?: string
1162 startDate?: string // ISO 8601
1163 endDate?: string // ISO 8601
1164 nsfw?: boolean
1165 categoryOneOf?: number[]
1166 licenceOneOf?: number[]
1167 languageOneOf?: string[]
1168 tagsOneOf?: string[]
1169 tagsAllOf?: string[]
1170 durationMin?: number // seconds
1171 durationMax?: number // seconds
1172 user?: UserModel,
1173 filter?: VideoFilter
1174 }) {
1175 const whereAnd = []
1176
1177 if (options.startDate || options.endDate) {
1178 const publishedAtRange = {}
1179
1180 if (options.startDate) publishedAtRange[ Sequelize.Op.gte ] = options.startDate
1181 if (options.endDate) publishedAtRange[ Sequelize.Op.lte ] = options.endDate
1182
1183 whereAnd.push({ publishedAt: publishedAtRange })
1184 }
1185
1186 if (options.durationMin || options.durationMax) {
1187 const durationRange = {}
1188
1189 if (options.durationMin) durationRange[ Sequelize.Op.gte ] = options.durationMin
1190 if (options.durationMax) durationRange[ Sequelize.Op.lte ] = options.durationMax
1191
1192 whereAnd.push({ duration: durationRange })
1193 }
1194
1195 const attributesInclude = []
1196 const escapedSearch = VideoModel.sequelize.escape(options.search)
1197 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
1198 if (options.search) {
1199 whereAnd.push(
1200 {
1201 id: {
1202 [ Sequelize.Op.in ]: Sequelize.literal(
1203 '(' +
1204 'SELECT "video"."id" FROM "video" ' +
1205 'WHERE ' +
1206 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
1207 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
1208 'UNION ALL ' +
1209 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' +
1210 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
1211 'WHERE "tag"."name" = ' + escapedSearch +
1212 ')'
1213 )
1214 }
1215 }
1216 )
1217
1218 attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search))
1219 }
1220
1221 // Cannot search on similarity if we don't have a search
1222 if (!options.search) {
1223 attributesInclude.push(
1224 Sequelize.literal('0 as similarity')
1225 )
1226 }
1227
1228 const query: IFindOptions<VideoModel> = {
1229 attributes: {
1230 include: attributesInclude
1231 },
1232 offset: options.start,
1233 limit: options.count,
1234 order: getVideoSort(options.sort),
1235 where: {
1236 [ Sequelize.Op.and ]: whereAnd
1237 }
1238 }
1239
1240 const serverActor = await getServerActor()
1241 const queryOptions = {
1242 followerActorId: serverActor.id,
1243 serverAccountId: serverActor.Account.id,
1244 includeLocalVideos: options.includeLocalVideos,
1245 nsfw: options.nsfw,
1246 categoryOneOf: options.categoryOneOf,
1247 licenceOneOf: options.licenceOneOf,
1248 languageOneOf: options.languageOneOf,
1249 tagsOneOf: options.tagsOneOf,
1250 tagsAllOf: options.tagsAllOf,
1251 user: options.user,
1252 filter: options.filter
1253 }
1254
1255 return VideoModel.getAvailableForApi(query, queryOptions)
1256 }
1257
1258 static load (id: number | string, t?: Sequelize.Transaction) {
1259 const where = VideoModel.buildWhereIdOrUUID(id)
1260 const options = {
1261 where,
1262 transaction: t
1263 }
1264
1265 return VideoModel.findOne(options)
1266 }
1267
1268 static loadWithRights (id: number | string, t?: Sequelize.Transaction) {
1269 const where = VideoModel.buildWhereIdOrUUID(id)
1270 const options = {
1271 where,
1272 transaction: t
1273 }
1274
1275 return VideoModel.scope([ ScopeNames.WITH_BLACKLISTED, ScopeNames.WITH_USER_ID ]).findOne(options)
1276 }
1277
1278 static loadOnlyId (id: number | string, t?: Sequelize.Transaction) {
1279 const where = VideoModel.buildWhereIdOrUUID(id)
1280
1281 const options = {
1282 attributes: [ 'id' ],
1283 where,
1284 transaction: t
1285 }
1286
1287 return VideoModel.findOne(options)
1288 }
1289
1290 static loadWithFiles (id: number, t?: Sequelize.Transaction, logging?: boolean) {
1291 return VideoModel.scope([ ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS ])
1292 .findById(id, { transaction: t, logging })
1293 }
1294
1295 static loadByUUIDWithFile (uuid: string) {
1296 const options = {
1297 where: {
1298 uuid
1299 }
1300 }
1301
1302 return VideoModel.findOne(options)
1303 }
1304
1305 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
1306 const query: IFindOptions<VideoModel> = {
1307 where: {
1308 url
1309 },
1310 transaction
1311 }
1312
1313 return VideoModel.findOne(query)
1314 }
1315
1316 static loadByUrlAndPopulateAccount (url: string, transaction?: Sequelize.Transaction) {
1317 const query: IFindOptions<VideoModel> = {
1318 where: {
1319 url
1320 },
1321 transaction
1322 }
1323
1324 return VideoModel.scope([
1325 ScopeNames.WITH_ACCOUNT_DETAILS,
1326 ScopeNames.WITH_FILES,
1327 ScopeNames.WITH_STREAMING_PLAYLISTS
1328 ]).findOne(query)
1329 }
1330
1331 static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Sequelize.Transaction, userId?: number) {
1332 const where = VideoModel.buildWhereIdOrUUID(id)
1333
1334 const options = {
1335 order: [ [ 'Tags', 'name', 'ASC' ] ],
1336 where,
1337 transaction: t
1338 }
1339
1340 const scopes = [
1341 ScopeNames.WITH_TAGS,
1342 ScopeNames.WITH_BLACKLISTED,
1343 ScopeNames.WITH_ACCOUNT_DETAILS,
1344 ScopeNames.WITH_SCHEDULED_UPDATE,
1345 ScopeNames.WITH_FILES,
1346 ScopeNames.WITH_STREAMING_PLAYLISTS
1347 ]
1348
1349 if (userId) {
1350 scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings
1351 }
1352
1353 return VideoModel
1354 .scope(scopes)
1355 .findOne(options)
1356 }
1357
1358 static loadForGetAPI (id: number | string, t?: Sequelize.Transaction, userId?: number) {
1359 const where = VideoModel.buildWhereIdOrUUID(id)
1360
1361 const options = {
1362 order: [ [ 'Tags', 'name', 'ASC' ] ],
1363 where,
1364 transaction: t
1365 }
1366
1367 const scopes = [
1368 ScopeNames.WITH_TAGS,
1369 ScopeNames.WITH_BLACKLISTED,
1370 ScopeNames.WITH_ACCOUNT_DETAILS,
1371 ScopeNames.WITH_SCHEDULED_UPDATE,
1372 { method: [ ScopeNames.WITH_FILES, true ] } as any, // FIXME: typings
1373 { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] } as any // FIXME: typings
1374 ]
1375
1376 if (userId) {
1377 scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings
1378 }
1379
1380 return VideoModel
1381 .scope(scopes)
1382 .findOne(options)
1383 }
1384
1385 static async getStats () {
1386 const totalLocalVideos = await VideoModel.count({
1387 where: {
1388 remote: false
1389 }
1390 })
1391 const totalVideos = await VideoModel.count()
1392
1393 let totalLocalVideoViews = await VideoModel.sum('views', {
1394 where: {
1395 remote: false
1396 }
1397 })
1398 // Sequelize could return null...
1399 if (!totalLocalVideoViews) totalLocalVideoViews = 0
1400
1401 return {
1402 totalLocalVideos,
1403 totalLocalVideoViews,
1404 totalVideos
1405 }
1406 }
1407
1408 static incrementViews (id: number, views: number) {
1409 return VideoModel.increment('views', {
1410 by: views,
1411 where: {
1412 id
1413 }
1414 })
1415 }
1416
1417 static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) {
1418 // Instances only share videos
1419 const query = 'SELECT 1 FROM "videoShare" ' +
1420 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
1421 'WHERE "actorFollow"."actorId" = $followerActorId AND "videoShare"."videoId" = $videoId ' +
1422 'LIMIT 1'
1423
1424 const options = {
1425 type: Sequelize.QueryTypes.SELECT,
1426 bind: { followerActorId, videoId },
1427 raw: true
1428 }
1429
1430 return VideoModel.sequelize.query(query, options)
1431 .then(results => results.length === 1)
1432 }
1433
1434 // threshold corresponds to how many video the field should have to be returned
1435 static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
1436 const serverActor = await getServerActor()
1437 const followerActorId = serverActor.id
1438
1439 const scopeOptions: AvailableForListIDsOptions = {
1440 serverAccountId: serverActor.Account.id,
1441 followerActorId,
1442 includeLocalVideos: true
1443 }
1444
1445 const query: IFindOptions<VideoModel> = {
1446 attributes: [ field ],
1447 limit: count,
1448 group: field,
1449 having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), {
1450 [ Sequelize.Op.gte ]: threshold
1451 }) as any, // FIXME: typings
1452 order: [ this.sequelize.random() ]
1453 }
1454
1455 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] })
1456 .findAll(query)
1457 .then(rows => rows.map(r => r[ field ]))
1458 }
1459
1460 static buildTrendingQuery (trendingDays: number) {
1461 return {
1462 attributes: [],
1463 subQuery: false,
1464 model: VideoViewModel,
1465 required: false,
1466 where: {
1467 startDate: {
1468 [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
1469 }
1470 }
1471 }
1472 }
1473
1474 private static buildActorWhereWithFilter (filter?: VideoFilter) {
1475 if (filter && (filter === 'local' || filter === 'all-local')) {
1476 return {
1477 serverId: null
1478 }
1479 }
1480
1481 return {}
1482 }
1483
1484 private static async getAvailableForApi (
1485 query: IFindOptions<VideoModel>,
1486 options: AvailableForListIDsOptions,
1487 countVideos = true
1488 ) {
1489 const idsScope = {
1490 method: [
1491 ScopeNames.AVAILABLE_FOR_LIST_IDS, options
1492 ]
1493 }
1494
1495 // Remove trending sort on count, because it uses a group by
1496 const countOptions = Object.assign({}, options, { trendingDays: undefined })
1497 const countQuery = Object.assign({}, query, { attributes: undefined, group: undefined })
1498 const countScope = {
1499 method: [
1500 ScopeNames.AVAILABLE_FOR_LIST_IDS, countOptions
1501 ]
1502 }
1503
1504 const [ count, rowsId ] = await Promise.all([
1505 countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve<number>(undefined),
1506 VideoModel.scope(idsScope).findAll(query)
1507 ])
1508 const ids = rowsId.map(r => r.id)
1509
1510 if (ids.length === 0) return { data: [], total: count }
1511
1512 // FIXME: typings
1513 const apiScope: any[] = [
1514 {
1515 method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ]
1516 }
1517 ]
1518
1519 if (options.user) {
1520 apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
1521 }
1522
1523 const secondQuery = {
1524 offset: 0,
1525 limit: query.limit,
1526 attributes: query.attributes,
1527 order: [ // Keep original order
1528 Sequelize.literal(
1529 ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ')
1530 )
1531 ]
1532 }
1533 const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
1534
1535 return {
1536 data: rows,
1537 total: count
1538 }
1539 }
1540
1541 static getCategoryLabel (id: number) {
1542 return VIDEO_CATEGORIES[ id ] || 'Misc'
1543 }
1544
1545 static getLicenceLabel (id: number) {
1546 return VIDEO_LICENCES[ id ] || 'Unknown'
1547 }
1548
1549 static getLanguageLabel (id: string) {
1550 return VIDEO_LANGUAGES[ id ] || 'Unknown'
1551 }
1552
1553 static getPrivacyLabel (id: number) {
1554 return VIDEO_PRIVACIES[ id ] || 'Unknown'
1555 }
1556
1557 static getStateLabel (id: number) {
1558 return VIDEO_STATES[ id ] || 'Unknown'
1559 }
1560
1561 static buildWhereIdOrUUID (id: number | string) {
1562 return validator.isInt('' + id) ? { id } : { uuid: id }
1563 }
1564
1565 getOriginalFile () {
1566 if (Array.isArray(this.VideoFiles) === false) return undefined
1567
1568 // The original file is the file that have the higher resolution
1569 return maxBy(this.VideoFiles, file => file.resolution)
1570 }
1571
1572 getVideoFilename (videoFile: VideoFileModel) {
1573 return this.uuid + '-' + videoFile.resolution + videoFile.extname
1574 }
1575
1576 getThumbnailName () {
1577 // We always have a copy of the thumbnail
1578 const extension = '.jpg'
1579 return this.uuid + extension
1580 }
1581
1582 getPreviewName () {
1583 const extension = '.jpg'
1584 return this.uuid + extension
1585 }
1586
1587 getTorrentFileName (videoFile: VideoFileModel) {
1588 const extension = '.torrent'
1589 return this.uuid + '-' + videoFile.resolution + extension
1590 }
1591
1592 isOwned () {
1593 return this.remote === false
1594 }
1595
1596 createPreview (videoFile: VideoFileModel) {
1597 return generateImageFromVideoFile(
1598 this.getVideoFilePath(videoFile),
1599 CONFIG.STORAGE.PREVIEWS_DIR,
1600 this.getPreviewName(),
1601 PREVIEWS_SIZE
1602 )
1603 }
1604
1605 createThumbnail (videoFile: VideoFileModel) {
1606 return generateImageFromVideoFile(
1607 this.getVideoFilePath(videoFile),
1608 CONFIG.STORAGE.THUMBNAILS_DIR,
1609 this.getThumbnailName(),
1610 THUMBNAILS_SIZE
1611 )
1612 }
1613
1614 getTorrentFilePath (videoFile: VideoFileModel) {
1615 return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1616 }
1617
1618 getVideoFilePath (videoFile: VideoFileModel) {
1619 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1620 }
1621
1622 async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
1623 const options = {
1624 // Keep the extname, it's used by the client to stream the file inside a web browser
1625 name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
1626 createdBy: 'PeerTube',
1627 announceList: [
1628 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
1629 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
1630 ],
1631 urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
1632 }
1633
1634 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1635
1636 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1637 logger.info('Creating torrent %s.', filePath)
1638
1639 await writeFile(filePath, torrent)
1640
1641 const parsedTorrent = parseTorrent(torrent)
1642 videoFile.infoHash = parsedTorrent.infoHash
1643 }
1644
1645 getWatchStaticPath () {
1646 return '/videos/watch/' + this.uuid
1647 }
1648
1649 getEmbedStaticPath () {
1650 return '/videos/embed/' + this.uuid
1651 }
1652
1653 getThumbnailStaticPath () {
1654 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1655 }
1656
1657 getPreviewStaticPath () {
1658 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1659 }
1660
1661 toFormattedJSON (options?: VideoFormattingJSONOptions): Video {
1662 return videoModelToFormattedJSON(this, options)
1663 }
1664
1665 toFormattedDetailsJSON (): VideoDetails {
1666 return videoModelToFormattedDetailsJSON(this)
1667 }
1668
1669 getFormattedVideoFilesJSON (): VideoFile[] {
1670 return videoFilesModelToFormattedJSON(this, this.VideoFiles)
1671 }
1672
1673 toActivityPubObject (): VideoTorrentObject {
1674 return videoModelToActivityPubObject(this)
1675 }
1676
1677 getTruncatedDescription () {
1678 if (!this.description) return null
1679
1680 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1681 return peertubeTruncate(this.description, maxLength)
1682 }
1683
1684 getOriginalFileResolution () {
1685 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1686
1687 return getVideoFileResolution(originalFilePath)
1688 }
1689
1690 getDescriptionAPIPath () {
1691 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1692 }
1693
1694 removeThumbnail () {
1695 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1696 return remove(thumbnailPath)
1697 .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err }))
1698 }
1699
1700 removePreview () {
1701 const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1702 return remove(previewPath)
1703 .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err }))
1704 }
1705
1706 removeFile (videoFile: VideoFileModel, isRedundancy = false) {
1707 const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
1708
1709 const filePath = join(baseDir, this.getVideoFilename(videoFile))
1710 return remove(filePath)
1711 .catch(err => logger.warn('Cannot delete file %s.', filePath, { err }))
1712 }
1713
1714 removeTorrent (videoFile: VideoFileModel) {
1715 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1716 return remove(torrentPath)
1717 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
1718 }
1719
1720 removeStreamingPlaylist (isRedundancy = false) {
1721 const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_PLAYLIST_DIRECTORY
1722
1723 const filePath = join(baseDir, this.uuid)
1724 return remove(filePath)
1725 .catch(err => logger.warn('Cannot delete playlist directory %s.', filePath, { err }))
1726 }
1727
1728 isOutdated () {
1729 if (this.isOwned()) return false
1730
1731 const now = Date.now()
1732 const createdAtTime = this.createdAt.getTime()
1733 const updatedAtTime = this.updatedAt.getTime()
1734
1735 return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL &&
1736 (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
1737 }
1738
1739 setAsRefreshed () {
1740 this.changed('updatedAt', true)
1741
1742 return this.save()
1743 }
1744
1745 getBaseUrls () {
1746 let baseUrlHttp
1747 let baseUrlWs
1748
1749 if (this.isOwned()) {
1750 baseUrlHttp = CONFIG.WEBSERVER.URL
1751 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1752 } else {
1753 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1754 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1755 }
1756
1757 return { baseUrlHttp, baseUrlWs }
1758 }
1759
1760 generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1761 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1762 const announce = this.getTrackerUrls(baseUrlHttp, baseUrlWs)
1763 let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1764
1765 const redundancies = videoFile.RedundancyVideos
1766 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
1767
1768 const magnetHash = {
1769 xs,
1770 announce,
1771 urlList,
1772 infoHash: videoFile.infoHash,
1773 name: this.name
1774 }
1775
1776 return magnetUtil.encode(magnetHash)
1777 }
1778
1779 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
1780 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1781 }
1782
1783 getThumbnailUrl (baseUrlHttp: string) {
1784 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1785 }
1786
1787 getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1788 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1789 }
1790
1791 getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1792 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1793 }
1794
1795 getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1796 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1797 }
1798
1799 getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1800 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile)
1801 }
1802
1803 getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1804 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
1805 }
1806
1807 getBandwidthBits (videoFile: VideoFileModel) {
1808 return Math.ceil((videoFile.size * 8) / this.duration)
1809 }
1810 }