]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video.ts
Trending by interval
[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 { 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 { 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
94 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
95 const indexes: Sequelize.DefineIndexesOptions[] = [
96 buildTrigramSearchIndex('video_name_trigram', 'name'),
97
98 { fields: [ 'createdAt' ] },
99 { fields: [ 'publishedAt' ] },
100 { fields: [ 'duration' ] },
101 { fields: [ 'category' ] },
102 { fields: [ 'licence' ] },
103 { fields: [ 'nsfw' ] },
104 { fields: [ 'language' ] },
105 { fields: [ 'waitTranscoding' ] },
106 { fields: [ 'state' ] },
107 { fields: [ 'remote' ] },
108 { fields: [ 'views' ] },
109 { fields: [ 'likes' ] },
110 { fields: [ 'channelId' ] },
111 {
112 fields: [ 'uuid' ],
113 unique: true
114 },
115 {
116 fields: [ 'url'],
117 unique: true
118 }
119 ]
120
121 export enum ScopeNames {
122 AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
123 FOR_API = 'FOR_API',
124 WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
125 WITH_TAGS = 'WITH_TAGS',
126 WITH_FILES = 'WITH_FILES',
127 WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE',
128 WITH_BLACKLISTED = 'WITH_BLACKLISTED'
129 }
130
131 type ForAPIOptions = {
132 ids: number[]
133 withFiles?: boolean
134 }
135
136 type AvailableForListIDsOptions = {
137 actorId: number
138 includeLocalVideos: boolean
139 filter?: VideoFilter
140 categoryOneOf?: number[]
141 nsfw?: boolean
142 licenceOneOf?: number[]
143 languageOneOf?: string[]
144 tagsOneOf?: string[]
145 tagsAllOf?: string[]
146 withFiles?: boolean
147 accountId?: number
148 videoChannelId?: number
149 trendingDays?: number
150 }
151
152 @Scopes({
153 [ScopeNames.FOR_API]: (options: ForAPIOptions) => {
154 const accountInclude = {
155 attributes: [ 'id', 'name' ],
156 model: AccountModel.unscoped(),
157 required: true,
158 include: [
159 {
160 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
161 model: ActorModel.unscoped(),
162 required: true,
163 include: [
164 {
165 attributes: [ 'host' ],
166 model: ServerModel.unscoped(),
167 required: false
168 },
169 {
170 model: AvatarModel.unscoped(),
171 required: false
172 }
173 ]
174 }
175 ]
176 }
177
178 const videoChannelInclude = {
179 attributes: [ 'name', 'description', 'id' ],
180 model: VideoChannelModel.unscoped(),
181 required: true,
182 include: [
183 {
184 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
185 model: ActorModel.unscoped(),
186 required: true,
187 include: [
188 {
189 attributes: [ 'host' ],
190 model: ServerModel.unscoped(),
191 required: false
192 },
193 {
194 model: AvatarModel.unscoped(),
195 required: false
196 }
197 ]
198 },
199 accountInclude
200 ]
201 }
202
203 const query: IFindOptions<VideoModel> = {
204 where: {
205 id: {
206 [Sequelize.Op.any]: options.ids
207 }
208 },
209 include: [ videoChannelInclude ]
210 }
211
212 if (options.withFiles === true) {
213 query.include.push({
214 model: VideoFileModel.unscoped(),
215 required: true
216 })
217 }
218
219 return query
220 },
221 [ScopeNames.AVAILABLE_FOR_LIST_IDS]: (options: AvailableForListIDsOptions) => {
222 const query: IFindOptions<VideoModel> = {
223 attributes: [ 'id' ],
224 where: {
225 id: {
226 [Sequelize.Op.and]: [
227 {
228 [ Sequelize.Op.notIn ]: Sequelize.literal(
229 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
230 )
231 }
232 ]
233 },
234 // Always list public videos
235 privacy: VideoPrivacy.PUBLIC,
236 // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
237 [ Sequelize.Op.or ]: [
238 {
239 state: VideoState.PUBLISHED
240 },
241 {
242 [ Sequelize.Op.and ]: {
243 state: VideoState.TO_TRANSCODE,
244 waitTranscoding: false
245 }
246 }
247 ]
248 },
249 include: [ ]
250 }
251
252 if (options.filter || options.accountId || options.videoChannelId) {
253 const videoChannelInclude: IIncludeOptions = {
254 attributes: [],
255 model: VideoChannelModel.unscoped(),
256 required: true
257 }
258
259 if (options.videoChannelId) {
260 videoChannelInclude.where = {
261 id: options.videoChannelId
262 }
263 }
264
265 if (options.filter || options.accountId) {
266 const accountInclude: IIncludeOptions = {
267 attributes: [],
268 model: AccountModel.unscoped(),
269 required: true
270 }
271
272 if (options.filter) {
273 accountInclude.include = [
274 {
275 attributes: [],
276 model: ActorModel.unscoped(),
277 required: true,
278 where: VideoModel.buildActorWhereWithFilter(options.filter)
279 }
280 ]
281 }
282
283 if (options.accountId) {
284 accountInclude.where = { id: options.accountId }
285 }
286
287 videoChannelInclude.include = [ accountInclude ]
288 }
289
290 query.include.push(videoChannelInclude)
291 }
292
293 if (options.actorId) {
294 let localVideosReq = ''
295 if (options.includeLocalVideos === true) {
296 localVideosReq = ' UNION ALL ' +
297 'SELECT "video"."id" AS "id" FROM "video" ' +
298 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
299 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
300 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
301 'WHERE "actor"."serverId" IS NULL'
302 }
303
304 // Force actorId to be a number to avoid SQL injections
305 const actorIdNumber = parseInt(options.actorId.toString(), 10)
306 query.where['id'][Sequelize.Op.and].push({
307 [ Sequelize.Op.in ]: Sequelize.literal(
308 '(' +
309 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
310 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
311 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
312 ' UNION ALL ' +
313 'SELECT "video"."id" AS "id" FROM "video" ' +
314 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
315 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
316 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
317 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
318 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
319 localVideosReq +
320 ')'
321 )
322 })
323 }
324
325 if (options.withFiles === true) {
326 query.where['id'][Sequelize.Op.and].push({
327 [ Sequelize.Op.in ]: Sequelize.literal(
328 '(SELECT "videoId" FROM "videoFile")'
329 )
330 })
331 }
332
333 // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
334 if (options.tagsAllOf || options.tagsOneOf) {
335 const createTagsIn = (tags: string[]) => {
336 return tags.map(t => VideoModel.sequelize.escape(t))
337 .join(', ')
338 }
339
340 if (options.tagsOneOf) {
341 query.where['id'][Sequelize.Op.and].push({
342 [Sequelize.Op.in]: Sequelize.literal(
343 '(' +
344 'SELECT "videoId" FROM "videoTag" ' +
345 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
346 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
347 ')'
348 )
349 })
350 }
351
352 if (options.tagsAllOf) {
353 query.where['id'][Sequelize.Op.and].push({
354 [Sequelize.Op.in]: Sequelize.literal(
355 '(' +
356 'SELECT "videoId" FROM "videoTag" ' +
357 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
358 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
359 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
360 ')'
361 )
362 })
363 }
364 }
365
366 if (options.nsfw === true || options.nsfw === false) {
367 query.where['nsfw'] = options.nsfw
368 }
369
370 if (options.categoryOneOf) {
371 query.where['category'] = {
372 [Sequelize.Op.or]: options.categoryOneOf
373 }
374 }
375
376 if (options.licenceOneOf) {
377 query.where['licence'] = {
378 [Sequelize.Op.or]: options.licenceOneOf
379 }
380 }
381
382 if (options.languageOneOf) {
383 query.where['language'] = {
384 [Sequelize.Op.or]: options.languageOneOf
385 }
386 }
387
388 if (options.trendingDays) {
389 query.include.push({
390 attributes: [],
391 model: VideoViewModel,
392 required: false,
393 where: {
394 startDate: {
395 [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays)
396 }
397 }
398 })
399
400 query.subQuery = false
401 }
402
403 return query
404 },
405 [ScopeNames.WITH_ACCOUNT_DETAILS]: {
406 include: [
407 {
408 model: () => VideoChannelModel.unscoped(),
409 required: true,
410 include: [
411 {
412 attributes: {
413 exclude: [ 'privateKey', 'publicKey' ]
414 },
415 model: () => ActorModel.unscoped(),
416 required: true,
417 include: [
418 {
419 attributes: [ 'host' ],
420 model: () => ServerModel.unscoped(),
421 required: false
422 },
423 {
424 model: () => AvatarModel.unscoped(),
425 required: false
426 }
427 ]
428 },
429 {
430 model: () => AccountModel.unscoped(),
431 required: true,
432 include: [
433 {
434 model: () => ActorModel.unscoped(),
435 attributes: {
436 exclude: [ 'privateKey', 'publicKey' ]
437 },
438 required: true,
439 include: [
440 {
441 attributes: [ 'host' ],
442 model: () => ServerModel.unscoped(),
443 required: false
444 },
445 {
446 model: () => AvatarModel.unscoped(),
447 required: false
448 }
449 ]
450 }
451 ]
452 }
453 ]
454 }
455 ]
456 },
457 [ScopeNames.WITH_TAGS]: {
458 include: [ () => TagModel ]
459 },
460 [ScopeNames.WITH_BLACKLISTED]: {
461 include: [
462 {
463 attributes: [ 'id', 'reason' ],
464 model: () => VideoBlacklistModel,
465 required: false
466 }
467 ]
468 },
469 [ScopeNames.WITH_FILES]: {
470 include: [
471 {
472 model: () => VideoFileModel.unscoped(),
473 required: false
474 }
475 ]
476 },
477 [ScopeNames.WITH_SCHEDULED_UPDATE]: {
478 include: [
479 {
480 model: () => ScheduleVideoUpdateModel.unscoped(),
481 required: false
482 }
483 ]
484 }
485 })
486 @Table({
487 tableName: 'video',
488 indexes
489 })
490 export class VideoModel extends Model<VideoModel> {
491
492 @AllowNull(false)
493 @Default(DataType.UUIDV4)
494 @IsUUID(4)
495 @Column(DataType.UUID)
496 uuid: string
497
498 @AllowNull(false)
499 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
500 @Column
501 name: string
502
503 @AllowNull(true)
504 @Default(null)
505 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
506 @Column
507 category: number
508
509 @AllowNull(true)
510 @Default(null)
511 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
512 @Column
513 licence: number
514
515 @AllowNull(true)
516 @Default(null)
517 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
518 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
519 language: string
520
521 @AllowNull(false)
522 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
523 @Column
524 privacy: number
525
526 @AllowNull(false)
527 @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
528 @Column
529 nsfw: boolean
530
531 @AllowNull(true)
532 @Default(null)
533 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
534 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
535 description: string
536
537 @AllowNull(true)
538 @Default(null)
539 @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
540 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
541 support: string
542
543 @AllowNull(false)
544 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
545 @Column
546 duration: number
547
548 @AllowNull(false)
549 @Default(0)
550 @IsInt
551 @Min(0)
552 @Column
553 views: number
554
555 @AllowNull(false)
556 @Default(0)
557 @IsInt
558 @Min(0)
559 @Column
560 likes: number
561
562 @AllowNull(false)
563 @Default(0)
564 @IsInt
565 @Min(0)
566 @Column
567 dislikes: number
568
569 @AllowNull(false)
570 @Column
571 remote: boolean
572
573 @AllowNull(false)
574 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
575 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
576 url: string
577
578 @AllowNull(false)
579 @Column
580 commentsEnabled: boolean
581
582 @AllowNull(false)
583 @Column
584 waitTranscoding: boolean
585
586 @AllowNull(false)
587 @Default(null)
588 @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
589 @Column
590 state: VideoState
591
592 @CreatedAt
593 createdAt: Date
594
595 @UpdatedAt
596 updatedAt: Date
597
598 @AllowNull(false)
599 @Default(Sequelize.NOW)
600 @Column
601 publishedAt: Date
602
603 @ForeignKey(() => VideoChannelModel)
604 @Column
605 channelId: number
606
607 @BelongsTo(() => VideoChannelModel, {
608 foreignKey: {
609 allowNull: true
610 },
611 hooks: true
612 })
613 VideoChannel: VideoChannelModel
614
615 @BelongsToMany(() => TagModel, {
616 foreignKey: 'videoId',
617 through: () => VideoTagModel,
618 onDelete: 'CASCADE'
619 })
620 Tags: TagModel[]
621
622 @HasMany(() => VideoAbuseModel, {
623 foreignKey: {
624 name: 'videoId',
625 allowNull: false
626 },
627 onDelete: 'cascade'
628 })
629 VideoAbuses: VideoAbuseModel[]
630
631 @HasMany(() => VideoFileModel, {
632 foreignKey: {
633 name: 'videoId',
634 allowNull: false
635 },
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 }) {
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)
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 const options = t ? { transaction: t } : undefined
1067
1068 return VideoModel.findById(id, options)
1069 }
1070
1071 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
1072 const query: IFindOptions<VideoModel> = {
1073 where: {
1074 url
1075 }
1076 }
1077
1078 if (t !== undefined) query.transaction = t
1079
1080 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
1081 }
1082
1083 static loadAndPopulateAccountAndServerAndTags (id: number) {
1084 const options = {
1085 order: [ [ 'Tags', 'name', 'ASC' ] ]
1086 }
1087
1088 return VideoModel
1089 .scope([
1090 ScopeNames.WITH_TAGS,
1091 ScopeNames.WITH_BLACKLISTED,
1092 ScopeNames.WITH_FILES,
1093 ScopeNames.WITH_ACCOUNT_DETAILS,
1094 ScopeNames.WITH_SCHEDULED_UPDATE
1095 ])
1096 .findById(id, options)
1097 }
1098
1099 static loadByUUID (uuid: string) {
1100 const options = {
1101 where: {
1102 uuid
1103 }
1104 }
1105
1106 return VideoModel
1107 .scope([ ScopeNames.WITH_FILES ])
1108 .findOne(options)
1109 }
1110
1111 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string, t?: Sequelize.Transaction) {
1112 const options = {
1113 order: [ [ 'Tags', 'name', 'ASC' ] ],
1114 where: {
1115 uuid
1116 },
1117 transaction: t
1118 }
1119
1120 return VideoModel
1121 .scope([
1122 ScopeNames.WITH_TAGS,
1123 ScopeNames.WITH_BLACKLISTED,
1124 ScopeNames.WITH_FILES,
1125 ScopeNames.WITH_ACCOUNT_DETAILS,
1126 ScopeNames.WITH_SCHEDULED_UPDATE
1127 ])
1128 .findOne(options)
1129 }
1130
1131 static async getStats () {
1132 const totalLocalVideos = await VideoModel.count({
1133 where: {
1134 remote: false
1135 }
1136 })
1137 const totalVideos = await VideoModel.count()
1138
1139 let totalLocalVideoViews = await VideoModel.sum('views', {
1140 where: {
1141 remote: false
1142 }
1143 })
1144 // Sequelize could return null...
1145 if (!totalLocalVideoViews) totalLocalVideoViews = 0
1146
1147 return {
1148 totalLocalVideos,
1149 totalLocalVideoViews,
1150 totalVideos
1151 }
1152 }
1153
1154 static incrementViews (id: number, views: number) {
1155 return VideoModel.increment('views', {
1156 by: views,
1157 where: {
1158 id
1159 }
1160 })
1161 }
1162
1163 // threshold corresponds to how many video the field should have to be returned
1164 static getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
1165 const query: IFindOptions<VideoModel> = {
1166 attributes: [ field ],
1167 limit: count,
1168 group: field,
1169 having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), {
1170 [Sequelize.Op.gte]: threshold
1171 }) as any, // FIXME: typings
1172 where: {
1173 [field]: {
1174 [Sequelize.Op.not]: null
1175 },
1176 privacy: VideoPrivacy.PUBLIC,
1177 state: VideoState.PUBLISHED
1178 },
1179 order: [ this.sequelize.random() ]
1180 }
1181
1182 return VideoModel.findAll(query)
1183 .then(rows => rows.map(r => r[field]))
1184 }
1185
1186 private static buildActorWhereWithFilter (filter?: VideoFilter) {
1187 if (filter && filter === 'local') {
1188 return {
1189 serverId: null
1190 }
1191 }
1192
1193 return {}
1194 }
1195
1196 private static async getAvailableForApi (query: IFindOptions<VideoModel>, options: AvailableForListIDsOptions) {
1197 const idsScope = {
1198 method: [
1199 ScopeNames.AVAILABLE_FOR_LIST_IDS, options
1200 ]
1201 }
1202
1203 const { count, rows: rowsId } = await VideoModel.scope(idsScope).findAndCountAll(query)
1204 const ids = rowsId.map(r => r.id)
1205
1206 if (ids.length === 0) return { data: [], total: count }
1207
1208 const apiScope = {
1209 method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ]
1210 }
1211
1212 const secondQuery = {
1213 offset: 0,
1214 limit: query.limit,
1215 attributes: query.attributes,
1216 order: [ // Keep original order
1217 Sequelize.literal(
1218 ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ')
1219 )
1220 ]
1221 }
1222 const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
1223
1224 return {
1225 data: rows,
1226 total: count
1227 }
1228 }
1229
1230 private static getCategoryLabel (id: number) {
1231 return VIDEO_CATEGORIES[id] || 'Misc'
1232 }
1233
1234 private static getLicenceLabel (id: number) {
1235 return VIDEO_LICENCES[id] || 'Unknown'
1236 }
1237
1238 private static getLanguageLabel (id: string) {
1239 return VIDEO_LANGUAGES[id] || 'Unknown'
1240 }
1241
1242 private static getPrivacyLabel (id: number) {
1243 return VIDEO_PRIVACIES[id] || 'Unknown'
1244 }
1245
1246 private static getStateLabel (id: number) {
1247 return VIDEO_STATES[id] || 'Unknown'
1248 }
1249
1250 getOriginalFile () {
1251 if (Array.isArray(this.VideoFiles) === false) return undefined
1252
1253 // The original file is the file that have the higher resolution
1254 return maxBy(this.VideoFiles, file => file.resolution)
1255 }
1256
1257 getVideoFilename (videoFile: VideoFileModel) {
1258 return this.uuid + '-' + videoFile.resolution + videoFile.extname
1259 }
1260
1261 getThumbnailName () {
1262 // We always have a copy of the thumbnail
1263 const extension = '.jpg'
1264 return this.uuid + extension
1265 }
1266
1267 getPreviewName () {
1268 const extension = '.jpg'
1269 return this.uuid + extension
1270 }
1271
1272 getTorrentFileName (videoFile: VideoFileModel) {
1273 const extension = '.torrent'
1274 return this.uuid + '-' + videoFile.resolution + extension
1275 }
1276
1277 isOwned () {
1278 return this.remote === false
1279 }
1280
1281 createPreview (videoFile: VideoFileModel) {
1282 return generateImageFromVideoFile(
1283 this.getVideoFilePath(videoFile),
1284 CONFIG.STORAGE.PREVIEWS_DIR,
1285 this.getPreviewName(),
1286 PREVIEWS_SIZE
1287 )
1288 }
1289
1290 createThumbnail (videoFile: VideoFileModel) {
1291 return generateImageFromVideoFile(
1292 this.getVideoFilePath(videoFile),
1293 CONFIG.STORAGE.THUMBNAILS_DIR,
1294 this.getThumbnailName(),
1295 THUMBNAILS_SIZE
1296 )
1297 }
1298
1299 getTorrentFilePath (videoFile: VideoFileModel) {
1300 return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1301 }
1302
1303 getVideoFilePath (videoFile: VideoFileModel) {
1304 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1305 }
1306
1307 async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
1308 const options = {
1309 // Keep the extname, it's used by the client to stream the file inside a web browser
1310 name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
1311 createdBy: 'PeerTube',
1312 announceList: [
1313 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
1314 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
1315 ],
1316 urlList: [
1317 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1318 ]
1319 }
1320
1321 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1322
1323 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1324 logger.info('Creating torrent %s.', filePath)
1325
1326 await writeFile(filePath, torrent)
1327
1328 const parsedTorrent = parseTorrent(torrent)
1329 videoFile.infoHash = parsedTorrent.infoHash
1330 }
1331
1332 getEmbedStaticPath () {
1333 return '/videos/embed/' + this.uuid
1334 }
1335
1336 getThumbnailStaticPath () {
1337 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1338 }
1339
1340 getPreviewStaticPath () {
1341 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1342 }
1343
1344 toFormattedJSON (options?: {
1345 additionalAttributes: {
1346 state?: boolean,
1347 waitTranscoding?: boolean,
1348 scheduledUpdate?: boolean,
1349 blacklistInfo?: boolean
1350 }
1351 }): Video {
1352 const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
1353 const formattedVideoChannel = this.VideoChannel.toFormattedJSON()
1354
1355 const videoObject: Video = {
1356 id: this.id,
1357 uuid: this.uuid,
1358 name: this.name,
1359 category: {
1360 id: this.category,
1361 label: VideoModel.getCategoryLabel(this.category)
1362 },
1363 licence: {
1364 id: this.licence,
1365 label: VideoModel.getLicenceLabel(this.licence)
1366 },
1367 language: {
1368 id: this.language,
1369 label: VideoModel.getLanguageLabel(this.language)
1370 },
1371 privacy: {
1372 id: this.privacy,
1373 label: VideoModel.getPrivacyLabel(this.privacy)
1374 },
1375 nsfw: this.nsfw,
1376 description: this.getTruncatedDescription(),
1377 isLocal: this.isOwned(),
1378 duration: this.duration,
1379 views: this.views,
1380 likes: this.likes,
1381 dislikes: this.dislikes,
1382 thumbnailPath: this.getThumbnailStaticPath(),
1383 previewPath: this.getPreviewStaticPath(),
1384 embedPath: this.getEmbedStaticPath(),
1385 createdAt: this.createdAt,
1386 updatedAt: this.updatedAt,
1387 publishedAt: this.publishedAt,
1388 account: {
1389 id: formattedAccount.id,
1390 uuid: formattedAccount.uuid,
1391 name: formattedAccount.name,
1392 displayName: formattedAccount.displayName,
1393 url: formattedAccount.url,
1394 host: formattedAccount.host,
1395 avatar: formattedAccount.avatar
1396 },
1397 channel: {
1398 id: formattedVideoChannel.id,
1399 uuid: formattedVideoChannel.uuid,
1400 name: formattedVideoChannel.name,
1401 displayName: formattedVideoChannel.displayName,
1402 url: formattedVideoChannel.url,
1403 host: formattedVideoChannel.host,
1404 avatar: formattedVideoChannel.avatar
1405 }
1406 }
1407
1408 if (options) {
1409 if (options.additionalAttributes.state === true) {
1410 videoObject.state = {
1411 id: this.state,
1412 label: VideoModel.getStateLabel(this.state)
1413 }
1414 }
1415
1416 if (options.additionalAttributes.waitTranscoding === true) {
1417 videoObject.waitTranscoding = this.waitTranscoding
1418 }
1419
1420 if (options.additionalAttributes.scheduledUpdate === true && this.ScheduleVideoUpdate) {
1421 videoObject.scheduledUpdate = {
1422 updateAt: this.ScheduleVideoUpdate.updateAt,
1423 privacy: this.ScheduleVideoUpdate.privacy || undefined
1424 }
1425 }
1426
1427 if (options.additionalAttributes.blacklistInfo === true) {
1428 videoObject.blacklisted = !!this.VideoBlacklist
1429 videoObject.blacklistedReason = this.VideoBlacklist ? this.VideoBlacklist.reason : null
1430 }
1431 }
1432
1433 return videoObject
1434 }
1435
1436 toFormattedDetailsJSON (): VideoDetails {
1437 const formattedJson = this.toFormattedJSON({
1438 additionalAttributes: {
1439 scheduledUpdate: true,
1440 blacklistInfo: true
1441 }
1442 })
1443
1444 const detailsJson = {
1445 support: this.support,
1446 descriptionPath: this.getDescriptionPath(),
1447 channel: this.VideoChannel.toFormattedJSON(),
1448 account: this.VideoChannel.Account.toFormattedJSON(),
1449 tags: map(this.Tags, 'name'),
1450 commentsEnabled: this.commentsEnabled,
1451 waitTranscoding: this.waitTranscoding,
1452 state: {
1453 id: this.state,
1454 label: VideoModel.getStateLabel(this.state)
1455 },
1456 files: []
1457 }
1458
1459 // Format and sort video files
1460 detailsJson.files = this.getFormattedVideoFilesJSON()
1461
1462 return Object.assign(formattedJson, detailsJson)
1463 }
1464
1465 getFormattedVideoFilesJSON (): VideoFile[] {
1466 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1467
1468 return this.VideoFiles
1469 .map(videoFile => {
1470 let resolutionLabel = videoFile.resolution + 'p'
1471
1472 return {
1473 resolution: {
1474 id: videoFile.resolution,
1475 label: resolutionLabel
1476 },
1477 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
1478 size: videoFile.size,
1479 fps: videoFile.fps,
1480 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
1481 torrentDownloadUrl: this.getTorrentDownloadUrl(videoFile, baseUrlHttp),
1482 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp),
1483 fileDownloadUrl: this.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
1484 } as VideoFile
1485 })
1486 .sort((a, b) => {
1487 if (a.resolution.id < b.resolution.id) return 1
1488 if (a.resolution.id === b.resolution.id) return 0
1489 return -1
1490 })
1491 }
1492
1493 toActivityPubObject (): VideoTorrentObject {
1494 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1495 if (!this.Tags) this.Tags = []
1496
1497 const tag = this.Tags.map(t => ({
1498 type: 'Hashtag' as 'Hashtag',
1499 name: t.name
1500 }))
1501
1502 let language
1503 if (this.language) {
1504 language = {
1505 identifier: this.language,
1506 name: VideoModel.getLanguageLabel(this.language)
1507 }
1508 }
1509
1510 let category
1511 if (this.category) {
1512 category = {
1513 identifier: this.category + '',
1514 name: VideoModel.getCategoryLabel(this.category)
1515 }
1516 }
1517
1518 let licence
1519 if (this.licence) {
1520 licence = {
1521 identifier: this.licence + '',
1522 name: VideoModel.getLicenceLabel(this.licence)
1523 }
1524 }
1525
1526 const url = []
1527 for (const file of this.VideoFiles) {
1528 url.push({
1529 type: 'Link',
1530 mimeType: VIDEO_EXT_MIMETYPE[file.extname],
1531 href: this.getVideoFileUrl(file, baseUrlHttp),
1532 height: file.resolution,
1533 size: file.size,
1534 fps: file.fps
1535 })
1536
1537 url.push({
1538 type: 'Link',
1539 mimeType: 'application/x-bittorrent',
1540 href: this.getTorrentUrl(file, baseUrlHttp),
1541 height: file.resolution
1542 })
1543
1544 url.push({
1545 type: 'Link',
1546 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
1547 href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
1548 height: file.resolution
1549 })
1550 }
1551
1552 // Add video url too
1553 url.push({
1554 type: 'Link',
1555 mimeType: 'text/html',
1556 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
1557 })
1558
1559 const subtitleLanguage = []
1560 for (const caption of this.VideoCaptions) {
1561 subtitleLanguage.push({
1562 identifier: caption.language,
1563 name: VideoCaptionModel.getLanguageLabel(caption.language)
1564 })
1565 }
1566
1567 return {
1568 type: 'Video' as 'Video',
1569 id: this.url,
1570 name: this.name,
1571 duration: this.getActivityStreamDuration(),
1572 uuid: this.uuid,
1573 tag,
1574 category,
1575 licence,
1576 language,
1577 views: this.views,
1578 sensitive: this.nsfw,
1579 waitTranscoding: this.waitTranscoding,
1580 state: this.state,
1581 commentsEnabled: this.commentsEnabled,
1582 published: this.publishedAt.toISOString(),
1583 updated: this.updatedAt.toISOString(),
1584 mediaType: 'text/markdown',
1585 content: this.getTruncatedDescription(),
1586 support: this.support,
1587 subtitleLanguage,
1588 icon: {
1589 type: 'Image',
1590 url: this.getThumbnailUrl(baseUrlHttp),
1591 mediaType: 'image/jpeg',
1592 width: THUMBNAILS_SIZE.width,
1593 height: THUMBNAILS_SIZE.height
1594 },
1595 url,
1596 likes: getVideoLikesActivityPubUrl(this),
1597 dislikes: getVideoDislikesActivityPubUrl(this),
1598 shares: getVideoSharesActivityPubUrl(this),
1599 comments: getVideoCommentsActivityPubUrl(this),
1600 attributedTo: [
1601 {
1602 type: 'Person',
1603 id: this.VideoChannel.Account.Actor.url
1604 },
1605 {
1606 type: 'Group',
1607 id: this.VideoChannel.Actor.url
1608 }
1609 ]
1610 }
1611 }
1612
1613 getTruncatedDescription () {
1614 if (!this.description) return null
1615
1616 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1617 return peertubeTruncate(this.description, maxLength)
1618 }
1619
1620 async optimizeOriginalVideofile () {
1621 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1622 const newExtname = '.mp4'
1623 const inputVideoFile = this.getOriginalFile()
1624 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
1625 const videoTranscodedPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
1626
1627 const transcodeOptions = {
1628 inputPath: videoInputPath,
1629 outputPath: videoTranscodedPath
1630 }
1631
1632 // Could be very long!
1633 await transcode(transcodeOptions)
1634
1635 try {
1636 await remove(videoInputPath)
1637
1638 // Important to do this before getVideoFilename() to take in account the new file extension
1639 inputVideoFile.set('extname', newExtname)
1640
1641 const videoOutputPath = this.getVideoFilePath(inputVideoFile)
1642 await rename(videoTranscodedPath, videoOutputPath)
1643 const stats = await stat(videoOutputPath)
1644 const fps = await getVideoFileFPS(videoOutputPath)
1645
1646 inputVideoFile.set('size', stats.size)
1647 inputVideoFile.set('fps', fps)
1648
1649 await this.createTorrentAndSetInfoHash(inputVideoFile)
1650 await inputVideoFile.save()
1651
1652 } catch (err) {
1653 // Auto destruction...
1654 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
1655
1656 throw err
1657 }
1658 }
1659
1660 async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) {
1661 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1662 const extname = '.mp4'
1663
1664 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
1665 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
1666
1667 const newVideoFile = new VideoFileModel({
1668 resolution,
1669 extname,
1670 size: 0,
1671 videoId: this.id
1672 })
1673 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
1674
1675 const transcodeOptions = {
1676 inputPath: videoInputPath,
1677 outputPath: videoOutputPath,
1678 resolution,
1679 isPortraitMode
1680 }
1681
1682 await transcode(transcodeOptions)
1683
1684 const stats = await stat(videoOutputPath)
1685 const fps = await getVideoFileFPS(videoOutputPath)
1686
1687 newVideoFile.set('size', stats.size)
1688 newVideoFile.set('fps', fps)
1689
1690 await this.createTorrentAndSetInfoHash(newVideoFile)
1691
1692 await newVideoFile.save()
1693
1694 this.VideoFiles.push(newVideoFile)
1695 }
1696
1697 async importVideoFile (inputFilePath: string) {
1698 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
1699 const { size } = await stat(inputFilePath)
1700 const fps = await getVideoFileFPS(inputFilePath)
1701
1702 let updatedVideoFile = new VideoFileModel({
1703 resolution: videoFileResolution,
1704 extname: extname(inputFilePath),
1705 size,
1706 fps,
1707 videoId: this.id
1708 })
1709
1710 const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
1711
1712 if (currentVideoFile) {
1713 // Remove old file and old torrent
1714 await this.removeFile(currentVideoFile)
1715 await this.removeTorrent(currentVideoFile)
1716 // Remove the old video file from the array
1717 this.VideoFiles = this.VideoFiles.filter(f => f !== currentVideoFile)
1718
1719 // Update the database
1720 currentVideoFile.set('extname', updatedVideoFile.extname)
1721 currentVideoFile.set('size', updatedVideoFile.size)
1722 currentVideoFile.set('fps', updatedVideoFile.fps)
1723
1724 updatedVideoFile = currentVideoFile
1725 }
1726
1727 const outputPath = this.getVideoFilePath(updatedVideoFile)
1728 await copy(inputFilePath, outputPath)
1729
1730 await this.createTorrentAndSetInfoHash(updatedVideoFile)
1731
1732 await updatedVideoFile.save()
1733
1734 this.VideoFiles.push(updatedVideoFile)
1735 }
1736
1737 getOriginalFileResolution () {
1738 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1739
1740 return getVideoFileResolution(originalFilePath)
1741 }
1742
1743 getDescriptionPath () {
1744 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1745 }
1746
1747 removeThumbnail () {
1748 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1749 return remove(thumbnailPath)
1750 .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err }))
1751 }
1752
1753 removePreview () {
1754 const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1755 return remove(previewPath)
1756 .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err }))
1757 }
1758
1759 removeFile (videoFile: VideoFileModel) {
1760 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1761 return remove(filePath)
1762 .catch(err => logger.warn('Cannot delete file %s.', filePath, { err }))
1763 }
1764
1765 removeTorrent (videoFile: VideoFileModel) {
1766 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1767 return remove(torrentPath)
1768 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
1769 }
1770
1771 getActivityStreamDuration () {
1772 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
1773 return 'PT' + this.duration + 'S'
1774 }
1775
1776 isOutdated () {
1777 if (this.isOwned()) return false
1778
1779 const now = Date.now()
1780 const createdAtTime = this.createdAt.getTime()
1781 const updatedAtTime = this.updatedAt.getTime()
1782
1783 return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL &&
1784 (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
1785 }
1786
1787 private getBaseUrls () {
1788 let baseUrlHttp
1789 let baseUrlWs
1790
1791 if (this.isOwned()) {
1792 baseUrlHttp = CONFIG.WEBSERVER.URL
1793 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1794 } else {
1795 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1796 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1797 }
1798
1799 return { baseUrlHttp, baseUrlWs }
1800 }
1801
1802 private getThumbnailUrl (baseUrlHttp: string) {
1803 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1804 }
1805
1806 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1807 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1808 }
1809
1810 private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1811 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1812 }
1813
1814 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1815 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1816 }
1817
1818 private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1819 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
1820 }
1821
1822 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1823 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1824 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1825 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1826
1827 const magnetHash = {
1828 xs,
1829 announce,
1830 urlList,
1831 infoHash: videoFile.infoHash,
1832 name: this.name
1833 }
1834
1835 return magnetUtil.encode(magnetHash)
1836 }
1837 }