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