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