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