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