2 import { logger } from '@server/helpers/logger'
3 import { sequelizeTypescript } from '@server/initializers/database'
4 import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
5 import { VideoModel } from '@server/models/video/video'
6 import { MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models'
7 import { VideoObject } from '@shared/models'
8 import { APVideoAbstractBuilder } from './abstract-builder'
9 import { getVideoAttributesFromObject } from './object-to-model-attributes'
11 export class APVideoCreator extends APVideoAbstractBuilder {
13 constructor (protected readonly videoObject: VideoObject) {
17 async create (waitThumbnail = false) {
18 logger.debug('Adding remote video %s.', this.videoObject.id)
20 const channelActor = await this.getOrCreateVideoChannelFromVideoObject()
21 const channel = channelActor.VideoChannel
23 const videoData = await getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to)
24 const video = VideoModel.build(videoData) as MVideoThumbnail
26 const promiseThumbnail = this.tryToGenerateThumbnail(video)
28 let thumbnailModel: MThumbnail
29 if (waitThumbnail === true) {
30 thumbnailModel = await promiseThumbnail
33 const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => {
35 const videoCreated = await video.save({ transaction: t }) as MVideoFullLight
36 videoCreated.VideoChannel = channel
38 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
40 await this.setPreview(videoCreated, t)
41 await this.setWebTorrentFiles(videoCreated, t)
42 await this.setStreamingPlaylists(videoCreated, t)
43 await this.setTags(videoCreated, t)
44 await this.setTrackers(videoCreated, t)
45 await this.insertOrReplaceCaptions(videoCreated, t)
46 await this.insertOrReplaceLive(videoCreated, t)
48 // We added a video in this channel, set it as updated
49 await channel.setAsUpdated(t)
51 const autoBlacklisted = await autoBlacklistVideoIfNeeded({
59 logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid)
61 return { autoBlacklisted, videoCreated }
63 // FIXME: Use rollback hook when https://github.com/sequelize/sequelize/pull/13038 is released
65 if (thumbnailModel) await thumbnailModel.removeThumbnail()
71 if (waitThumbnail === false) {
72 // Error is already caught above
73 // eslint-disable-next-line @typescript-eslint/no-floating-promises
74 promiseThumbnail.then(thumbnailModel => {
75 if (!thumbnailModel) return
77 thumbnailModel = videoCreated.id
79 return thumbnailModel.save()
83 return { autoBlacklisted, videoCreated }