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