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