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