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