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