]>
Commit | Line | Data |
---|---|---|
08a47c75 | 1 | |
46320694 | 2 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
08a47c75 C |
3 | import { sequelizeTypescript } from '@server/initializers/database' |
4 | import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist' | |
5 | import { VideoModel } from '@server/models/video/video' | |
c56faf0d | 6 | import { MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models' |
08a47c75 C |
7 | import { VideoObject } from '@shared/models' |
8 | import { APVideoAbstractBuilder } from './abstract-builder' | |
9 | import { getVideoAttributesFromObject } from './object-to-model-attributes' | |
10 | ||
11 | export class APVideoCreator extends APVideoAbstractBuilder { | |
46320694 | 12 | protected lTags = loggerTagsFactory('ap', 'video', 'create') |
08a47c75 | 13 | |
c56faf0d | 14 | constructor (protected readonly videoObject: VideoObject) { |
08a47c75 | 15 | super() |
08a47c75 C |
16 | } |
17 | ||
18 | async create (waitThumbnail = false) { | |
46320694 | 19 | logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags(this.videoObject.uuid)) |
08a47c75 | 20 | |
c56faf0d C |
21 | const channelActor = await this.getOrCreateVideoChannelFromVideoObject() |
22 | const channel = channelActor.VideoChannel | |
23 | ||
24 | const videoData = await getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to) | |
08a47c75 C |
25 | const video = VideoModel.build(videoData) as MVideoThumbnail |
26 | ||
27 | const promiseThumbnail = this.tryToGenerateThumbnail(video) | |
28 | ||
29 | let thumbnailModel: MThumbnail | |
30 | if (waitThumbnail === true) { | |
31 | thumbnailModel = await promiseThumbnail | |
32 | } | |
33 | ||
34 | const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { | |
35 | try { | |
36 | const videoCreated = await video.save({ transaction: t }) as MVideoFullLight | |
c56faf0d | 37 | videoCreated.VideoChannel = channel |
08a47c75 C |
38 | |
39 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
40 | ||
41 | await this.setPreview(videoCreated, t) | |
42 | await this.setWebTorrentFiles(videoCreated, t) | |
43 | await this.setStreamingPlaylists(videoCreated, t) | |
44 | await this.setTags(videoCreated, t) | |
45 | await this.setTrackers(videoCreated, t) | |
46 | await this.insertOrReplaceCaptions(videoCreated, t) | |
47 | await this.insertOrReplaceLive(videoCreated, t) | |
48 | ||
49 | // We added a video in this channel, set it as updated | |
c56faf0d | 50 | await channel.setAsUpdated(t) |
08a47c75 C |
51 | |
52 | const autoBlacklisted = await autoBlacklistVideoIfNeeded({ | |
53 | video: videoCreated, | |
54 | user: undefined, | |
55 | isRemote: true, | |
56 | isNew: true, | |
57 | transaction: t | |
58 | }) | |
59 | ||
46320694 | 60 | logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags(videoCreated.uuid)) |
08a47c75 C |
61 | |
62 | return { autoBlacklisted, videoCreated } | |
63 | } catch (err) { | |
64 | // FIXME: Use rollback hook when https://github.com/sequelize/sequelize/pull/13038 is released | |
65 | // Remove thumbnail | |
66 | if (thumbnailModel) await thumbnailModel.removeThumbnail() | |
67 | ||
68 | throw err | |
69 | } | |
70 | }) | |
71 | ||
72 | if (waitThumbnail === false) { | |
73 | // Error is already caught above | |
74 | // eslint-disable-next-line @typescript-eslint/no-floating-promises | |
75 | promiseThumbnail.then(thumbnailModel => { | |
76 | if (!thumbnailModel) return | |
77 | ||
78 | thumbnailModel = videoCreated.id | |
79 | ||
80 | return thumbnailModel.save() | |
81 | }) | |
82 | } | |
83 | ||
84 | return { autoBlacklisted, videoCreated } | |
85 | } | |
86 | } |