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