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