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