]>
Commit | Line | Data |
---|---|---|
39445ead | 1 | import * as Bluebird from 'bluebird' |
c73e83da | 2 | import { map, maxBy } from 'lodash' |
571389d4 | 3 | import * as magnetUtil from 'magnet-uri' |
4d4e5cd4 | 4 | import * as parseTorrent from 'parse-torrent' |
28be8916 | 5 | import { extname, join } from 'path' |
e02643f3 | 6 | import * as Sequelize from 'sequelize' |
3fd3ab2d | 7 | import { |
4ba3b8ea C |
8 | AllowNull, |
9 | BeforeDestroy, | |
10 | BelongsTo, | |
11 | BelongsToMany, | |
12 | Column, | |
13 | CreatedAt, | |
14 | DataType, | |
15 | Default, | |
16 | ForeignKey, | |
17 | HasMany, | |
2baea0c7 | 18 | HasOne, |
4ba3b8ea C |
19 | IFindOptions, |
20 | Is, | |
21 | IsInt, | |
22 | IsUUID, | |
23 | Min, | |
24 | Model, | |
25 | Scopes, | |
26 | Table, | |
27 | UpdatedAt | |
3fd3ab2d | 28 | } from 'sequelize-typescript' |
2186386c | 29 | import { VideoPrivacy, VideoResolution, VideoState } from '../../../shared' |
3fd3ab2d | 30 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
a8462c8e | 31 | import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' |
066e94c5 | 32 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
c73e83da | 33 | import { |
28be8916 | 34 | copyFilePromise, |
06be7ed0 C |
35 | createTorrentPromise, |
36 | peertubeTruncate, | |
37 | renamePromise, | |
38 | statPromise, | |
39 | unlinkPromise, | |
c73e83da C |
40 | writeFilePromise |
41 | } from '../../helpers/core-utils' | |
da854ddd | 42 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
47564bbe | 43 | import { isBooleanValid } from '../../helpers/custom-validators/misc' |
3fd3ab2d | 44 | import { |
4ba3b8ea C |
45 | isVideoCategoryValid, |
46 | isVideoDescriptionValid, | |
47 | isVideoDurationValid, | |
48 | isVideoLanguageValid, | |
49 | isVideoLicenceValid, | |
50 | isVideoNameValid, | |
2baea0c7 C |
51 | isVideoPrivacyValid, |
52 | isVideoStateValid, | |
b64c950a | 53 | isVideoSupportValid |
3fd3ab2d | 54 | } from '../../helpers/custom-validators/videos' |
3a6f351b | 55 | import { generateImageFromVideoFile, getVideoFileFPS, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils' |
da854ddd | 56 | import { logger } from '../../helpers/logger' |
f05a1c30 | 57 | import { getServerActor } from '../../helpers/utils' |
65fcc311 | 58 | import { |
4ba3b8ea C |
59 | API_VERSION, |
60 | CONFIG, | |
61 | CONSTRAINTS_FIELDS, | |
62 | PREVIEWS_SIZE, | |
63 | REMOTE_SCHEME, | |
02756fbd | 64 | STATIC_DOWNLOAD_PATHS, |
4ba3b8ea C |
65 | STATIC_PATHS, |
66 | THUMBNAILS_SIZE, | |
67 | VIDEO_CATEGORIES, | |
28be8916 | 68 | VIDEO_EXT_MIMETYPE, |
4ba3b8ea C |
69 | VIDEO_LANGUAGES, |
70 | VIDEO_LICENCES, | |
2baea0c7 C |
71 | VIDEO_PRIVACIES, |
72 | VIDEO_STATES | |
3fd3ab2d | 73 | } from '../../initializers' |
46531a0a C |
74 | import { |
75 | getVideoCommentsActivityPubUrl, | |
76 | getVideoDislikesActivityPubUrl, | |
77 | getVideoLikesActivityPubUrl, | |
78 | getVideoSharesActivityPubUrl | |
79 | } from '../../lib/activitypub' | |
50d6de9c | 80 | import { sendDeleteVideo } from '../../lib/activitypub/send' |
3fd3ab2d C |
81 | import { AccountModel } from '../account/account' |
82 | import { AccountVideoRateModel } from '../account/account-video-rate' | |
50d6de9c | 83 | import { ActorModel } from '../activitypub/actor' |
b6a4fd6b | 84 | import { AvatarModel } from '../avatar/avatar' |
3fd3ab2d | 85 | import { ServerModel } from '../server/server' |
dbfd3e9b | 86 | import { buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils' |
3fd3ab2d C |
87 | import { TagModel } from './tag' |
88 | import { VideoAbuseModel } from './video-abuse' | |
89 | import { VideoChannelModel } from './video-channel' | |
da854ddd | 90 | import { VideoCommentModel } from './video-comment' |
3fd3ab2d C |
91 | import { VideoFileModel } from './video-file' |
92 | import { VideoShareModel } from './video-share' | |
93 | import { VideoTagModel } from './video-tag' | |
2baea0c7 | 94 | import { ScheduleVideoUpdateModel } from './schedule-video-update' |
40e87e9e | 95 | import { VideoCaptionModel } from './video-caption' |
26b7305a | 96 | import { VideoBlacklistModel } from './video-blacklist' |
3fd3ab2d | 97 | |
57c36b27 C |
98 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation |
99 | const indexes: Sequelize.DefineIndexesOptions[] = [ | |
100 | buildTrigramSearchIndex('video_name_trigram', 'name'), | |
101 | ||
8cd72bd3 C |
102 | { fields: [ 'createdAt' ] }, |
103 | { fields: [ 'publishedAt' ] }, | |
104 | { fields: [ 'duration' ] }, | |
105 | { fields: [ 'category' ] }, | |
106 | { fields: [ 'licence' ] }, | |
107 | { fields: [ 'nsfw' ] }, | |
108 | { fields: [ 'language' ] }, | |
109 | { fields: [ 'waitTranscoding' ] }, | |
110 | { fields: [ 'state' ] }, | |
111 | { fields: [ 'remote' ] }, | |
112 | { fields: [ 'views' ] }, | |
113 | { fields: [ 'likes' ] }, | |
114 | { fields: [ 'channelId' ] }, | |
57c36b27 | 115 | { |
8cd72bd3 C |
116 | fields: [ 'uuid' ], |
117 | unique: true | |
57c36b27 C |
118 | }, |
119 | { | |
120 | fields: [ 'url'], | |
121 | unique: true | |
122 | } | |
123 | ] | |
124 | ||
2baea0c7 | 125 | export enum ScopeNames { |
50d6de9c | 126 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', |
4cb6d457 | 127 | WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS', |
d48ff09d | 128 | WITH_TAGS = 'WITH_TAGS', |
bbe0f064 | 129 | WITH_FILES = 'WITH_FILES', |
191764f3 C |
130 | WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE', |
131 | WITH_BLACKLISTED = 'WITH_BLACKLISTED' | |
d48ff09d C |
132 | } |
133 | ||
d525fc39 C |
134 | type AvailableForListOptions = { |
135 | actorId: number, | |
136 | filter?: VideoFilter, | |
137 | categoryOneOf?: number[], | |
138 | nsfw?: boolean, | |
139 | licenceOneOf?: number[], | |
140 | languageOneOf?: string[], | |
141 | tagsOneOf?: string[], | |
142 | tagsAllOf?: string[], | |
143 | withFiles?: boolean, | |
144 | accountId?: number, | |
145 | videoChannelId?: number | |
146 | } | |
147 | ||
d48ff09d | 148 | @Scopes({ |
d525fc39 | 149 | [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { |
0626e7af | 150 | const accountInclude = { |
03e12d7c | 151 | attributes: [ 'id', 'name' ], |
0626e7af C |
152 | model: AccountModel.unscoped(), |
153 | required: true, | |
154 | where: {}, | |
155 | include: [ | |
156 | { | |
03e12d7c | 157 | attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ], |
0626e7af C |
158 | model: ActorModel.unscoped(), |
159 | required: true, | |
48dce1c9 | 160 | where: VideoModel.buildActorWhereWithFilter(options.filter), |
0626e7af C |
161 | include: [ |
162 | { | |
163 | attributes: [ 'host' ], | |
164 | model: ServerModel.unscoped(), | |
165 | required: false | |
166 | }, | |
167 | { | |
168 | model: AvatarModel.unscoped(), | |
169 | required: false | |
170 | } | |
171 | ] | |
172 | } | |
173 | ] | |
174 | } | |
175 | ||
48dce1c9 | 176 | const videoChannelInclude = { |
0f320037 | 177 | attributes: [ 'name', 'description', 'id' ], |
48dce1c9 C |
178 | model: VideoChannelModel.unscoped(), |
179 | required: true, | |
180 | where: {}, | |
181 | include: [ | |
0f320037 C |
182 | { |
183 | attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ], | |
184 | model: ActorModel.unscoped(), | |
185 | required: true, | |
186 | include: [ | |
187 | { | |
188 | attributes: [ 'host' ], | |
189 | model: ServerModel.unscoped(), | |
190 | required: false | |
191 | }, | |
192 | { | |
193 | model: AvatarModel.unscoped(), | |
194 | required: false | |
195 | } | |
196 | ] | |
197 | }, | |
48dce1c9 C |
198 | accountInclude |
199 | ] | |
200 | } | |
201 | ||
202 | // Force actorId to be a number to avoid SQL injections | |
203 | const actorIdNumber = parseInt(options.actorId.toString(), 10) | |
8d194d9a C |
204 | |
205 | // FIXME: It would be more efficient to use a CTE so we join AFTER the filters, but sequelize does not support it... | |
244e76a5 RK |
206 | const query: IFindOptions<VideoModel> = { |
207 | where: { | |
208 | id: { | |
209 | [Sequelize.Op.notIn]: Sequelize.literal( | |
210 | '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")' | |
211 | ), | |
212 | [ Sequelize.Op.in ]: Sequelize.literal( | |
213 | '(' + | |
8cd72bd3 C |
214 | 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + |
215 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + | |
216 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
217 | ' UNION ' + | |
218 | 'SELECT "video"."id" AS "id" FROM "video" ' + | |
219 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
220 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | |
221 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + | |
8d194d9a C |
222 | 'WHERE "actor"."serverId" IS NULL ' + |
223 | ' UNION ALL ' + | |
224 | 'SELECT "video"."id" AS "id" FROM "video" ' + | |
225 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
226 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | |
227 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + | |
228 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + | |
229 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
244e76a5 RK |
230 | ')' |
231 | ) | |
232 | }, | |
2186386c C |
233 | // Always list public videos |
234 | privacy: VideoPrivacy.PUBLIC, | |
235 | // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding | |
236 | [ Sequelize.Op.or ]: [ | |
237 | { | |
238 | state: VideoState.PUBLISHED | |
239 | }, | |
240 | { | |
241 | [ Sequelize.Op.and ]: { | |
242 | state: VideoState.TO_TRANSCODE, | |
243 | waitTranscoding: false | |
244 | } | |
245 | } | |
246 | ] | |
50d6de9c | 247 | }, |
48dce1c9 | 248 | include: [ videoChannelInclude ] |
244e76a5 RK |
249 | } |
250 | ||
48dce1c9 | 251 | if (options.withFiles === true) { |
244e76a5 RK |
252 | query.include.push({ |
253 | model: VideoFileModel.unscoped(), | |
254 | required: true | |
255 | }) | |
256 | } | |
257 | ||
d525fc39 C |
258 | // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN() |
259 | if (options.tagsAllOf || options.tagsOneOf) { | |
260 | const createTagsIn = (tags: string[]) => { | |
261 | return tags.map(t => VideoModel.sequelize.escape(t)) | |
262 | .join(', ') | |
263 | } | |
264 | ||
265 | if (options.tagsOneOf) { | |
266 | query.where['id'][Sequelize.Op.in] = Sequelize.literal( | |
267 | '(' + | |
268 | 'SELECT "videoId" FROM "videoTag" ' + | |
269 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
270 | 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' + | |
271 | ')' | |
272 | ) | |
273 | } | |
274 | ||
275 | if (options.tagsAllOf) { | |
276 | query.where['id'][Sequelize.Op.in] = Sequelize.literal( | |
277 | '(' + | |
278 | 'SELECT "videoId" FROM "videoTag" ' + | |
279 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
280 | 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' + | |
281 | 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length + | |
282 | ')' | |
283 | ) | |
284 | } | |
285 | } | |
286 | ||
287 | if (options.nsfw === true || options.nsfw === false) { | |
288 | query.where['nsfw'] = options.nsfw | |
289 | } | |
290 | ||
291 | if (options.categoryOneOf) { | |
292 | query.where['category'] = { | |
293 | [Sequelize.Op.or]: options.categoryOneOf | |
294 | } | |
295 | } | |
296 | ||
297 | if (options.licenceOneOf) { | |
298 | query.where['licence'] = { | |
299 | [Sequelize.Op.or]: options.licenceOneOf | |
300 | } | |
0883b324 C |
301 | } |
302 | ||
d525fc39 C |
303 | if (options.languageOneOf) { |
304 | query.where['language'] = { | |
305 | [Sequelize.Op.or]: options.languageOneOf | |
306 | } | |
61b909b9 P |
307 | } |
308 | ||
48dce1c9 | 309 | if (options.accountId) { |
0626e7af | 310 | accountInclude.where = { |
48dce1c9 C |
311 | id: options.accountId |
312 | } | |
313 | } | |
314 | ||
315 | if (options.videoChannelId) { | |
316 | videoChannelInclude.where = { | |
317 | id: options.videoChannelId | |
0626e7af C |
318 | } |
319 | } | |
320 | ||
244e76a5 RK |
321 | return query |
322 | }, | |
4cb6d457 | 323 | [ScopeNames.WITH_ACCOUNT_DETAILS]: { |
d48ff09d C |
324 | include: [ |
325 | { | |
6120941f | 326 | model: () => VideoChannelModel.unscoped(), |
d48ff09d C |
327 | required: true, |
328 | include: [ | |
6120941f C |
329 | { |
330 | attributes: { | |
331 | exclude: [ 'privateKey', 'publicKey' ] | |
332 | }, | |
3e500247 C |
333 | model: () => ActorModel.unscoped(), |
334 | required: true, | |
335 | include: [ | |
336 | { | |
337 | attributes: [ 'host' ], | |
338 | model: () => ServerModel.unscoped(), | |
339 | required: false | |
52d9f792 C |
340 | }, |
341 | { | |
342 | model: () => AvatarModel.unscoped(), | |
343 | required: false | |
3e500247 C |
344 | } |
345 | ] | |
6120941f | 346 | }, |
d48ff09d | 347 | { |
3e500247 | 348 | model: () => AccountModel.unscoped(), |
d48ff09d C |
349 | required: true, |
350 | include: [ | |
351 | { | |
3e500247 | 352 | model: () => ActorModel.unscoped(), |
6120941f C |
353 | attributes: { |
354 | exclude: [ 'privateKey', 'publicKey' ] | |
355 | }, | |
50d6de9c C |
356 | required: true, |
357 | include: [ | |
358 | { | |
3e500247 C |
359 | attributes: [ 'host' ], |
360 | model: () => ServerModel.unscoped(), | |
50d6de9c | 361 | required: false |
b6a4fd6b C |
362 | }, |
363 | { | |
364 | model: () => AvatarModel.unscoped(), | |
365 | required: false | |
50d6de9c C |
366 | } |
367 | ] | |
d48ff09d C |
368 | } |
369 | ] | |
370 | } | |
371 | ] | |
372 | } | |
373 | ] | |
374 | }, | |
375 | [ScopeNames.WITH_TAGS]: { | |
376 | include: [ () => TagModel ] | |
377 | }, | |
191764f3 C |
378 | [ScopeNames.WITH_BLACKLISTED]: { |
379 | include: [ | |
380 | { | |
381 | attributes: [ 'id', 'reason' ], | |
382 | model: () => VideoBlacklistModel, | |
383 | required: false | |
384 | } | |
385 | ] | |
386 | }, | |
d48ff09d C |
387 | [ScopeNames.WITH_FILES]: { |
388 | include: [ | |
389 | { | |
e53f952e | 390 | model: () => VideoFileModel.unscoped(), |
fbad87b0 | 391 | required: false |
d48ff09d C |
392 | } |
393 | ] | |
bbe0f064 C |
394 | }, |
395 | [ScopeNames.WITH_SCHEDULED_UPDATE]: { | |
396 | include: [ | |
397 | { | |
398 | model: () => ScheduleVideoUpdateModel.unscoped(), | |
399 | required: false | |
400 | } | |
401 | ] | |
d48ff09d C |
402 | } |
403 | }) | |
3fd3ab2d C |
404 | @Table({ |
405 | tableName: 'video', | |
57c36b27 | 406 | indexes |
3fd3ab2d C |
407 | }) |
408 | export class VideoModel extends Model<VideoModel> { | |
409 | ||
410 | @AllowNull(false) | |
411 | @Default(DataType.UUIDV4) | |
412 | @IsUUID(4) | |
413 | @Column(DataType.UUID) | |
414 | uuid: string | |
415 | ||
416 | @AllowNull(false) | |
417 | @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name')) | |
418 | @Column | |
419 | name: string | |
420 | ||
421 | @AllowNull(true) | |
422 | @Default(null) | |
423 | @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category')) | |
424 | @Column | |
425 | category: number | |
426 | ||
427 | @AllowNull(true) | |
428 | @Default(null) | |
429 | @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence')) | |
430 | @Column | |
431 | licence: number | |
432 | ||
433 | @AllowNull(true) | |
434 | @Default(null) | |
435 | @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language')) | |
9d3ef9fe C |
436 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max)) |
437 | language: string | |
3fd3ab2d C |
438 | |
439 | @AllowNull(false) | |
440 | @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy')) | |
441 | @Column | |
442 | privacy: number | |
443 | ||
444 | @AllowNull(false) | |
47564bbe | 445 | @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean')) |
3fd3ab2d C |
446 | @Column |
447 | nsfw: boolean | |
448 | ||
449 | @AllowNull(true) | |
450 | @Default(null) | |
451 | @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description')) | |
452 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max)) | |
453 | description: string | |
454 | ||
2422c46b C |
455 | @AllowNull(true) |
456 | @Default(null) | |
457 | @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support')) | |
458 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max)) | |
459 | support: string | |
460 | ||
3fd3ab2d C |
461 | @AllowNull(false) |
462 | @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration')) | |
463 | @Column | |
464 | duration: number | |
465 | ||
466 | @AllowNull(false) | |
467 | @Default(0) | |
468 | @IsInt | |
469 | @Min(0) | |
470 | @Column | |
471 | views: number | |
472 | ||
473 | @AllowNull(false) | |
474 | @Default(0) | |
475 | @IsInt | |
476 | @Min(0) | |
477 | @Column | |
478 | likes: number | |
479 | ||
480 | @AllowNull(false) | |
481 | @Default(0) | |
482 | @IsInt | |
483 | @Min(0) | |
484 | @Column | |
485 | dislikes: number | |
486 | ||
487 | @AllowNull(false) | |
488 | @Column | |
489 | remote: boolean | |
490 | ||
491 | @AllowNull(false) | |
492 | @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
493 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max)) | |
494 | url: string | |
495 | ||
47564bbe C |
496 | @AllowNull(false) |
497 | @Column | |
498 | commentsEnabled: boolean | |
499 | ||
2186386c C |
500 | @AllowNull(false) |
501 | @Column | |
502 | waitTranscoding: boolean | |
503 | ||
504 | @AllowNull(false) | |
505 | @Default(null) | |
506 | @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state')) | |
507 | @Column | |
508 | state: VideoState | |
509 | ||
3fd3ab2d C |
510 | @CreatedAt |
511 | createdAt: Date | |
512 | ||
513 | @UpdatedAt | |
514 | updatedAt: Date | |
515 | ||
2922e048 JLB |
516 | @AllowNull(false) |
517 | @Default(Sequelize.NOW) | |
518 | @Column | |
519 | publishedAt: Date | |
520 | ||
3fd3ab2d C |
521 | @ForeignKey(() => VideoChannelModel) |
522 | @Column | |
523 | channelId: number | |
524 | ||
525 | @BelongsTo(() => VideoChannelModel, { | |
feb4bdfd | 526 | foreignKey: { |
50d6de9c | 527 | allowNull: true |
feb4bdfd | 528 | }, |
6b738c7a | 529 | hooks: true |
feb4bdfd | 530 | }) |
3fd3ab2d | 531 | VideoChannel: VideoChannelModel |
7920c273 | 532 | |
3fd3ab2d | 533 | @BelongsToMany(() => TagModel, { |
7920c273 | 534 | foreignKey: 'videoId', |
3fd3ab2d C |
535 | through: () => VideoTagModel, |
536 | onDelete: 'CASCADE' | |
7920c273 | 537 | }) |
3fd3ab2d | 538 | Tags: TagModel[] |
55fa55a9 | 539 | |
3fd3ab2d | 540 | @HasMany(() => VideoAbuseModel, { |
55fa55a9 C |
541 | foreignKey: { |
542 | name: 'videoId', | |
543 | allowNull: false | |
544 | }, | |
545 | onDelete: 'cascade' | |
546 | }) | |
3fd3ab2d | 547 | VideoAbuses: VideoAbuseModel[] |
93e1258c | 548 | |
3fd3ab2d | 549 | @HasMany(() => VideoFileModel, { |
93e1258c C |
550 | foreignKey: { |
551 | name: 'videoId', | |
552 | allowNull: false | |
553 | }, | |
554 | onDelete: 'cascade' | |
555 | }) | |
3fd3ab2d | 556 | VideoFiles: VideoFileModel[] |
e71bcc0f | 557 | |
3fd3ab2d | 558 | @HasMany(() => VideoShareModel, { |
e71bcc0f C |
559 | foreignKey: { |
560 | name: 'videoId', | |
561 | allowNull: false | |
562 | }, | |
563 | onDelete: 'cascade' | |
564 | }) | |
3fd3ab2d | 565 | VideoShares: VideoShareModel[] |
16b90975 | 566 | |
3fd3ab2d | 567 | @HasMany(() => AccountVideoRateModel, { |
16b90975 C |
568 | foreignKey: { |
569 | name: 'videoId', | |
570 | allowNull: false | |
571 | }, | |
572 | onDelete: 'cascade' | |
573 | }) | |
3fd3ab2d | 574 | AccountVideoRates: AccountVideoRateModel[] |
f285faa0 | 575 | |
da854ddd C |
576 | @HasMany(() => VideoCommentModel, { |
577 | foreignKey: { | |
578 | name: 'videoId', | |
579 | allowNull: false | |
580 | }, | |
f05a1c30 C |
581 | onDelete: 'cascade', |
582 | hooks: true | |
da854ddd C |
583 | }) |
584 | VideoComments: VideoCommentModel[] | |
585 | ||
2baea0c7 C |
586 | @HasOne(() => ScheduleVideoUpdateModel, { |
587 | foreignKey: { | |
588 | name: 'videoId', | |
589 | allowNull: false | |
590 | }, | |
591 | onDelete: 'cascade' | |
592 | }) | |
593 | ScheduleVideoUpdate: ScheduleVideoUpdateModel | |
594 | ||
26b7305a C |
595 | @HasOne(() => VideoBlacklistModel, { |
596 | foreignKey: { | |
597 | name: 'videoId', | |
598 | allowNull: false | |
599 | }, | |
600 | onDelete: 'cascade' | |
601 | }) | |
602 | VideoBlacklist: VideoBlacklistModel | |
603 | ||
40e87e9e C |
604 | @HasMany(() => VideoCaptionModel, { |
605 | foreignKey: { | |
606 | name: 'videoId', | |
607 | allowNull: false | |
608 | }, | |
609 | onDelete: 'cascade', | |
610 | hooks: true, | |
611 | ['separate' as any]: true | |
612 | }) | |
613 | VideoCaptions: VideoCaptionModel[] | |
614 | ||
f05a1c30 C |
615 | @BeforeDestroy |
616 | static async sendDelete (instance: VideoModel, options) { | |
617 | if (instance.isOwned()) { | |
618 | if (!instance.VideoChannel) { | |
619 | instance.VideoChannel = await instance.$get('VideoChannel', { | |
620 | include: [ | |
621 | { | |
622 | model: AccountModel, | |
623 | include: [ ActorModel ] | |
624 | } | |
625 | ], | |
626 | transaction: options.transaction | |
627 | }) as VideoChannelModel | |
628 | } | |
629 | ||
f05a1c30 C |
630 | return sendDeleteVideo(instance, options.transaction) |
631 | } | |
632 | ||
633 | return undefined | |
634 | } | |
635 | ||
6b738c7a | 636 | @BeforeDestroy |
40e87e9e | 637 | static async removeFiles (instance: VideoModel) { |
f05a1c30 | 638 | const tasks: Promise<any>[] = [] |
f285faa0 | 639 | |
8e0fd45e | 640 | logger.info('Removing files of video %s.', instance.url) |
6b738c7a | 641 | |
f05a1c30 | 642 | tasks.push(instance.removeThumbnail()) |
93e1258c | 643 | |
3fd3ab2d | 644 | if (instance.isOwned()) { |
f05a1c30 C |
645 | if (!Array.isArray(instance.VideoFiles)) { |
646 | instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[] | |
647 | } | |
648 | ||
649 | tasks.push(instance.removePreview()) | |
40298b02 | 650 | |
3fd3ab2d C |
651 | // Remove physical files and torrents |
652 | instance.VideoFiles.forEach(file => { | |
653 | tasks.push(instance.removeFile(file)) | |
654 | tasks.push(instance.removeTorrent(file)) | |
655 | }) | |
656 | } | |
40298b02 | 657 | |
6b738c7a C |
658 | // Do not wait video deletion because we could be in a transaction |
659 | Promise.all(tasks) | |
3fd3ab2d | 660 | .catch(err => { |
18dbb5b9 | 661 | logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err }) |
3fd3ab2d | 662 | }) |
6b738c7a C |
663 | |
664 | return undefined | |
3fd3ab2d | 665 | } |
f285faa0 | 666 | |
3fd3ab2d | 667 | static list () { |
d48ff09d | 668 | return VideoModel.scope(ScopeNames.WITH_FILES).findAll() |
3fd3ab2d | 669 | } |
f285faa0 | 670 | |
50d6de9c | 671 | static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) { |
3fd3ab2d C |
672 | function getRawQuery (select: string) { |
673 | const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' + | |
674 | 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + | |
50d6de9c C |
675 | 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' + |
676 | 'WHERE "Account"."actorId" = ' + actorId | |
3fd3ab2d C |
677 | const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' + |
678 | 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + | |
50d6de9c | 679 | 'WHERE "VideoShare"."actorId" = ' + actorId |
558d7c23 | 680 | |
3fd3ab2d C |
681 | return `(${queryVideo}) UNION (${queryVideoShare})` |
682 | } | |
aaf61f38 | 683 | |
3fd3ab2d C |
684 | const rawQuery = getRawQuery('"Video"."id"') |
685 | const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"') | |
686 | ||
687 | const query = { | |
688 | distinct: true, | |
689 | offset: start, | |
690 | limit: count, | |
3bb6c526 | 691 | order: getSort('createdAt', [ 'Tags', 'name', 'ASC' ]), |
3fd3ab2d C |
692 | where: { |
693 | id: { | |
694 | [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')') | |
3c75ce12 C |
695 | }, |
696 | [Sequelize.Op.or]: [ | |
697 | { privacy: VideoPrivacy.PUBLIC }, | |
698 | { privacy: VideoPrivacy.UNLISTED } | |
699 | ] | |
3fd3ab2d C |
700 | }, |
701 | include: [ | |
40e87e9e C |
702 | { |
703 | attributes: [ 'language' ], | |
704 | model: VideoCaptionModel.unscoped(), | |
705 | required: false | |
706 | }, | |
3fd3ab2d | 707 | { |
1d230c44 | 708 | attributes: [ 'id', 'url' ], |
2c897999 | 709 | model: VideoShareModel.unscoped(), |
3fd3ab2d | 710 | required: false, |
e3d5ea4f C |
711 | // We only want videos shared by this actor |
712 | where: { | |
713 | [Sequelize.Op.and]: [ | |
714 | { | |
715 | id: { | |
716 | [Sequelize.Op.not]: null | |
717 | } | |
718 | }, | |
719 | { | |
720 | actorId | |
721 | } | |
722 | ] | |
723 | }, | |
50d6de9c C |
724 | include: [ |
725 | { | |
2c897999 C |
726 | attributes: [ 'id', 'url' ], |
727 | model: ActorModel.unscoped() | |
50d6de9c C |
728 | } |
729 | ] | |
3fd3ab2d C |
730 | }, |
731 | { | |
2c897999 | 732 | model: VideoChannelModel.unscoped(), |
3fd3ab2d C |
733 | required: true, |
734 | include: [ | |
735 | { | |
2c897999 C |
736 | attributes: [ 'name' ], |
737 | model: AccountModel.unscoped(), | |
738 | required: true, | |
739 | include: [ | |
740 | { | |
e3d5ea4f | 741 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 C |
742 | model: ActorModel.unscoped(), |
743 | required: true | |
744 | } | |
745 | ] | |
746 | }, | |
747 | { | |
e3d5ea4f | 748 | attributes: [ 'id', 'url', 'followersUrl' ], |
2c897999 | 749 | model: ActorModel.unscoped(), |
3fd3ab2d C |
750 | required: true |
751 | } | |
752 | ] | |
753 | }, | |
3fd3ab2d | 754 | VideoFileModel, |
2c897999 | 755 | TagModel |
3fd3ab2d C |
756 | ] |
757 | } | |
164174a6 | 758 | |
3fd3ab2d C |
759 | return Bluebird.all([ |
760 | // FIXME: typing issue | |
761 | VideoModel.findAll(query as any), | |
762 | VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT }) | |
763 | ]).then(([ rows, totals ]) => { | |
764 | // totals: totalVideos + totalVideoShares | |
765 | let totalVideos = 0 | |
766 | let totalVideoShares = 0 | |
767 | if (totals[0]) totalVideos = parseInt(totals[0].total, 10) | |
768 | if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10) | |
769 | ||
770 | const total = totalVideos + totalVideoShares | |
771 | return { | |
772 | data: rows, | |
773 | total: total | |
774 | } | |
775 | }) | |
776 | } | |
93e1258c | 777 | |
26b7305a | 778 | static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { |
244e76a5 | 779 | const query: IFindOptions<VideoModel> = { |
3fd3ab2d C |
780 | offset: start, |
781 | limit: count, | |
3bb6c526 | 782 | order: getSort(sort), |
3fd3ab2d C |
783 | include: [ |
784 | { | |
785 | model: VideoChannelModel, | |
786 | required: true, | |
787 | include: [ | |
788 | { | |
789 | model: AccountModel, | |
790 | where: { | |
7b87d2d5 | 791 | id: accountId |
3fd3ab2d C |
792 | }, |
793 | required: true | |
794 | } | |
795 | ] | |
2baea0c7 C |
796 | }, |
797 | { | |
798 | model: ScheduleVideoUpdateModel, | |
799 | required: false | |
26b7305a C |
800 | }, |
801 | { | |
802 | model: VideoBlacklistModel, | |
803 | required: false | |
d48ff09d | 804 | } |
3fd3ab2d C |
805 | ] |
806 | } | |
d8755eed | 807 | |
244e76a5 RK |
808 | if (withFiles === true) { |
809 | query.include.push({ | |
810 | model: VideoFileModel.unscoped(), | |
811 | required: true | |
812 | }) | |
813 | } | |
814 | ||
3fd3ab2d C |
815 | return VideoModel.findAndCountAll(query).then(({ rows, count }) => { |
816 | return { | |
817 | data: rows, | |
818 | total: count | |
819 | } | |
820 | }) | |
821 | } | |
93e1258c | 822 | |
48dce1c9 | 823 | static async listForApi (options: { |
0626e7af C |
824 | start: number, |
825 | count: number, | |
826 | sort: string, | |
d525fc39 | 827 | nsfw: boolean, |
48dce1c9 | 828 | withFiles: boolean, |
d525fc39 C |
829 | categoryOneOf?: number[], |
830 | licenceOneOf?: number[], | |
831 | languageOneOf?: string[], | |
832 | tagsOneOf?: string[], | |
833 | tagsAllOf?: string[], | |
0626e7af | 834 | filter?: VideoFilter, |
48dce1c9 C |
835 | accountId?: number, |
836 | videoChannelId?: number | |
837 | }) { | |
3fd3ab2d | 838 | const query = { |
48dce1c9 C |
839 | offset: options.start, |
840 | limit: options.count, | |
841 | order: getSort(options.sort) | |
3fd3ab2d | 842 | } |
93e1258c | 843 | |
f05a1c30 | 844 | const serverActor = await getServerActor() |
48dce1c9 C |
845 | const scopes = { |
846 | method: [ | |
847 | ScopeNames.AVAILABLE_FOR_LIST, { | |
848 | actorId: serverActor.id, | |
d525fc39 C |
849 | nsfw: options.nsfw, |
850 | categoryOneOf: options.categoryOneOf, | |
851 | licenceOneOf: options.licenceOneOf, | |
852 | languageOneOf: options.languageOneOf, | |
853 | tagsOneOf: options.tagsOneOf, | |
854 | tagsAllOf: options.tagsAllOf, | |
48dce1c9 C |
855 | filter: options.filter, |
856 | withFiles: options.withFiles, | |
857 | accountId: options.accountId, | |
858 | videoChannelId: options.videoChannelId | |
d525fc39 | 859 | } as AvailableForListOptions |
48dce1c9 C |
860 | ] |
861 | } | |
862 | ||
863 | return VideoModel.scope(scopes) | |
d48ff09d C |
864 | .findAndCountAll(query) |
865 | .then(({ rows, count }) => { | |
866 | return { | |
867 | data: rows, | |
868 | total: count | |
869 | } | |
870 | }) | |
93e1258c C |
871 | } |
872 | ||
0b18f4aa | 873 | static async searchAndPopulateAccountAndServer (options: { |
d4112450 | 874 | search?: string |
0b18f4aa C |
875 | start?: number |
876 | count?: number | |
877 | sort?: string | |
878 | startDate?: string // ISO 8601 | |
879 | endDate?: string // ISO 8601 | |
880 | nsfw?: boolean | |
881 | categoryOneOf?: number[] | |
882 | licenceOneOf?: number[] | |
883 | languageOneOf?: string[] | |
884 | tagsOneOf?: string[] | |
885 | tagsAllOf?: string[] | |
886 | durationMin?: number // seconds | |
887 | durationMax?: number // seconds | |
888 | }) { | |
d525fc39 C |
889 | const whereAnd = [ ] |
890 | ||
891 | if (options.startDate || options.endDate) { | |
892 | const publishedAtRange = { } | |
893 | ||
894 | if (options.startDate) publishedAtRange[Sequelize.Op.gte] = options.startDate | |
895 | if (options.endDate) publishedAtRange[Sequelize.Op.lte] = options.endDate | |
896 | ||
897 | whereAnd.push({ publishedAt: publishedAtRange }) | |
898 | } | |
899 | ||
900 | if (options.durationMin || options.durationMax) { | |
901 | const durationRange = { } | |
902 | ||
903 | if (options.durationMin) durationRange[Sequelize.Op.gte] = options.durationMin | |
904 | if (options.durationMax) durationRange[Sequelize.Op.lte] = options.durationMax | |
905 | ||
906 | whereAnd.push({ duration: durationRange }) | |
907 | } | |
908 | ||
d4112450 | 909 | const attributesInclude = [] |
dbfd3e9b C |
910 | const escapedSearch = VideoModel.sequelize.escape(options.search) |
911 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
d4112450 C |
912 | if (options.search) { |
913 | whereAnd.push( | |
914 | { | |
dbfd3e9b C |
915 | id: { |
916 | [ Sequelize.Op.in ]: Sequelize.literal( | |
917 | '(' + | |
918 | 'SELECT "video"."id" FROM "video" WHERE ' + | |
919 | 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + | |
920 | 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + | |
921 | 'UNION ALL ' + | |
922 | 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' + | |
923 | 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
924 | 'WHERE "tag"."name" = ' + escapedSearch + | |
925 | ')' | |
926 | ) | |
927 | } | |
d4112450 C |
928 | } |
929 | ) | |
930 | ||
931 | attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search)) | |
932 | } | |
933 | ||
934 | // Cannot search on similarity if we don't have a search | |
935 | if (!options.search) { | |
936 | attributesInclude.push( | |
937 | Sequelize.literal('0 as similarity') | |
938 | ) | |
939 | } | |
d525fc39 | 940 | |
f05a1c30 | 941 | const query: IFindOptions<VideoModel> = { |
57c36b27 | 942 | attributes: { |
d4112450 | 943 | include: attributesInclude |
57c36b27 | 944 | }, |
d525fc39 C |
945 | offset: options.start, |
946 | limit: options.count, | |
947 | order: getSort(options.sort), | |
948 | where: { | |
949 | [ Sequelize.Op.and ]: whereAnd | |
950 | } | |
f05a1c30 C |
951 | } |
952 | ||
953 | const serverActor = await getServerActor() | |
48dce1c9 C |
954 | const scopes = { |
955 | method: [ | |
956 | ScopeNames.AVAILABLE_FOR_LIST, { | |
957 | actorId: serverActor.id, | |
d525fc39 C |
958 | nsfw: options.nsfw, |
959 | categoryOneOf: options.categoryOneOf, | |
960 | licenceOneOf: options.licenceOneOf, | |
961 | languageOneOf: options.languageOneOf, | |
962 | tagsOneOf: options.tagsOneOf, | |
963 | tagsAllOf: options.tagsAllOf | |
964 | } as AvailableForListOptions | |
48dce1c9 C |
965 | ] |
966 | } | |
f05a1c30 | 967 | |
48dce1c9 | 968 | return VideoModel.scope(scopes) |
244e76a5 RK |
969 | .findAndCountAll(query) |
970 | .then(({ rows, count }) => { | |
f05a1c30 C |
971 | return { |
972 | data: rows, | |
973 | total: count | |
974 | } | |
975 | }) | |
976 | } | |
977 | ||
516df59b C |
978 | static load (id: number, t?: Sequelize.Transaction) { |
979 | const options = t ? { transaction: t } : undefined | |
980 | ||
981 | return VideoModel.findById(id, options) | |
3fd3ab2d | 982 | } |
fdbda9e3 | 983 | |
3fd3ab2d C |
984 | static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) { |
985 | const query: IFindOptions<VideoModel> = { | |
986 | where: { | |
987 | url | |
d48ff09d | 988 | } |
3fd3ab2d | 989 | } |
d8755eed | 990 | |
3fd3ab2d | 991 | if (t !== undefined) query.transaction = t |
d8755eed | 992 | |
4cb6d457 | 993 | return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query) |
3fd3ab2d | 994 | } |
d8755eed | 995 | |
2ccaeeb3 | 996 | static loadByUUIDOrURLAndPopulateAccount (uuid: string, url: string, t?: Sequelize.Transaction) { |
3fd3ab2d C |
997 | const query: IFindOptions<VideoModel> = { |
998 | where: { | |
999 | [Sequelize.Op.or]: [ | |
1000 | { uuid }, | |
1001 | { url } | |
1002 | ] | |
d48ff09d | 1003 | } |
3fd3ab2d | 1004 | } |
feb4bdfd | 1005 | |
3fd3ab2d | 1006 | if (t !== undefined) query.transaction = t |
feb4bdfd | 1007 | |
2ccaeeb3 | 1008 | return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query) |
72c7248b C |
1009 | } |
1010 | ||
3fd3ab2d C |
1011 | static loadAndPopulateAccountAndServerAndTags (id: number) { |
1012 | const options = { | |
d48ff09d | 1013 | order: [ [ 'Tags', 'name', 'ASC' ] ] |
3fd3ab2d | 1014 | } |
72c7248b | 1015 | |
d48ff09d | 1016 | return VideoModel |
191764f3 C |
1017 | .scope([ |
1018 | ScopeNames.WITH_TAGS, | |
1019 | ScopeNames.WITH_BLACKLISTED, | |
1020 | ScopeNames.WITH_FILES, | |
1021 | ScopeNames.WITH_ACCOUNT_DETAILS, | |
1022 | ScopeNames.WITH_SCHEDULED_UPDATE | |
1023 | ]) | |
d48ff09d | 1024 | .findById(id, options) |
3fd3ab2d | 1025 | } |
72c7248b | 1026 | |
8fa5653a C |
1027 | static loadByUUID (uuid: string) { |
1028 | const options = { | |
1029 | where: { | |
1030 | uuid | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | return VideoModel | |
1035 | .scope([ ScopeNames.WITH_FILES ]) | |
1036 | .findOne(options) | |
1037 | } | |
1038 | ||
2186386c | 1039 | static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string, t?: Sequelize.Transaction) { |
3fd3ab2d C |
1040 | const options = { |
1041 | order: [ [ 'Tags', 'name', 'ASC' ] ], | |
1042 | where: { | |
1043 | uuid | |
2186386c C |
1044 | }, |
1045 | transaction: t | |
3fd3ab2d | 1046 | } |
fd45e8f4 | 1047 | |
d48ff09d | 1048 | return VideoModel |
191764f3 C |
1049 | .scope([ |
1050 | ScopeNames.WITH_TAGS, | |
1051 | ScopeNames.WITH_BLACKLISTED, | |
1052 | ScopeNames.WITH_FILES, | |
1053 | ScopeNames.WITH_ACCOUNT_DETAILS, | |
1054 | ScopeNames.WITH_SCHEDULED_UPDATE | |
1055 | ]) | |
da854ddd C |
1056 | .findOne(options) |
1057 | } | |
1058 | ||
09cababd C |
1059 | static async getStats () { |
1060 | const totalLocalVideos = await VideoModel.count({ | |
1061 | where: { | |
1062 | remote: false | |
1063 | } | |
1064 | }) | |
1065 | const totalVideos = await VideoModel.count() | |
1066 | ||
1067 | let totalLocalVideoViews = await VideoModel.sum('views', { | |
1068 | where: { | |
1069 | remote: false | |
1070 | } | |
1071 | }) | |
1072 | // Sequelize could return null... | |
1073 | if (!totalLocalVideoViews) totalLocalVideoViews = 0 | |
1074 | ||
1075 | return { | |
1076 | totalLocalVideos, | |
1077 | totalLocalVideoViews, | |
1078 | totalVideos | |
1079 | } | |
1080 | } | |
1081 | ||
066e94c5 C |
1082 | private static buildActorWhereWithFilter (filter?: VideoFilter) { |
1083 | if (filter && filter === 'local') { | |
1084 | return { | |
1085 | serverId: null | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | return {} | |
1090 | } | |
1091 | ||
ae5a3dd6 | 1092 | private static getCategoryLabel (id: number) { |
2186386c | 1093 | return VIDEO_CATEGORIES[id] || 'Misc' |
ae5a3dd6 C |
1094 | } |
1095 | ||
1096 | private static getLicenceLabel (id: number) { | |
2186386c | 1097 | return VIDEO_LICENCES[id] || 'Unknown' |
ae5a3dd6 C |
1098 | } |
1099 | ||
9d3ef9fe | 1100 | private static getLanguageLabel (id: string) { |
2186386c | 1101 | return VIDEO_LANGUAGES[id] || 'Unknown' |
ae5a3dd6 C |
1102 | } |
1103 | ||
2243730c | 1104 | private static getPrivacyLabel (id: number) { |
2186386c C |
1105 | return VIDEO_PRIVACIES[id] || 'Unknown' |
1106 | } | |
2243730c | 1107 | |
2186386c C |
1108 | private static getStateLabel (id: number) { |
1109 | return VIDEO_STATES[id] || 'Unknown' | |
2243730c C |
1110 | } |
1111 | ||
3fd3ab2d C |
1112 | getOriginalFile () { |
1113 | if (Array.isArray(this.VideoFiles) === false) return undefined | |
aaf61f38 | 1114 | |
3fd3ab2d C |
1115 | // The original file is the file that have the higher resolution |
1116 | return maxBy(this.VideoFiles, file => file.resolution) | |
e4f97bab | 1117 | } |
aaf61f38 | 1118 | |
3fd3ab2d C |
1119 | getVideoFilename (videoFile: VideoFileModel) { |
1120 | return this.uuid + '-' + videoFile.resolution + videoFile.extname | |
1121 | } | |
165cdc75 | 1122 | |
3fd3ab2d C |
1123 | getThumbnailName () { |
1124 | // We always have a copy of the thumbnail | |
1125 | const extension = '.jpg' | |
1126 | return this.uuid + extension | |
7b1f49de C |
1127 | } |
1128 | ||
3fd3ab2d C |
1129 | getPreviewName () { |
1130 | const extension = '.jpg' | |
1131 | return this.uuid + extension | |
1132 | } | |
7b1f49de | 1133 | |
3fd3ab2d C |
1134 | getTorrentFileName (videoFile: VideoFileModel) { |
1135 | const extension = '.torrent' | |
1136 | return this.uuid + '-' + videoFile.resolution + extension | |
1137 | } | |
8e7f08b5 | 1138 | |
3fd3ab2d C |
1139 | isOwned () { |
1140 | return this.remote === false | |
9567011b C |
1141 | } |
1142 | ||
3fd3ab2d | 1143 | createPreview (videoFile: VideoFileModel) { |
3fd3ab2d C |
1144 | return generateImageFromVideoFile( |
1145 | this.getVideoFilePath(videoFile), | |
1146 | CONFIG.STORAGE.PREVIEWS_DIR, | |
1147 | this.getPreviewName(), | |
26670720 | 1148 | PREVIEWS_SIZE |
3fd3ab2d C |
1149 | ) |
1150 | } | |
9567011b | 1151 | |
3fd3ab2d | 1152 | createThumbnail (videoFile: VideoFileModel) { |
3fd3ab2d C |
1153 | return generateImageFromVideoFile( |
1154 | this.getVideoFilePath(videoFile), | |
1155 | CONFIG.STORAGE.THUMBNAILS_DIR, | |
1156 | this.getThumbnailName(), | |
26670720 | 1157 | THUMBNAILS_SIZE |
3fd3ab2d | 1158 | ) |
14d3270f C |
1159 | } |
1160 | ||
02756fbd C |
1161 | getTorrentFilePath (videoFile: VideoFileModel) { |
1162 | return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | |
1163 | } | |
1164 | ||
3fd3ab2d C |
1165 | getVideoFilePath (videoFile: VideoFileModel) { |
1166 | return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
1167 | } | |
14d3270f | 1168 | |
81e504b3 | 1169 | async createTorrentAndSetInfoHash (videoFile: VideoFileModel) { |
3fd3ab2d | 1170 | const options = { |
6cced8f9 C |
1171 | // Keep the extname, it's used by the client to stream the file inside a web browser |
1172 | name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`, | |
81e504b3 | 1173 | createdBy: 'PeerTube', |
3fd3ab2d | 1174 | announceList: [ |
0edf0581 C |
1175 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ], |
1176 | [ CONFIG.WEBSERVER.URL + '/tracker/announce' ] | |
3fd3ab2d C |
1177 | ], |
1178 | urlList: [ | |
1179 | CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) | |
1180 | ] | |
1181 | } | |
14d3270f | 1182 | |
3fd3ab2d | 1183 | const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options) |
e4f97bab | 1184 | |
3fd3ab2d C |
1185 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
1186 | logger.info('Creating torrent %s.', filePath) | |
e4f97bab | 1187 | |
3fd3ab2d | 1188 | await writeFilePromise(filePath, torrent) |
e4f97bab | 1189 | |
3fd3ab2d C |
1190 | const parsedTorrent = parseTorrent(torrent) |
1191 | videoFile.infoHash = parsedTorrent.infoHash | |
1192 | } | |
e4f97bab | 1193 | |
40e87e9e | 1194 | getEmbedStaticPath () { |
3fd3ab2d C |
1195 | return '/videos/embed/' + this.uuid |
1196 | } | |
e4f97bab | 1197 | |
40e87e9e | 1198 | getThumbnailStaticPath () { |
3fd3ab2d | 1199 | return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()) |
e4f97bab | 1200 | } |
227d02fe | 1201 | |
40e87e9e | 1202 | getPreviewStaticPath () { |
3fd3ab2d C |
1203 | return join(STATIC_PATHS.PREVIEWS, this.getPreviewName()) |
1204 | } | |
40298b02 | 1205 | |
2186386c C |
1206 | toFormattedJSON (options?: { |
1207 | additionalAttributes: { | |
bbe0f064 C |
1208 | state?: boolean, |
1209 | waitTranscoding?: boolean, | |
26b7305a C |
1210 | scheduledUpdate?: boolean, |
1211 | blacklistInfo?: boolean | |
2186386c C |
1212 | } |
1213 | }): Video { | |
b64c950a | 1214 | const formattedAccount = this.VideoChannel.Account.toFormattedJSON() |
0f320037 | 1215 | const formattedVideoChannel = this.VideoChannel.toFormattedJSON() |
14d3270f | 1216 | |
2186386c | 1217 | const videoObject: Video = { |
3fd3ab2d C |
1218 | id: this.id, |
1219 | uuid: this.uuid, | |
1220 | name: this.name, | |
ae5a3dd6 C |
1221 | category: { |
1222 | id: this.category, | |
1223 | label: VideoModel.getCategoryLabel(this.category) | |
1224 | }, | |
1225 | licence: { | |
1226 | id: this.licence, | |
1227 | label: VideoModel.getLicenceLabel(this.licence) | |
1228 | }, | |
1229 | language: { | |
1230 | id: this.language, | |
1231 | label: VideoModel.getLanguageLabel(this.language) | |
1232 | }, | |
2243730c C |
1233 | privacy: { |
1234 | id: this.privacy, | |
1235 | label: VideoModel.getPrivacyLabel(this.privacy) | |
1236 | }, | |
3fd3ab2d C |
1237 | nsfw: this.nsfw, |
1238 | description: this.getTruncatedDescription(), | |
3fd3ab2d | 1239 | isLocal: this.isOwned(), |
3fd3ab2d C |
1240 | duration: this.duration, |
1241 | views: this.views, | |
1242 | likes: this.likes, | |
1243 | dislikes: this.dislikes, | |
40e87e9e C |
1244 | thumbnailPath: this.getThumbnailStaticPath(), |
1245 | previewPath: this.getPreviewStaticPath(), | |
1246 | embedPath: this.getEmbedStaticPath(), | |
3fd3ab2d | 1247 | createdAt: this.createdAt, |
b64c950a | 1248 | updatedAt: this.updatedAt, |
2922e048 | 1249 | publishedAt: this.publishedAt, |
b64c950a | 1250 | account: { |
03e12d7c C |
1251 | id: formattedAccount.id, |
1252 | uuid: formattedAccount.uuid, | |
b64c950a C |
1253 | name: formattedAccount.name, |
1254 | displayName: formattedAccount.displayName, | |
1255 | url: formattedAccount.url, | |
1256 | host: formattedAccount.host, | |
1257 | avatar: formattedAccount.avatar | |
0f320037 C |
1258 | }, |
1259 | channel: { | |
1260 | id: formattedVideoChannel.id, | |
1261 | uuid: formattedVideoChannel.uuid, | |
1262 | name: formattedVideoChannel.name, | |
1263 | displayName: formattedVideoChannel.displayName, | |
1264 | url: formattedVideoChannel.url, | |
1265 | host: formattedVideoChannel.host, | |
1266 | avatar: formattedVideoChannel.avatar | |
b64c950a | 1267 | } |
2422c46b | 1268 | } |
2186386c C |
1269 | |
1270 | if (options) { | |
bbe0f064 | 1271 | if (options.additionalAttributes.state === true) { |
2186386c C |
1272 | videoObject.state = { |
1273 | id: this.state, | |
1274 | label: VideoModel.getStateLabel(this.state) | |
1275 | } | |
1276 | } | |
1277 | ||
bbe0f064 | 1278 | if (options.additionalAttributes.waitTranscoding === true) { |
2baea0c7 C |
1279 | videoObject.waitTranscoding = this.waitTranscoding |
1280 | } | |
1281 | ||
bbe0f064 | 1282 | if (options.additionalAttributes.scheduledUpdate === true && this.ScheduleVideoUpdate) { |
2baea0c7 C |
1283 | videoObject.scheduledUpdate = { |
1284 | updateAt: this.ScheduleVideoUpdate.updateAt, | |
1285 | privacy: this.ScheduleVideoUpdate.privacy || undefined | |
1286 | } | |
1287 | } | |
26b7305a C |
1288 | |
1289 | if (options.additionalAttributes.blacklistInfo === true) { | |
1290 | videoObject.blacklisted = !!this.VideoBlacklist | |
1291 | videoObject.blacklistedReason = this.VideoBlacklist ? this.VideoBlacklist.reason : null | |
1292 | } | |
2186386c C |
1293 | } |
1294 | ||
1295 | return videoObject | |
14d3270f | 1296 | } |
14d3270f | 1297 | |
2422c46b | 1298 | toFormattedDetailsJSON (): VideoDetails { |
bbe0f064 C |
1299 | const formattedJson = this.toFormattedJSON({ |
1300 | additionalAttributes: { | |
191764f3 C |
1301 | scheduledUpdate: true, |
1302 | blacklistInfo: true | |
bbe0f064 C |
1303 | } |
1304 | }) | |
e4f97bab | 1305 | |
3fd3ab2d | 1306 | const detailsJson = { |
2422c46b | 1307 | support: this.support, |
3fd3ab2d C |
1308 | descriptionPath: this.getDescriptionPath(), |
1309 | channel: this.VideoChannel.toFormattedJSON(), | |
1310 | account: this.VideoChannel.Account.toFormattedJSON(), | |
ee28cdf1 | 1311 | tags: map(this.Tags, 'name'), |
47564bbe | 1312 | commentsEnabled: this.commentsEnabled, |
2186386c C |
1313 | waitTranscoding: this.waitTranscoding, |
1314 | state: { | |
1315 | id: this.state, | |
1316 | label: VideoModel.getStateLabel(this.state) | |
1317 | }, | |
3fd3ab2d C |
1318 | files: [] |
1319 | } | |
e4f97bab | 1320 | |
3fd3ab2d | 1321 | // Format and sort video files |
244e76a5 RK |
1322 | detailsJson.files = this.getFormattedVideoFilesJSON() |
1323 | ||
1324 | return Object.assign(formattedJson, detailsJson) | |
1325 | } | |
1326 | ||
1327 | getFormattedVideoFilesJSON (): VideoFile[] { | |
3fd3ab2d | 1328 | const { baseUrlHttp, baseUrlWs } = this.getBaseUrls() |
3fd3ab2d | 1329 | |
244e76a5 RK |
1330 | return this.VideoFiles |
1331 | .map(videoFile => { | |
1332 | let resolutionLabel = videoFile.resolution + 'p' | |
3fd3ab2d | 1333 | |
244e76a5 RK |
1334 | return { |
1335 | resolution: { | |
1336 | id: videoFile.resolution, | |
1337 | label: resolutionLabel | |
1338 | }, | |
1339 | magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs), | |
1340 | size: videoFile.size, | |
3a6f351b | 1341 | fps: videoFile.fps, |
244e76a5 | 1342 | torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp), |
02756fbd C |
1343 | torrentDownloadUrl: this.getTorrentDownloadUrl(videoFile, baseUrlHttp), |
1344 | fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp), | |
1345 | fileDownloadUrl: this.getVideoFileDownloadUrl(videoFile, baseUrlHttp) | |
244e76a5 RK |
1346 | } as VideoFile |
1347 | }) | |
1348 | .sort((a, b) => { | |
1349 | if (a.resolution.id < b.resolution.id) return 1 | |
1350 | if (a.resolution.id === b.resolution.id) return 0 | |
1351 | return -1 | |
1352 | }) | |
3fd3ab2d | 1353 | } |
e4f97bab | 1354 | |
3fd3ab2d C |
1355 | toActivityPubObject (): VideoTorrentObject { |
1356 | const { baseUrlHttp, baseUrlWs } = this.getBaseUrls() | |
1357 | if (!this.Tags) this.Tags = [] | |
e4f97bab | 1358 | |
3fd3ab2d C |
1359 | const tag = this.Tags.map(t => ({ |
1360 | type: 'Hashtag' as 'Hashtag', | |
1361 | name: t.name | |
1362 | })) | |
40298b02 | 1363 | |
3fd3ab2d C |
1364 | let language |
1365 | if (this.language) { | |
1366 | language = { | |
9d3ef9fe | 1367 | identifier: this.language, |
ae5a3dd6 | 1368 | name: VideoModel.getLanguageLabel(this.language) |
3fd3ab2d C |
1369 | } |
1370 | } | |
40298b02 | 1371 | |
3fd3ab2d C |
1372 | let category |
1373 | if (this.category) { | |
1374 | category = { | |
1375 | identifier: this.category + '', | |
ae5a3dd6 | 1376 | name: VideoModel.getCategoryLabel(this.category) |
3fd3ab2d C |
1377 | } |
1378 | } | |
40298b02 | 1379 | |
3fd3ab2d C |
1380 | let licence |
1381 | if (this.licence) { | |
1382 | licence = { | |
1383 | identifier: this.licence + '', | |
ae5a3dd6 | 1384 | name: VideoModel.getLicenceLabel(this.licence) |
3fd3ab2d C |
1385 | } |
1386 | } | |
9567011b | 1387 | |
3fd3ab2d C |
1388 | const url = [] |
1389 | for (const file of this.VideoFiles) { | |
1390 | url.push({ | |
1391 | type: 'Link', | |
28be8916 | 1392 | mimeType: VIDEO_EXT_MIMETYPE[file.extname], |
9fb3abfd | 1393 | href: this.getVideoFileUrl(file, baseUrlHttp), |
3fd3ab2d | 1394 | width: file.resolution, |
b2977eec C |
1395 | size: file.size, |
1396 | fps: file.fps | |
3fd3ab2d C |
1397 | }) |
1398 | ||
1399 | url.push({ | |
1400 | type: 'Link', | |
1401 | mimeType: 'application/x-bittorrent', | |
9fb3abfd | 1402 | href: this.getTorrentUrl(file, baseUrlHttp), |
3fd3ab2d C |
1403 | width: file.resolution |
1404 | }) | |
1405 | ||
1406 | url.push({ | |
1407 | type: 'Link', | |
1408 | mimeType: 'application/x-bittorrent;x-scheme-handler/magnet', | |
9fb3abfd | 1409 | href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs), |
3fd3ab2d C |
1410 | width: file.resolution |
1411 | }) | |
1412 | } | |
93e1258c | 1413 | |
3fd3ab2d C |
1414 | // Add video url too |
1415 | url.push({ | |
1416 | type: 'Link', | |
1417 | mimeType: 'text/html', | |
9fb3abfd | 1418 | href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid |
3fd3ab2d | 1419 | }) |
93e1258c | 1420 | |
40e87e9e C |
1421 | const subtitleLanguage = [] |
1422 | for (const caption of this.VideoCaptions) { | |
1423 | subtitleLanguage.push({ | |
1424 | identifier: caption.language, | |
1425 | name: VideoCaptionModel.getLanguageLabel(caption.language) | |
1426 | }) | |
1427 | } | |
1428 | ||
3fd3ab2d C |
1429 | return { |
1430 | type: 'Video' as 'Video', | |
1431 | id: this.url, | |
1432 | name: this.name, | |
093237cf | 1433 | duration: this.getActivityStreamDuration(), |
3fd3ab2d C |
1434 | uuid: this.uuid, |
1435 | tag, | |
1436 | category, | |
1437 | licence, | |
1438 | language, | |
1439 | views: this.views, | |
0a67e28b | 1440 | sensitive: this.nsfw, |
2186386c C |
1441 | waitTranscoding: this.waitTranscoding, |
1442 | state: this.state, | |
47564bbe | 1443 | commentsEnabled: this.commentsEnabled, |
2922e048 | 1444 | published: this.publishedAt.toISOString(), |
3fd3ab2d C |
1445 | updated: this.updatedAt.toISOString(), |
1446 | mediaType: 'text/markdown', | |
1447 | content: this.getTruncatedDescription(), | |
2422c46b | 1448 | support: this.support, |
40e87e9e | 1449 | subtitleLanguage, |
3fd3ab2d C |
1450 | icon: { |
1451 | type: 'Image', | |
1452 | url: this.getThumbnailUrl(baseUrlHttp), | |
1453 | mediaType: 'image/jpeg', | |
1454 | width: THUMBNAILS_SIZE.width, | |
1455 | height: THUMBNAILS_SIZE.height | |
1456 | }, | |
1457 | url, | |
8fffe21a C |
1458 | likes: getVideoLikesActivityPubUrl(this), |
1459 | dislikes: getVideoDislikesActivityPubUrl(this), | |
1460 | shares: getVideoSharesActivityPubUrl(this), | |
1461 | comments: getVideoCommentsActivityPubUrl(this), | |
50d6de9c | 1462 | attributedTo: [ |
2ccaeeb3 C |
1463 | { |
1464 | type: 'Person', | |
1465 | id: this.VideoChannel.Account.Actor.url | |
fc27b17c C |
1466 | }, |
1467 | { | |
1468 | type: 'Group', | |
1469 | id: this.VideoChannel.Actor.url | |
50d6de9c C |
1470 | } |
1471 | ] | |
3fd3ab2d C |
1472 | } |
1473 | } | |
1474 | ||
1475 | getTruncatedDescription () { | |
1476 | if (!this.description) return null | |
93e1258c | 1477 | |
bffbebbe | 1478 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max |
c73e83da | 1479 | return peertubeTruncate(this.description, maxLength) |
93e1258c C |
1480 | } |
1481 | ||
81e504b3 | 1482 | async optimizeOriginalVideofile () { |
3fd3ab2d C |
1483 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
1484 | const newExtname = '.mp4' | |
1485 | const inputVideoFile = this.getOriginalFile() | |
1486 | const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile)) | |
3a6f351b | 1487 | const videoTranscodedPath = join(videosDirectory, this.id + '-transcoded' + newExtname) |
b769007f | 1488 | |
3fd3ab2d C |
1489 | const transcodeOptions = { |
1490 | inputPath: videoInputPath, | |
3a6f351b | 1491 | outputPath: videoTranscodedPath |
3fd3ab2d | 1492 | } |
c46edbc2 | 1493 | |
b0ef1782 C |
1494 | // Could be very long! |
1495 | await transcode(transcodeOptions) | |
c46edbc2 | 1496 | |
b0ef1782 | 1497 | try { |
3fd3ab2d | 1498 | await unlinkPromise(videoInputPath) |
c46edbc2 | 1499 | |
3fd3ab2d C |
1500 | // Important to do this before getVideoFilename() to take in account the new file extension |
1501 | inputVideoFile.set('extname', newExtname) | |
e71bcc0f | 1502 | |
3a6f351b C |
1503 | const videoOutputPath = this.getVideoFilePath(inputVideoFile) |
1504 | await renamePromise(videoTranscodedPath, videoOutputPath) | |
1505 | const stats = await statPromise(videoOutputPath) | |
1506 | const fps = await getVideoFileFPS(videoOutputPath) | |
e71bcc0f | 1507 | |
3fd3ab2d | 1508 | inputVideoFile.set('size', stats.size) |
3a6f351b | 1509 | inputVideoFile.set('fps', fps) |
e71bcc0f | 1510 | |
3fd3ab2d C |
1511 | await this.createTorrentAndSetInfoHash(inputVideoFile) |
1512 | await inputVideoFile.save() | |
fd45e8f4 | 1513 | |
3fd3ab2d C |
1514 | } catch (err) { |
1515 | // Auto destruction... | |
d5b7d911 | 1516 | this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) |
fd45e8f4 | 1517 | |
3fd3ab2d C |
1518 | throw err |
1519 | } | |
feb4bdfd C |
1520 | } |
1521 | ||
81e504b3 | 1522 | async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) { |
3fd3ab2d C |
1523 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
1524 | const extname = '.mp4' | |
aaf61f38 | 1525 | |
3fd3ab2d C |
1526 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed |
1527 | const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile())) | |
feb4bdfd | 1528 | |
3fd3ab2d C |
1529 | const newVideoFile = new VideoFileModel({ |
1530 | resolution, | |
1531 | extname, | |
1532 | size: 0, | |
1533 | videoId: this.id | |
1534 | }) | |
1535 | const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile)) | |
a041b171 | 1536 | |
3fd3ab2d C |
1537 | const transcodeOptions = { |
1538 | inputPath: videoInputPath, | |
1539 | outputPath: videoOutputPath, | |
056aa7f2 C |
1540 | resolution, |
1541 | isPortraitMode | |
3fd3ab2d | 1542 | } |
a041b171 | 1543 | |
3fd3ab2d | 1544 | await transcode(transcodeOptions) |
a041b171 | 1545 | |
3fd3ab2d | 1546 | const stats = await statPromise(videoOutputPath) |
3a6f351b | 1547 | const fps = await getVideoFileFPS(videoOutputPath) |
d7d5611c | 1548 | |
3fd3ab2d | 1549 | newVideoFile.set('size', stats.size) |
3a6f351b | 1550 | newVideoFile.set('fps', fps) |
d7d5611c | 1551 | |
3fd3ab2d | 1552 | await this.createTorrentAndSetInfoHash(newVideoFile) |
d7d5611c | 1553 | |
3fd3ab2d C |
1554 | await newVideoFile.save() |
1555 | ||
1556 | this.VideoFiles.push(newVideoFile) | |
0d0e8dd0 C |
1557 | } |
1558 | ||
0138af92 | 1559 | async importVideoFile (inputFilePath: string) { |
3a6f351b C |
1560 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) |
1561 | const { size } = await statPromise(inputFilePath) | |
1562 | const fps = await getVideoFileFPS(inputFilePath) | |
1563 | ||
0138af92 | 1564 | let updatedVideoFile = new VideoFileModel({ |
3a6f351b | 1565 | resolution: videoFileResolution, |
0138af92 | 1566 | extname: extname(inputFilePath), |
3a6f351b C |
1567 | size, |
1568 | fps, | |
0138af92 FF |
1569 | videoId: this.id |
1570 | }) | |
1571 | ||
0138af92 | 1572 | const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) |
0138af92 | 1573 | |
28be8916 C |
1574 | if (currentVideoFile) { |
1575 | // Remove old file and old torrent | |
1576 | await this.removeFile(currentVideoFile) | |
1577 | await this.removeTorrent(currentVideoFile) | |
1578 | // Remove the old video file from the array | |
1579 | this.VideoFiles = this.VideoFiles.filter(f => f !== currentVideoFile) | |
1580 | ||
1581 | // Update the database | |
1582 | currentVideoFile.set('extname', updatedVideoFile.extname) | |
0138af92 | 1583 | currentVideoFile.set('size', updatedVideoFile.size) |
3a6f351b | 1584 | currentVideoFile.set('fps', updatedVideoFile.fps) |
28be8916 | 1585 | |
0138af92 FF |
1586 | updatedVideoFile = currentVideoFile |
1587 | } | |
1588 | ||
6ccdf3a2 C |
1589 | const outputPath = this.getVideoFilePath(updatedVideoFile) |
1590 | await copyFilePromise(inputFilePath, outputPath) | |
1591 | ||
0138af92 FF |
1592 | await this.createTorrentAndSetInfoHash(updatedVideoFile) |
1593 | ||
1594 | await updatedVideoFile.save() | |
1595 | ||
28be8916 | 1596 | this.VideoFiles.push(updatedVideoFile) |
0138af92 FF |
1597 | } |
1598 | ||
056aa7f2 | 1599 | getOriginalFileResolution () { |
3fd3ab2d | 1600 | const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) |
0d0e8dd0 | 1601 | |
056aa7f2 | 1602 | return getVideoFileResolution(originalFilePath) |
3fd3ab2d | 1603 | } |
0d0e8dd0 | 1604 | |
3fd3ab2d C |
1605 | getDescriptionPath () { |
1606 | return `/api/${API_VERSION}/videos/${this.uuid}/description` | |
feb4bdfd C |
1607 | } |
1608 | ||
3fd3ab2d C |
1609 | removeThumbnail () { |
1610 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | |
1611 | return unlinkPromise(thumbnailPath) | |
ed31c059 | 1612 | .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err })) |
feb4bdfd C |
1613 | } |
1614 | ||
3fd3ab2d | 1615 | removePreview () { |
ed31c059 C |
1616 | const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) |
1617 | return unlinkPromise(previewPath) | |
1618 | .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err })) | |
7920c273 C |
1619 | } |
1620 | ||
3fd3ab2d C |
1621 | removeFile (videoFile: VideoFileModel) { |
1622 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
1623 | return unlinkPromise(filePath) | |
ed31c059 | 1624 | .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) |
feb4bdfd C |
1625 | } |
1626 | ||
3fd3ab2d C |
1627 | removeTorrent (videoFile: VideoFileModel) { |
1628 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | |
1629 | return unlinkPromise(torrentPath) | |
ed31c059 | 1630 | .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) |
aaf61f38 C |
1631 | } |
1632 | ||
093237cf C |
1633 | getActivityStreamDuration () { |
1634 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | |
1635 | return 'PT' + this.duration + 'S' | |
1636 | } | |
1637 | ||
3fd3ab2d C |
1638 | private getBaseUrls () { |
1639 | let baseUrlHttp | |
1640 | let baseUrlWs | |
7920c273 | 1641 | |
3fd3ab2d C |
1642 | if (this.isOwned()) { |
1643 | baseUrlHttp = CONFIG.WEBSERVER.URL | |
1644 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | |
1645 | } else { | |
50d6de9c C |
1646 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host |
1647 | baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host | |
6fcd19ba | 1648 | } |
aaf61f38 | 1649 | |
3fd3ab2d | 1650 | return { baseUrlHttp, baseUrlWs } |
15d4ee04 | 1651 | } |
a96aed15 | 1652 | |
3fd3ab2d C |
1653 | private getThumbnailUrl (baseUrlHttp: string) { |
1654 | return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName() | |
a96aed15 C |
1655 | } |
1656 | ||
3fd3ab2d C |
1657 | private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
1658 | return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) | |
1659 | } | |
e4f97bab | 1660 | |
02756fbd C |
1661 | private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
1662 | return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile) | |
1663 | } | |
1664 | ||
3fd3ab2d C |
1665 | private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
1666 | return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) | |
1667 | } | |
a96aed15 | 1668 | |
02756fbd C |
1669 | private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { |
1670 | return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile) | |
1671 | } | |
1672 | ||
3fd3ab2d C |
1673 | private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) { |
1674 | const xs = this.getTorrentUrl(videoFile, baseUrlHttp) | |
1675 | const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] | |
1676 | const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ] | |
1677 | ||
1678 | const magnetHash = { | |
1679 | xs, | |
1680 | announce, | |
1681 | urlList, | |
1682 | infoHash: videoFile.infoHash, | |
1683 | name: this.name | |
1684 | } | |
a96aed15 | 1685 | |
3fd3ab2d | 1686 | return magnetUtil.encode(magnetHash) |
a96aed15 | 1687 | } |
a96aed15 | 1688 | } |