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