]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video.ts
440f4d17161a7a1c78f4ad6fa51baec9431acd98
[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 } from 'sequelize-typescript'
29 import { VideoPrivacy, VideoResolution, VideoState } from '../../../shared'
30 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
31 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
32 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
33 import {
34 copyFilePromise,
35 createTorrentPromise,
36 peertubeTruncate,
37 renamePromise,
38 statPromise,
39 unlinkPromise,
40 writeFilePromise
41 } from '../../helpers/core-utils'
42 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
43 import { isBooleanValid } from '../../helpers/custom-validators/misc'
44 import {
45 isVideoCategoryValid,
46 isVideoDescriptionValid,
47 isVideoDurationValid,
48 isVideoLanguageValid,
49 isVideoLicenceValid,
50 isVideoNameValid,
51 isVideoPrivacyValid,
52 isVideoStateValid,
53 isVideoSupportValid
54 } from '../../helpers/custom-validators/videos'
55 import { generateImageFromVideoFile, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils'
56 import { logger } from '../../helpers/logger'
57 import { getServerActor } from '../../helpers/utils'
58 import {
59 API_VERSION,
60 CONFIG,
61 CONSTRAINTS_FIELDS,
62 PREVIEWS_SIZE,
63 REMOTE_SCHEME,
64 STATIC_DOWNLOAD_PATHS,
65 STATIC_PATHS,
66 THUMBNAILS_SIZE,
67 VIDEO_CATEGORIES,
68 VIDEO_EXT_MIMETYPE,
69 VIDEO_LANGUAGES,
70 VIDEO_LICENCES,
71 VIDEO_PRIVACIES,
72 VIDEO_STATES
73 } from '../../initializers'
74 import {
75 getVideoCommentsActivityPubUrl,
76 getVideoDislikesActivityPubUrl,
77 getVideoLikesActivityPubUrl,
78 getVideoSharesActivityPubUrl
79 } from '../../lib/activitypub'
80 import { sendDeleteVideo } from '../../lib/activitypub/send'
81 import { AccountModel } from '../account/account'
82 import { AccountVideoRateModel } from '../account/account-video-rate'
83 import { ActorModel } from '../activitypub/actor'
84 import { AvatarModel } from '../avatar/avatar'
85 import { ServerModel } from '../server/server'
86 import { getSort, throwIfNotValid } from '../utils'
87 import { TagModel } from './tag'
88 import { VideoAbuseModel } from './video-abuse'
89 import { VideoChannelModel } from './video-channel'
90 import { VideoCommentModel } from './video-comment'
91 import { VideoFileModel } from './video-file'
92 import { VideoShareModel } from './video-share'
93 import { VideoTagModel } from './video-tag'
94 import { ScheduleVideoUpdateModel } from './schedule-video-update'
95
96 export enum ScopeNames {
97 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
98 WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
99 WITH_TAGS = 'WITH_TAGS',
100 WITH_FILES = 'WITH_FILES'
101 }
102
103 @Scopes({
104 [ScopeNames.AVAILABLE_FOR_LIST]: (options: {
105 actorId: number,
106 hideNSFW: boolean,
107 filter?: VideoFilter,
108 withFiles?: boolean,
109 accountId?: number,
110 videoChannelId?: number
111 }) => {
112 const accountInclude = {
113 attributes: [ 'id', 'name' ],
114 model: AccountModel.unscoped(),
115 required: true,
116 where: {},
117 include: [
118 {
119 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
120 model: ActorModel.unscoped(),
121 required: true,
122 where: VideoModel.buildActorWhereWithFilter(options.filter),
123 include: [
124 {
125 attributes: [ 'host' ],
126 model: ServerModel.unscoped(),
127 required: false
128 },
129 {
130 model: AvatarModel.unscoped(),
131 required: false
132 }
133 ]
134 }
135 ]
136 }
137
138 const videoChannelInclude = {
139 attributes: [ 'name', 'description', 'id' ],
140 model: VideoChannelModel.unscoped(),
141 required: true,
142 where: {},
143 include: [
144 {
145 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
146 model: ActorModel.unscoped(),
147 required: true,
148 include: [
149 {
150 attributes: [ 'host' ],
151 model: ServerModel.unscoped(),
152 required: false
153 },
154 {
155 model: AvatarModel.unscoped(),
156 required: false
157 }
158 ]
159 },
160 accountInclude
161 ]
162 }
163
164 // Force actorId to be a number to avoid SQL injections
165 const actorIdNumber = parseInt(options.actorId.toString(), 10)
166 const query: IFindOptions<VideoModel> = {
167 where: {
168 id: {
169 [Sequelize.Op.notIn]: Sequelize.literal(
170 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
171 ),
172 [ Sequelize.Op.in ]: Sequelize.literal(
173 '(' +
174 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
175 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
176 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
177 ' UNION ' +
178 'SELECT "video"."id" AS "id" FROM "video" ' +
179 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
180 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
181 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
182 'LEFT JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
183 'WHERE "actor"."serverId" IS NULL OR "actorFollow"."actorId" = ' + actorIdNumber +
184 ')'
185 )
186 },
187 // Always list public videos
188 privacy: VideoPrivacy.PUBLIC,
189 // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
190 [ Sequelize.Op.or ]: [
191 {
192 state: VideoState.PUBLISHED
193 },
194 {
195 [ Sequelize.Op.and ]: {
196 state: VideoState.TO_TRANSCODE,
197 waitTranscoding: false
198 }
199 }
200 ]
201 },
202 include: [ videoChannelInclude ]
203 }
204
205 if (options.withFiles === true) {
206 query.include.push({
207 model: VideoFileModel.unscoped(),
208 required: true
209 })
210 }
211
212 // Hide nsfw videos?
213 if (options.hideNSFW === true) {
214 query.where['nsfw'] = false
215 }
216
217 if (options.accountId) {
218 accountInclude.where = {
219 id: options.accountId
220 }
221 }
222
223 if (options.videoChannelId) {
224 videoChannelInclude.where = {
225 id: options.videoChannelId
226 }
227 }
228
229 return query
230 },
231 [ScopeNames.WITH_ACCOUNT_DETAILS]: {
232 include: [
233 {
234 model: () => VideoChannelModel.unscoped(),
235 required: true,
236 include: [
237 {
238 attributes: {
239 exclude: [ 'privateKey', 'publicKey' ]
240 },
241 model: () => ActorModel.unscoped(),
242 required: true,
243 include: [
244 {
245 attributes: [ 'host' ],
246 model: () => ServerModel.unscoped(),
247 required: false
248 }
249 ]
250 },
251 {
252 model: () => AccountModel.unscoped(),
253 required: true,
254 include: [
255 {
256 model: () => ActorModel.unscoped(),
257 attributes: {
258 exclude: [ 'privateKey', 'publicKey' ]
259 },
260 required: true,
261 include: [
262 {
263 attributes: [ 'host' ],
264 model: () => ServerModel.unscoped(),
265 required: false
266 },
267 {
268 model: () => AvatarModel.unscoped(),
269 required: false
270 }
271 ]
272 }
273 ]
274 }
275 ]
276 }
277 ]
278 },
279 [ScopeNames.WITH_TAGS]: {
280 include: [ () => TagModel ]
281 },
282 [ScopeNames.WITH_FILES]: {
283 include: [
284 {
285 model: () => VideoFileModel.unscoped(),
286 required: true
287 }
288 ]
289 }
290 })
291 @Table({
292 tableName: 'video',
293 indexes: [
294 {
295 fields: [ 'name' ]
296 },
297 {
298 fields: [ 'createdAt' ]
299 },
300 {
301 fields: [ 'duration' ]
302 },
303 {
304 fields: [ 'views' ]
305 },
306 {
307 fields: [ 'likes' ]
308 },
309 {
310 fields: [ 'uuid' ]
311 },
312 {
313 fields: [ 'channelId' ]
314 },
315 {
316 fields: [ 'id', 'privacy', 'state', 'waitTranscoding' ]
317 },
318 {
319 fields: [ 'url'],
320 unique: true
321 }
322 ]
323 })
324 export class VideoModel extends Model<VideoModel> {
325
326 @AllowNull(false)
327 @Default(DataType.UUIDV4)
328 @IsUUID(4)
329 @Column(DataType.UUID)
330 uuid: string
331
332 @AllowNull(false)
333 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
334 @Column
335 name: string
336
337 @AllowNull(true)
338 @Default(null)
339 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
340 @Column
341 category: number
342
343 @AllowNull(true)
344 @Default(null)
345 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
346 @Column
347 licence: number
348
349 @AllowNull(true)
350 @Default(null)
351 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
352 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
353 language: string
354
355 @AllowNull(false)
356 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
357 @Column
358 privacy: number
359
360 @AllowNull(false)
361 @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
362 @Column
363 nsfw: boolean
364
365 @AllowNull(true)
366 @Default(null)
367 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
368 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
369 description: string
370
371 @AllowNull(true)
372 @Default(null)
373 @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
374 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
375 support: string
376
377 @AllowNull(false)
378 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
379 @Column
380 duration: number
381
382 @AllowNull(false)
383 @Default(0)
384 @IsInt
385 @Min(0)
386 @Column
387 views: number
388
389 @AllowNull(false)
390 @Default(0)
391 @IsInt
392 @Min(0)
393 @Column
394 likes: number
395
396 @AllowNull(false)
397 @Default(0)
398 @IsInt
399 @Min(0)
400 @Column
401 dislikes: number
402
403 @AllowNull(false)
404 @Column
405 remote: boolean
406
407 @AllowNull(false)
408 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
409 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
410 url: string
411
412 @AllowNull(false)
413 @Column
414 commentsEnabled: boolean
415
416 @AllowNull(false)
417 @Column
418 waitTranscoding: boolean
419
420 @AllowNull(false)
421 @Default(null)
422 @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
423 @Column
424 state: VideoState
425
426 @CreatedAt
427 createdAt: Date
428
429 @UpdatedAt
430 updatedAt: Date
431
432 @AllowNull(false)
433 @Default(Sequelize.NOW)
434 @Column
435 publishedAt: Date
436
437 @ForeignKey(() => VideoChannelModel)
438 @Column
439 channelId: number
440
441 @BelongsTo(() => VideoChannelModel, {
442 foreignKey: {
443 allowNull: true
444 },
445 hooks: true
446 })
447 VideoChannel: VideoChannelModel
448
449 @BelongsToMany(() => TagModel, {
450 foreignKey: 'videoId',
451 through: () => VideoTagModel,
452 onDelete: 'CASCADE'
453 })
454 Tags: TagModel[]
455
456 @HasMany(() => VideoAbuseModel, {
457 foreignKey: {
458 name: 'videoId',
459 allowNull: false
460 },
461 onDelete: 'cascade'
462 })
463 VideoAbuses: VideoAbuseModel[]
464
465 @HasMany(() => VideoFileModel, {
466 foreignKey: {
467 name: 'videoId',
468 allowNull: false
469 },
470 onDelete: 'cascade'
471 })
472 VideoFiles: VideoFileModel[]
473
474 @HasMany(() => VideoShareModel, {
475 foreignKey: {
476 name: 'videoId',
477 allowNull: false
478 },
479 onDelete: 'cascade'
480 })
481 VideoShares: VideoShareModel[]
482
483 @HasMany(() => AccountVideoRateModel, {
484 foreignKey: {
485 name: 'videoId',
486 allowNull: false
487 },
488 onDelete: 'cascade'
489 })
490 AccountVideoRates: AccountVideoRateModel[]
491
492 @HasMany(() => VideoCommentModel, {
493 foreignKey: {
494 name: 'videoId',
495 allowNull: false
496 },
497 onDelete: 'cascade',
498 hooks: true
499 })
500 VideoComments: VideoCommentModel[]
501
502 @HasOne(() => ScheduleVideoUpdateModel, {
503 foreignKey: {
504 name: 'videoId',
505 allowNull: false
506 },
507 onDelete: 'cascade'
508 })
509 ScheduleVideoUpdate: ScheduleVideoUpdateModel
510
511 @BeforeDestroy
512 static async sendDelete (instance: VideoModel, options) {
513 if (instance.isOwned()) {
514 if (!instance.VideoChannel) {
515 instance.VideoChannel = await instance.$get('VideoChannel', {
516 include: [
517 {
518 model: AccountModel,
519 include: [ ActorModel ]
520 }
521 ],
522 transaction: options.transaction
523 }) as VideoChannelModel
524 }
525
526 logger.debug('Sending delete of video %s.', instance.url)
527
528 return sendDeleteVideo(instance, options.transaction)
529 }
530
531 return undefined
532 }
533
534 @BeforeDestroy
535 static async removeFilesAndSendDelete (instance: VideoModel) {
536 const tasks: Promise<any>[] = []
537
538 logger.debug('Removing files of video %s.', instance.url)
539
540 tasks.push(instance.removeThumbnail())
541
542 if (instance.isOwned()) {
543 if (!Array.isArray(instance.VideoFiles)) {
544 instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
545 }
546
547 tasks.push(instance.removePreview())
548
549 // Remove physical files and torrents
550 instance.VideoFiles.forEach(file => {
551 tasks.push(instance.removeFile(file))
552 tasks.push(instance.removeTorrent(file))
553 })
554 }
555
556 // Do not wait video deletion because we could be in a transaction
557 Promise.all(tasks)
558 .catch(err => {
559 logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, { err })
560 })
561
562 return undefined
563 }
564
565 static list () {
566 return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
567 }
568
569 static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
570 function getRawQuery (select: string) {
571 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
572 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
573 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
574 'WHERE "Account"."actorId" = ' + actorId
575 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
576 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
577 'WHERE "VideoShare"."actorId" = ' + actorId
578
579 return `(${queryVideo}) UNION (${queryVideoShare})`
580 }
581
582 const rawQuery = getRawQuery('"Video"."id"')
583 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
584
585 const query = {
586 distinct: true,
587 offset: start,
588 limit: count,
589 order: getSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
590 where: {
591 id: {
592 [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
593 },
594 [Sequelize.Op.or]: [
595 { privacy: VideoPrivacy.PUBLIC },
596 { privacy: VideoPrivacy.UNLISTED }
597 ]
598 },
599 include: [
600 {
601 attributes: [ 'id', 'url' ],
602 model: VideoShareModel.unscoped(),
603 required: false,
604 // We only want videos shared by this actor
605 where: {
606 [Sequelize.Op.and]: [
607 {
608 id: {
609 [Sequelize.Op.not]: null
610 }
611 },
612 {
613 actorId
614 }
615 ]
616 },
617 include: [
618 {
619 attributes: [ 'id', 'url' ],
620 model: ActorModel.unscoped()
621 }
622 ]
623 },
624 {
625 model: VideoChannelModel.unscoped(),
626 required: true,
627 include: [
628 {
629 attributes: [ 'name' ],
630 model: AccountModel.unscoped(),
631 required: true,
632 include: [
633 {
634 attributes: [ 'id', 'url', 'followersUrl' ],
635 model: ActorModel.unscoped(),
636 required: true
637 }
638 ]
639 },
640 {
641 attributes: [ 'id', 'url', 'followersUrl' ],
642 model: ActorModel.unscoped(),
643 required: true
644 }
645 ]
646 },
647 VideoFileModel,
648 TagModel
649 ]
650 }
651
652 return Bluebird.all([
653 // FIXME: typing issue
654 VideoModel.findAll(query as any),
655 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
656 ]).then(([ rows, totals ]) => {
657 // totals: totalVideos + totalVideoShares
658 let totalVideos = 0
659 let totalVideoShares = 0
660 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
661 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
662
663 const total = totalVideos + totalVideoShares
664 return {
665 data: rows,
666 total: total
667 }
668 })
669 }
670
671 static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) {
672 const query: IFindOptions<VideoModel> = {
673 offset: start,
674 limit: count,
675 order: getSort(sort),
676 include: [
677 {
678 model: VideoChannelModel,
679 required: true,
680 include: [
681 {
682 model: AccountModel,
683 where: {
684 id: accountId
685 },
686 required: true
687 }
688 ]
689 },
690 {
691 model: ScheduleVideoUpdateModel,
692 required: false
693 }
694 ]
695 }
696
697 if (withFiles === true) {
698 query.include.push({
699 model: VideoFileModel.unscoped(),
700 required: true
701 })
702 }
703
704 if (hideNSFW === true) {
705 query.where = {
706 nsfw: false
707 }
708 }
709
710 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
711 return {
712 data: rows,
713 total: count
714 }
715 })
716 }
717
718 static async listForApi (options: {
719 start: number,
720 count: number,
721 sort: string,
722 hideNSFW: boolean,
723 withFiles: boolean,
724 filter?: VideoFilter,
725 accountId?: number,
726 videoChannelId?: number
727 }) {
728 const query = {
729 offset: options.start,
730 limit: options.count,
731 order: getSort(options.sort)
732 }
733
734 const serverActor = await getServerActor()
735 const scopes = {
736 method: [
737 ScopeNames.AVAILABLE_FOR_LIST, {
738 actorId: serverActor.id,
739 hideNSFW: options.hideNSFW,
740 filter: options.filter,
741 withFiles: options.withFiles,
742 accountId: options.accountId,
743 videoChannelId: options.videoChannelId
744 }
745 ]
746 }
747
748 return VideoModel.scope(scopes)
749 .findAndCountAll(query)
750 .then(({ rows, count }) => {
751 return {
752 data: rows,
753 total: count
754 }
755 })
756 }
757
758 static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string, hideNSFW: boolean) {
759 const query: IFindOptions<VideoModel> = {
760 offset: start,
761 limit: count,
762 order: getSort(sort),
763 where: {
764 [Sequelize.Op.or]: [
765 {
766 name: {
767 [ Sequelize.Op.iLike ]: '%' + value + '%'
768 }
769 },
770 {
771 preferredUsernameChannel: Sequelize.where(Sequelize.col('VideoChannel->Actor.preferredUsername'), {
772 [ Sequelize.Op.iLike ]: '%' + value + '%'
773 })
774 },
775 {
776 preferredUsernameAccount: Sequelize.where(Sequelize.col('VideoChannel->Account->Actor.preferredUsername'), {
777 [ Sequelize.Op.iLike ]: '%' + value + '%'
778 })
779 },
780 {
781 host: Sequelize.where(Sequelize.col('VideoChannel->Account->Actor->Server.host'), {
782 [ Sequelize.Op.iLike ]: '%' + value + '%'
783 })
784 }
785 ]
786 }
787 }
788
789 const serverActor = await getServerActor()
790 const scopes = {
791 method: [
792 ScopeNames.AVAILABLE_FOR_LIST, {
793 actorId: serverActor.id,
794 hideNSFW
795 }
796 ]
797 }
798
799 return VideoModel.scope(scopes)
800 .findAndCountAll(query)
801 .then(({ rows, count }) => {
802 return {
803 data: rows,
804 total: count
805 }
806 })
807 }
808
809 static load (id: number) {
810 return VideoModel.findById(id)
811 }
812
813 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
814 const query: IFindOptions<VideoModel> = {
815 where: {
816 url
817 }
818 }
819
820 if (t !== undefined) query.transaction = t
821
822 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
823 }
824
825 static loadByUUIDOrURLAndPopulateAccount (uuid: string, url: string, t?: Sequelize.Transaction) {
826 const query: IFindOptions<VideoModel> = {
827 where: {
828 [Sequelize.Op.or]: [
829 { uuid },
830 { url }
831 ]
832 }
833 }
834
835 if (t !== undefined) query.transaction = t
836
837 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
838 }
839
840 static loadAndPopulateAccountAndServerAndTags (id: number) {
841 const options = {
842 order: [ [ 'Tags', 'name', 'ASC' ] ]
843 }
844
845 return VideoModel
846 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
847 .findById(id, options)
848 }
849
850 static loadByUUID (uuid: string) {
851 const options = {
852 where: {
853 uuid
854 }
855 }
856
857 return VideoModel
858 .scope([ ScopeNames.WITH_FILES ])
859 .findOne(options)
860 }
861
862 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string, t?: Sequelize.Transaction) {
863 const options = {
864 order: [ [ 'Tags', 'name', 'ASC' ] ],
865 where: {
866 uuid
867 },
868 transaction: t
869 }
870
871 return VideoModel
872 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
873 .findOne(options)
874 }
875
876 static async getStats () {
877 const totalLocalVideos = await VideoModel.count({
878 where: {
879 remote: false
880 }
881 })
882 const totalVideos = await VideoModel.count()
883
884 let totalLocalVideoViews = await VideoModel.sum('views', {
885 where: {
886 remote: false
887 }
888 })
889 // Sequelize could return null...
890 if (!totalLocalVideoViews) totalLocalVideoViews = 0
891
892 return {
893 totalLocalVideos,
894 totalLocalVideoViews,
895 totalVideos
896 }
897 }
898
899 private static buildActorWhereWithFilter (filter?: VideoFilter) {
900 if (filter && filter === 'local') {
901 return {
902 serverId: null
903 }
904 }
905
906 return {}
907 }
908
909 private static getCategoryLabel (id: number) {
910 return VIDEO_CATEGORIES[id] || 'Misc'
911 }
912
913 private static getLicenceLabel (id: number) {
914 return VIDEO_LICENCES[id] || 'Unknown'
915 }
916
917 private static getLanguageLabel (id: string) {
918 return VIDEO_LANGUAGES[id] || 'Unknown'
919 }
920
921 private static getPrivacyLabel (id: number) {
922 return VIDEO_PRIVACIES[id] || 'Unknown'
923 }
924
925 private static getStateLabel (id: number) {
926 return VIDEO_STATES[id] || 'Unknown'
927 }
928
929 getOriginalFile () {
930 if (Array.isArray(this.VideoFiles) === false) return undefined
931
932 // The original file is the file that have the higher resolution
933 return maxBy(this.VideoFiles, file => file.resolution)
934 }
935
936 getVideoFilename (videoFile: VideoFileModel) {
937 return this.uuid + '-' + videoFile.resolution + videoFile.extname
938 }
939
940 getThumbnailName () {
941 // We always have a copy of the thumbnail
942 const extension = '.jpg'
943 return this.uuid + extension
944 }
945
946 getPreviewName () {
947 const extension = '.jpg'
948 return this.uuid + extension
949 }
950
951 getTorrentFileName (videoFile: VideoFileModel) {
952 const extension = '.torrent'
953 return this.uuid + '-' + videoFile.resolution + extension
954 }
955
956 isOwned () {
957 return this.remote === false
958 }
959
960 createPreview (videoFile: VideoFileModel) {
961 return generateImageFromVideoFile(
962 this.getVideoFilePath(videoFile),
963 CONFIG.STORAGE.PREVIEWS_DIR,
964 this.getPreviewName(),
965 PREVIEWS_SIZE
966 )
967 }
968
969 createThumbnail (videoFile: VideoFileModel) {
970 return generateImageFromVideoFile(
971 this.getVideoFilePath(videoFile),
972 CONFIG.STORAGE.THUMBNAILS_DIR,
973 this.getThumbnailName(),
974 THUMBNAILS_SIZE
975 )
976 }
977
978 getTorrentFilePath (videoFile: VideoFileModel) {
979 return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
980 }
981
982 getVideoFilePath (videoFile: VideoFileModel) {
983 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
984 }
985
986 async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
987 const options = {
988 // Keep the extname, it's used by the client to stream the file inside a web browser
989 name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
990 createdBy: 'PeerTube',
991 announceList: [
992 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
993 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
994 ],
995 urlList: [
996 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
997 ]
998 }
999
1000 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1001
1002 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1003 logger.info('Creating torrent %s.', filePath)
1004
1005 await writeFilePromise(filePath, torrent)
1006
1007 const parsedTorrent = parseTorrent(torrent)
1008 videoFile.infoHash = parsedTorrent.infoHash
1009 }
1010
1011 getEmbedPath () {
1012 return '/videos/embed/' + this.uuid
1013 }
1014
1015 getThumbnailPath () {
1016 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1017 }
1018
1019 getPreviewPath () {
1020 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1021 }
1022
1023 toFormattedJSON (options?: {
1024 additionalAttributes: {
1025 state: boolean,
1026 waitTranscoding: boolean,
1027 scheduledUpdate: boolean
1028 }
1029 }): Video {
1030 const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
1031 const formattedVideoChannel = this.VideoChannel.toFormattedJSON()
1032
1033 const videoObject: Video = {
1034 id: this.id,
1035 uuid: this.uuid,
1036 name: this.name,
1037 category: {
1038 id: this.category,
1039 label: VideoModel.getCategoryLabel(this.category)
1040 },
1041 licence: {
1042 id: this.licence,
1043 label: VideoModel.getLicenceLabel(this.licence)
1044 },
1045 language: {
1046 id: this.language,
1047 label: VideoModel.getLanguageLabel(this.language)
1048 },
1049 privacy: {
1050 id: this.privacy,
1051 label: VideoModel.getPrivacyLabel(this.privacy)
1052 },
1053 nsfw: this.nsfw,
1054 description: this.getTruncatedDescription(),
1055 isLocal: this.isOwned(),
1056 duration: this.duration,
1057 views: this.views,
1058 likes: this.likes,
1059 dislikes: this.dislikes,
1060 thumbnailPath: this.getThumbnailPath(),
1061 previewPath: this.getPreviewPath(),
1062 embedPath: this.getEmbedPath(),
1063 createdAt: this.createdAt,
1064 updatedAt: this.updatedAt,
1065 publishedAt: this.publishedAt,
1066 account: {
1067 id: formattedAccount.id,
1068 uuid: formattedAccount.uuid,
1069 name: formattedAccount.name,
1070 displayName: formattedAccount.displayName,
1071 url: formattedAccount.url,
1072 host: formattedAccount.host,
1073 avatar: formattedAccount.avatar
1074 },
1075 channel: {
1076 id: formattedVideoChannel.id,
1077 uuid: formattedVideoChannel.uuid,
1078 name: formattedVideoChannel.name,
1079 displayName: formattedVideoChannel.displayName,
1080 url: formattedVideoChannel.url,
1081 host: formattedVideoChannel.host,
1082 avatar: formattedVideoChannel.avatar
1083 }
1084 }
1085
1086 if (options) {
1087 if (options.additionalAttributes.state) {
1088 videoObject.state = {
1089 id: this.state,
1090 label: VideoModel.getStateLabel(this.state)
1091 }
1092 }
1093
1094 if (options.additionalAttributes.waitTranscoding) {
1095 videoObject.waitTranscoding = this.waitTranscoding
1096 }
1097
1098 if (options.additionalAttributes.scheduledUpdate && this.ScheduleVideoUpdate) {
1099 videoObject.scheduledUpdate = {
1100 updateAt: this.ScheduleVideoUpdate.updateAt,
1101 privacy: this.ScheduleVideoUpdate.privacy || undefined
1102 }
1103 }
1104 }
1105
1106 return videoObject
1107 }
1108
1109 toFormattedDetailsJSON (): VideoDetails {
1110 const formattedJson = this.toFormattedJSON()
1111
1112 const detailsJson = {
1113 support: this.support,
1114 descriptionPath: this.getDescriptionPath(),
1115 channel: this.VideoChannel.toFormattedJSON(),
1116 account: this.VideoChannel.Account.toFormattedJSON(),
1117 tags: map(this.Tags, 'name'),
1118 commentsEnabled: this.commentsEnabled,
1119 waitTranscoding: this.waitTranscoding,
1120 state: {
1121 id: this.state,
1122 label: VideoModel.getStateLabel(this.state)
1123 },
1124 files: []
1125 }
1126
1127 // Format and sort video files
1128 detailsJson.files = this.getFormattedVideoFilesJSON()
1129
1130 return Object.assign(formattedJson, detailsJson)
1131 }
1132
1133 getFormattedVideoFilesJSON (): VideoFile[] {
1134 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1135
1136 return this.VideoFiles
1137 .map(videoFile => {
1138 let resolutionLabel = videoFile.resolution + 'p'
1139
1140 return {
1141 resolution: {
1142 id: videoFile.resolution,
1143 label: resolutionLabel
1144 },
1145 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
1146 size: videoFile.size,
1147 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
1148 torrentDownloadUrl: this.getTorrentDownloadUrl(videoFile, baseUrlHttp),
1149 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp),
1150 fileDownloadUrl: this.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
1151 } as VideoFile
1152 })
1153 .sort((a, b) => {
1154 if (a.resolution.id < b.resolution.id) return 1
1155 if (a.resolution.id === b.resolution.id) return 0
1156 return -1
1157 })
1158 }
1159
1160 toActivityPubObject (): VideoTorrentObject {
1161 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1162 if (!this.Tags) this.Tags = []
1163
1164 const tag = this.Tags.map(t => ({
1165 type: 'Hashtag' as 'Hashtag',
1166 name: t.name
1167 }))
1168
1169 let language
1170 if (this.language) {
1171 language = {
1172 identifier: this.language,
1173 name: VideoModel.getLanguageLabel(this.language)
1174 }
1175 }
1176
1177 let category
1178 if (this.category) {
1179 category = {
1180 identifier: this.category + '',
1181 name: VideoModel.getCategoryLabel(this.category)
1182 }
1183 }
1184
1185 let licence
1186 if (this.licence) {
1187 licence = {
1188 identifier: this.licence + '',
1189 name: VideoModel.getLicenceLabel(this.licence)
1190 }
1191 }
1192
1193 const url = []
1194 for (const file of this.VideoFiles) {
1195 url.push({
1196 type: 'Link',
1197 mimeType: VIDEO_EXT_MIMETYPE[file.extname],
1198 href: this.getVideoFileUrl(file, baseUrlHttp),
1199 width: file.resolution,
1200 size: file.size
1201 })
1202
1203 url.push({
1204 type: 'Link',
1205 mimeType: 'application/x-bittorrent',
1206 href: this.getTorrentUrl(file, baseUrlHttp),
1207 width: file.resolution
1208 })
1209
1210 url.push({
1211 type: 'Link',
1212 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
1213 href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
1214 width: file.resolution
1215 })
1216 }
1217
1218 // Add video url too
1219 url.push({
1220 type: 'Link',
1221 mimeType: 'text/html',
1222 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
1223 })
1224
1225 return {
1226 type: 'Video' as 'Video',
1227 id: this.url,
1228 name: this.name,
1229 duration: this.getActivityStreamDuration(),
1230 uuid: this.uuid,
1231 tag,
1232 category,
1233 licence,
1234 language,
1235 views: this.views,
1236 sensitive: this.nsfw,
1237 waitTranscoding: this.waitTranscoding,
1238 state: this.state,
1239 commentsEnabled: this.commentsEnabled,
1240 published: this.publishedAt.toISOString(),
1241 updated: this.updatedAt.toISOString(),
1242 mediaType: 'text/markdown',
1243 content: this.getTruncatedDescription(),
1244 support: this.support,
1245 icon: {
1246 type: 'Image',
1247 url: this.getThumbnailUrl(baseUrlHttp),
1248 mediaType: 'image/jpeg',
1249 width: THUMBNAILS_SIZE.width,
1250 height: THUMBNAILS_SIZE.height
1251 },
1252 url,
1253 likes: getVideoLikesActivityPubUrl(this),
1254 dislikes: getVideoDislikesActivityPubUrl(this),
1255 shares: getVideoSharesActivityPubUrl(this),
1256 comments: getVideoCommentsActivityPubUrl(this),
1257 attributedTo: [
1258 {
1259 type: 'Person',
1260 id: this.VideoChannel.Account.Actor.url
1261 },
1262 {
1263 type: 'Group',
1264 id: this.VideoChannel.Actor.url
1265 }
1266 ]
1267 }
1268 }
1269
1270 getTruncatedDescription () {
1271 if (!this.description) return null
1272
1273 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1274 return peertubeTruncate(this.description, maxLength)
1275 }
1276
1277 async optimizeOriginalVideofile () {
1278 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1279 const newExtname = '.mp4'
1280 const inputVideoFile = this.getOriginalFile()
1281 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
1282 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
1283
1284 const transcodeOptions = {
1285 inputPath: videoInputPath,
1286 outputPath: videoOutputPath
1287 }
1288
1289 // Could be very long!
1290 await transcode(transcodeOptions)
1291
1292 try {
1293 await unlinkPromise(videoInputPath)
1294
1295 // Important to do this before getVideoFilename() to take in account the new file extension
1296 inputVideoFile.set('extname', newExtname)
1297
1298 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
1299 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
1300
1301 inputVideoFile.set('size', stats.size)
1302
1303 await this.createTorrentAndSetInfoHash(inputVideoFile)
1304 await inputVideoFile.save()
1305
1306 } catch (err) {
1307 // Auto destruction...
1308 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
1309
1310 throw err
1311 }
1312 }
1313
1314 async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) {
1315 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1316 const extname = '.mp4'
1317
1318 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
1319 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
1320
1321 const newVideoFile = new VideoFileModel({
1322 resolution,
1323 extname,
1324 size: 0,
1325 videoId: this.id
1326 })
1327 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
1328
1329 const transcodeOptions = {
1330 inputPath: videoInputPath,
1331 outputPath: videoOutputPath,
1332 resolution,
1333 isPortraitMode
1334 }
1335
1336 await transcode(transcodeOptions)
1337
1338 const stats = await statPromise(videoOutputPath)
1339
1340 newVideoFile.set('size', stats.size)
1341
1342 await this.createTorrentAndSetInfoHash(newVideoFile)
1343
1344 await newVideoFile.save()
1345
1346 this.VideoFiles.push(newVideoFile)
1347 }
1348
1349 async importVideoFile (inputFilePath: string) {
1350 let updatedVideoFile = new VideoFileModel({
1351 resolution: (await getVideoFileResolution(inputFilePath)).videoFileResolution,
1352 extname: extname(inputFilePath),
1353 size: (await statPromise(inputFilePath)).size,
1354 videoId: this.id
1355 })
1356
1357 const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
1358
1359 if (currentVideoFile) {
1360 // Remove old file and old torrent
1361 await this.removeFile(currentVideoFile)
1362 await this.removeTorrent(currentVideoFile)
1363 // Remove the old video file from the array
1364 this.VideoFiles = this.VideoFiles.filter(f => f !== currentVideoFile)
1365
1366 // Update the database
1367 currentVideoFile.set('extname', updatedVideoFile.extname)
1368 currentVideoFile.set('size', updatedVideoFile.size)
1369
1370 updatedVideoFile = currentVideoFile
1371 }
1372
1373 const outputPath = this.getVideoFilePath(updatedVideoFile)
1374 await copyFilePromise(inputFilePath, outputPath)
1375
1376 await this.createTorrentAndSetInfoHash(updatedVideoFile)
1377
1378 await updatedVideoFile.save()
1379
1380 this.VideoFiles.push(updatedVideoFile)
1381 }
1382
1383 getOriginalFileResolution () {
1384 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1385
1386 return getVideoFileResolution(originalFilePath)
1387 }
1388
1389 getDescriptionPath () {
1390 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1391 }
1392
1393 removeThumbnail () {
1394 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1395 return unlinkPromise(thumbnailPath)
1396 }
1397
1398 removePreview () {
1399 // Same name than video thumbnail
1400 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1401 }
1402
1403 removeFile (videoFile: VideoFileModel) {
1404 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1405 return unlinkPromise(filePath)
1406 }
1407
1408 removeTorrent (videoFile: VideoFileModel) {
1409 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1410 return unlinkPromise(torrentPath)
1411 }
1412
1413 getActivityStreamDuration () {
1414 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
1415 return 'PT' + this.duration + 'S'
1416 }
1417
1418 private getBaseUrls () {
1419 let baseUrlHttp
1420 let baseUrlWs
1421
1422 if (this.isOwned()) {
1423 baseUrlHttp = CONFIG.WEBSERVER.URL
1424 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1425 } else {
1426 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1427 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1428 }
1429
1430 return { baseUrlHttp, baseUrlWs }
1431 }
1432
1433 private getThumbnailUrl (baseUrlHttp: string) {
1434 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1435 }
1436
1437 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1438 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1439 }
1440
1441 private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1442 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1443 }
1444
1445 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1446 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1447 }
1448
1449 private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1450 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
1451 }
1452
1453 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1454 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1455 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1456 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1457
1458 const magnetHash = {
1459 xs,
1460 announce,
1461 urlList,
1462 infoHash: videoFile.infoHash,
1463 name: this.name
1464 }
1465
1466 return magnetUtil.encode(magnetHash)
1467 }
1468 }