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