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