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