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