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