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