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