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