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