]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video.ts
a28b5209b56a1fba95af02afbbadfe354f17e82c
[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 [Sequelize.Op.or]: [
680 {
681 name: {
682 [ Sequelize.Op.iLike ]: '%' + value + '%'
683 }
684 },
685 {
686 preferredUsername: Sequelize.where(Sequelize.col('preferredUsername'), {
687 [ Sequelize.Op.iLike ]: '%' + value + '%'
688 })
689 },
690 {
691 host: Sequelize.where(Sequelize.col('host'), {
692 [ Sequelize.Op.iLike ]: '%' + value + '%'
693 })
694 }
695 ]
696 }
697 }
698
699 const serverActor = await getServerActor()
700
701 return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id ] })
702 .findAndCountAll(query).then(({ rows, count }) => {
703 return {
704 data: rows,
705 total: count
706 }
707 })
708 }
709
710 static load (id: number) {
711 return VideoModel.findById(id)
712 }
713
714 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
715 const query: IFindOptions<VideoModel> = {
716 where: {
717 url
718 }
719 }
720
721 if (t !== undefined) query.transaction = t
722
723 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
724 }
725
726 static loadByUUIDOrURLAndPopulateAccount (uuid: string, url: string, t?: Sequelize.Transaction) {
727 const query: IFindOptions<VideoModel> = {
728 where: {
729 [Sequelize.Op.or]: [
730 { uuid },
731 { url }
732 ]
733 }
734 }
735
736 if (t !== undefined) query.transaction = t
737
738 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
739 }
740
741 static loadAndPopulateAccountAndServerAndTags (id: number) {
742 const options = {
743 order: [ [ 'Tags', 'name', 'ASC' ] ]
744 }
745
746 return VideoModel
747 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
748 .findById(id, options)
749 }
750
751 static loadByUUID (uuid: string) {
752 const options = {
753 where: {
754 uuid
755 }
756 }
757
758 return VideoModel
759 .scope([ ScopeNames.WITH_FILES ])
760 .findOne(options)
761 }
762
763 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string) {
764 const options = {
765 order: [ [ 'Tags', 'name', 'ASC' ] ],
766 where: {
767 uuid
768 }
769 }
770
771 return VideoModel
772 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ])
773 .findOne(options)
774 }
775
776 static loadAndPopulateAll (id: number) {
777 const options = {
778 order: [ [ 'Tags', 'name', 'ASC' ] ],
779 where: {
780 id
781 }
782 }
783
784 return VideoModel
785 .scope([
786 ScopeNames.WITH_RATES,
787 ScopeNames.WITH_SHARES,
788 ScopeNames.WITH_TAGS,
789 ScopeNames.WITH_FILES,
790 ScopeNames.WITH_ACCOUNT_DETAILS,
791 ScopeNames.WITH_COMMENTS
792 ])
793 .findOne(options)
794 }
795
796 static async getStats () {
797 const totalLocalVideos = await VideoModel.count({
798 where: {
799 remote: false
800 }
801 })
802 const totalVideos = await VideoModel.count()
803
804 let totalLocalVideoViews = await VideoModel.sum('views', {
805 where: {
806 remote: false
807 }
808 })
809 // Sequelize could return null...
810 if (!totalLocalVideoViews) totalLocalVideoViews = 0
811
812 return {
813 totalLocalVideos,
814 totalLocalVideoViews,
815 totalVideos
816 }
817 }
818
819 private static buildActorWhereWithFilter (filter?: VideoFilter) {
820 if (filter && filter === 'local') {
821 return {
822 serverId: null
823 }
824 }
825
826 return {}
827 }
828
829 private static getCategoryLabel (id: number) {
830 let categoryLabel = VIDEO_CATEGORIES[id]
831 if (!categoryLabel) categoryLabel = 'Misc'
832
833 return categoryLabel
834 }
835
836 private static getLicenceLabel (id: number) {
837 let licenceLabel = VIDEO_LICENCES[id]
838 if (!licenceLabel) licenceLabel = 'Unknown'
839
840 return licenceLabel
841 }
842
843 private static getLanguageLabel (id: number) {
844 let languageLabel = VIDEO_LANGUAGES[id]
845 if (!languageLabel) languageLabel = 'Unknown'
846
847 return languageLabel
848 }
849
850 getOriginalFile () {
851 if (Array.isArray(this.VideoFiles) === false) return undefined
852
853 // The original file is the file that have the higher resolution
854 return maxBy(this.VideoFiles, file => file.resolution)
855 }
856
857 getVideoFilename (videoFile: VideoFileModel) {
858 return this.uuid + '-' + videoFile.resolution + videoFile.extname
859 }
860
861 getThumbnailName () {
862 // We always have a copy of the thumbnail
863 const extension = '.jpg'
864 return this.uuid + extension
865 }
866
867 getPreviewName () {
868 const extension = '.jpg'
869 return this.uuid + extension
870 }
871
872 getTorrentFileName (videoFile: VideoFileModel) {
873 const extension = '.torrent'
874 return this.uuid + '-' + videoFile.resolution + extension
875 }
876
877 isOwned () {
878 return this.remote === false
879 }
880
881 createPreview (videoFile: VideoFileModel) {
882 return generateImageFromVideoFile(
883 this.getVideoFilePath(videoFile),
884 CONFIG.STORAGE.PREVIEWS_DIR,
885 this.getPreviewName(),
886 PREVIEWS_SIZE
887 )
888 }
889
890 createThumbnail (videoFile: VideoFileModel) {
891 return generateImageFromVideoFile(
892 this.getVideoFilePath(videoFile),
893 CONFIG.STORAGE.THUMBNAILS_DIR,
894 this.getThumbnailName(),
895 THUMBNAILS_SIZE
896 )
897 }
898
899 getVideoFilePath (videoFile: VideoFileModel) {
900 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
901 }
902
903 createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
904 const options = {
905 announceList: [
906 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
907 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
908 ],
909 urlList: [
910 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
911 ]
912 }
913
914 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
915
916 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
917 logger.info('Creating torrent %s.', filePath)
918
919 await writeFilePromise(filePath, torrent)
920
921 const parsedTorrent = parseTorrent(torrent)
922 videoFile.infoHash = parsedTorrent.infoHash
923 }
924
925 getEmbedPath () {
926 return '/videos/embed/' + this.uuid
927 }
928
929 getThumbnailPath () {
930 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
931 }
932
933 getPreviewPath () {
934 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
935 }
936
937 toFormattedJSON (): Video {
938 const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
939
940 return {
941 id: this.id,
942 uuid: this.uuid,
943 name: this.name,
944 category: {
945 id: this.category,
946 label: VideoModel.getCategoryLabel(this.category)
947 },
948 licence: {
949 id: this.licence,
950 label: VideoModel.getLicenceLabel(this.licence)
951 },
952 language: {
953 id: this.language,
954 label: VideoModel.getLanguageLabel(this.language)
955 },
956 nsfw: this.nsfw,
957 description: this.getTruncatedDescription(),
958 isLocal: this.isOwned(),
959 duration: this.duration,
960 views: this.views,
961 likes: this.likes,
962 dislikes: this.dislikes,
963 thumbnailPath: this.getThumbnailPath(),
964 previewPath: this.getPreviewPath(),
965 embedPath: this.getEmbedPath(),
966 createdAt: this.createdAt,
967 updatedAt: this.updatedAt,
968 account: {
969 name: formattedAccount.name,
970 displayName: formattedAccount.displayName,
971 url: formattedAccount.url,
972 host: formattedAccount.host,
973 avatar: formattedAccount.avatar
974 }
975 }
976 }
977
978 toFormattedDetailsJSON (): VideoDetails {
979 const formattedJson = this.toFormattedJSON()
980
981 // Maybe our server is not up to date and there are new privacy settings since our version
982 let privacyLabel = VIDEO_PRIVACIES[this.privacy]
983 if (!privacyLabel) privacyLabel = 'Unknown'
984
985 const detailsJson = {
986 privacy: {
987 id: this.privacy,
988 label: privacyLabel
989 },
990 support: this.support,
991 descriptionPath: this.getDescriptionPath(),
992 channel: this.VideoChannel.toFormattedJSON(),
993 account: this.VideoChannel.Account.toFormattedJSON(),
994 tags: map<TagModel, string>(this.Tags, 'name'),
995 commentsEnabled: this.commentsEnabled,
996 files: []
997 }
998
999 // Format and sort video files
1000 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1001 detailsJson.files = this.VideoFiles
1002 .map(videoFile => {
1003 let resolutionLabel = videoFile.resolution + 'p'
1004
1005 return {
1006 resolution: {
1007 id: videoFile.resolution,
1008 label: resolutionLabel
1009 },
1010 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
1011 size: videoFile.size,
1012 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
1013 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp)
1014 }
1015 })
1016 .sort((a, b) => {
1017 if (a.resolution.id < b.resolution.id) return 1
1018 if (a.resolution.id === b.resolution.id) return 0
1019 return -1
1020 })
1021
1022 return Object.assign(formattedJson, detailsJson)
1023 }
1024
1025 toActivityPubObject (): VideoTorrentObject {
1026 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1027 if (!this.Tags) this.Tags = []
1028
1029 const tag = this.Tags.map(t => ({
1030 type: 'Hashtag' as 'Hashtag',
1031 name: t.name
1032 }))
1033
1034 let language
1035 if (this.language) {
1036 language = {
1037 identifier: this.language + '',
1038 name: VideoModel.getLanguageLabel(this.language)
1039 }
1040 }
1041
1042 let category
1043 if (this.category) {
1044 category = {
1045 identifier: this.category + '',
1046 name: VideoModel.getCategoryLabel(this.category)
1047 }
1048 }
1049
1050 let licence
1051 if (this.licence) {
1052 licence = {
1053 identifier: this.licence + '',
1054 name: VideoModel.getLicenceLabel(this.licence)
1055 }
1056 }
1057
1058 let likesObject
1059 let dislikesObject
1060
1061 if (Array.isArray(this.AccountVideoRates)) {
1062 const res = this.toRatesActivityPubObjects()
1063 likesObject = res.likesObject
1064 dislikesObject = res.dislikesObject
1065 }
1066
1067 let sharesObject
1068 if (Array.isArray(this.VideoShares)) {
1069 sharesObject = this.toAnnouncesActivityPubObject()
1070 }
1071
1072 let commentsObject
1073 if (Array.isArray(this.VideoComments)) {
1074 commentsObject = this.toCommentsActivityPubObject()
1075 }
1076
1077 const url = []
1078 for (const file of this.VideoFiles) {
1079 url.push({
1080 type: 'Link',
1081 mimeType: 'video/' + file.extname.replace('.', ''),
1082 href: this.getVideoFileUrl(file, baseUrlHttp),
1083 width: file.resolution,
1084 size: file.size
1085 })
1086
1087 url.push({
1088 type: 'Link',
1089 mimeType: 'application/x-bittorrent',
1090 href: this.getTorrentUrl(file, baseUrlHttp),
1091 width: file.resolution
1092 })
1093
1094 url.push({
1095 type: 'Link',
1096 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
1097 href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
1098 width: file.resolution
1099 })
1100 }
1101
1102 // Add video url too
1103 url.push({
1104 type: 'Link',
1105 mimeType: 'text/html',
1106 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
1107 })
1108
1109 return {
1110 type: 'Video' as 'Video',
1111 id: this.url,
1112 name: this.name,
1113 duration: this.getActivityStreamDuration(),
1114 uuid: this.uuid,
1115 tag,
1116 category,
1117 licence,
1118 language,
1119 views: this.views,
1120 sensitive: this.nsfw,
1121 commentsEnabled: this.commentsEnabled,
1122 published: this.createdAt.toISOString(),
1123 updated: this.updatedAt.toISOString(),
1124 mediaType: 'text/markdown',
1125 content: this.getTruncatedDescription(),
1126 support: this.support,
1127 icon: {
1128 type: 'Image',
1129 url: this.getThumbnailUrl(baseUrlHttp),
1130 mediaType: 'image/jpeg',
1131 width: THUMBNAILS_SIZE.width,
1132 height: THUMBNAILS_SIZE.height
1133 },
1134 url,
1135 likes: likesObject,
1136 dislikes: dislikesObject,
1137 shares: sharesObject,
1138 comments: commentsObject,
1139 attributedTo: [
1140 {
1141 type: 'Person',
1142 id: this.VideoChannel.Account.Actor.url
1143 },
1144 {
1145 type: 'Group',
1146 id: this.VideoChannel.Actor.url
1147 }
1148 ]
1149 }
1150 }
1151
1152 toAnnouncesActivityPubObject () {
1153 const shares: string[] = []
1154
1155 for (const videoShare of this.VideoShares) {
1156 shares.push(videoShare.url)
1157 }
1158
1159 return activityPubCollection(getVideoSharesActivityPubUrl(this), shares)
1160 }
1161
1162 toCommentsActivityPubObject () {
1163 const comments: string[] = []
1164
1165 for (const videoComment of this.VideoComments) {
1166 comments.push(videoComment.url)
1167 }
1168
1169 return activityPubCollection(getVideoCommentsActivityPubUrl(this), comments)
1170 }
1171
1172 toRatesActivityPubObjects () {
1173 const likes: string[] = []
1174 const dislikes: string[] = []
1175
1176 for (const rate of this.AccountVideoRates) {
1177 if (rate.type === 'like') {
1178 likes.push(rate.Account.Actor.url)
1179 } else if (rate.type === 'dislike') {
1180 dislikes.push(rate.Account.Actor.url)
1181 }
1182 }
1183
1184 const likesObject = activityPubCollection(getVideoLikesActivityPubUrl(this), likes)
1185 const dislikesObject = activityPubCollection(getVideoDislikesActivityPubUrl(this), dislikes)
1186
1187 return { likesObject, dislikesObject }
1188 }
1189
1190 getTruncatedDescription () {
1191 if (!this.description) return null
1192
1193 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1194
1195 const options = {
1196 length: maxLength
1197 }
1198 const truncatedDescription = truncate(this.description, options)
1199
1200 // The truncated string is okay, we can return it
1201 if (truncatedDescription.length <= maxLength) return truncatedDescription
1202
1203 // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2
1204 // We always use the .length so we need to truncate more if needed
1205 options.length -= maxLength - truncatedDescription.length
1206 return truncate(this.description, options)
1207 }
1208
1209 optimizeOriginalVideofile = async function () {
1210 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1211 const newExtname = '.mp4'
1212 const inputVideoFile = this.getOriginalFile()
1213 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
1214 const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
1215
1216 const transcodeOptions = {
1217 inputPath: videoInputPath,
1218 outputPath: videoOutputPath
1219 }
1220
1221 // Could be very long!
1222 await transcode(transcodeOptions)
1223
1224 try {
1225 await unlinkPromise(videoInputPath)
1226
1227 // Important to do this before getVideoFilename() to take in account the new file extension
1228 inputVideoFile.set('extname', newExtname)
1229
1230 await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
1231 const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
1232
1233 inputVideoFile.set('size', stats.size)
1234
1235 await this.createTorrentAndSetInfoHash(inputVideoFile)
1236 await inputVideoFile.save()
1237
1238 } catch (err) {
1239 // Auto destruction...
1240 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
1241
1242 throw err
1243 }
1244 }
1245
1246 transcodeOriginalVideofile = async function (resolution: VideoResolution, isPortraitMode: boolean) {
1247 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1248 const extname = '.mp4'
1249
1250 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
1251 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
1252
1253 const newVideoFile = new VideoFileModel({
1254 resolution,
1255 extname,
1256 size: 0,
1257 videoId: this.id
1258 })
1259 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
1260
1261 const transcodeOptions = {
1262 inputPath: videoInputPath,
1263 outputPath: videoOutputPath,
1264 resolution,
1265 isPortraitMode
1266 }
1267
1268 await transcode(transcodeOptions)
1269
1270 const stats = await statPromise(videoOutputPath)
1271
1272 newVideoFile.set('size', stats.size)
1273
1274 await this.createTorrentAndSetInfoHash(newVideoFile)
1275
1276 await newVideoFile.save()
1277
1278 this.VideoFiles.push(newVideoFile)
1279 }
1280
1281 getOriginalFileResolution () {
1282 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1283
1284 return getVideoFileResolution(originalFilePath)
1285 }
1286
1287 getDescriptionPath () {
1288 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1289 }
1290
1291 removeThumbnail () {
1292 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1293 return unlinkPromise(thumbnailPath)
1294 }
1295
1296 removePreview () {
1297 // Same name than video thumbnail
1298 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1299 }
1300
1301 removeFile (videoFile: VideoFileModel) {
1302 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1303 return unlinkPromise(filePath)
1304 }
1305
1306 removeTorrent (videoFile: VideoFileModel) {
1307 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1308 return unlinkPromise(torrentPath)
1309 }
1310
1311 getActivityStreamDuration () {
1312 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
1313 return 'PT' + this.duration + 'S'
1314 }
1315
1316 private getBaseUrls () {
1317 let baseUrlHttp
1318 let baseUrlWs
1319
1320 if (this.isOwned()) {
1321 baseUrlHttp = CONFIG.WEBSERVER.URL
1322 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1323 } else {
1324 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1325 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1326 }
1327
1328 return { baseUrlHttp, baseUrlWs }
1329 }
1330
1331 private getThumbnailUrl (baseUrlHttp: string) {
1332 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1333 }
1334
1335 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1336 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1337 }
1338
1339 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1340 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1341 }
1342
1343 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1344 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1345 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1346 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1347
1348 const magnetHash = {
1349 xs,
1350 announce,
1351 urlList,
1352 infoHash: videoFile.infoHash,
1353 name: this.name
1354 }
1355
1356 return magnetUtil.encode(magnetHash)
1357 }
1358 }