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