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