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