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