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