2021-06-02 03:41:46 -05:00
|
|
|
|
2021-06-03 09:56:42 -05:00
|
|
|
import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
|
2021-06-02 03:41:46 -05:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
|
|
|
import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
|
|
|
|
import { VideoModel } from '@server/models/video/video'
|
2021-06-02 04:54:29 -05:00
|
|
|
import { MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models'
|
2021-06-02 03:41:46 -05:00
|
|
|
import { VideoObject } from '@shared/models'
|
|
|
|
import { APVideoAbstractBuilder } from './abstract-builder'
|
|
|
|
import { getVideoAttributesFromObject } from './object-to-model-attributes'
|
|
|
|
|
|
|
|
export class APVideoCreator extends APVideoAbstractBuilder {
|
2021-06-03 09:56:42 -05:00
|
|
|
protected lTags: LoggerTagsFn
|
2021-06-02 03:41:46 -05:00
|
|
|
|
2021-06-02 04:54:29 -05:00
|
|
|
constructor (protected readonly videoObject: VideoObject) {
|
2021-06-02 03:41:46 -05:00
|
|
|
super()
|
2021-06-03 09:56:42 -05:00
|
|
|
|
|
|
|
this.lTags = loggerTagsFactory('ap', 'video', 'create', this.videoObject.uuid, this.videoObject.id)
|
2021-06-02 03:41:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async create (waitThumbnail = false) {
|
2021-06-03 09:56:42 -05:00
|
|
|
logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags())
|
2021-06-02 03:41:46 -05:00
|
|
|
|
2021-06-02 04:54:29 -05:00
|
|
|
const channelActor = await this.getOrCreateVideoChannelFromVideoObject()
|
|
|
|
const channel = channelActor.VideoChannel
|
|
|
|
|
|
|
|
const videoData = await getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to)
|
2021-06-02 03:41:46 -05:00
|
|
|
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
|
2021-06-02 04:54:29 -05:00
|
|
|
videoCreated.VideoChannel = channel
|
2021-06-02 03:41:46 -05:00
|
|
|
|
|
|
|
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
|
2021-06-02 04:54:29 -05:00
|
|
|
await channel.setAsUpdated(t)
|
2021-06-02 03:41:46 -05:00
|
|
|
|
|
|
|
const autoBlacklisted = await autoBlacklistVideoIfNeeded({
|
|
|
|
video: videoCreated,
|
|
|
|
user: undefined,
|
|
|
|
isRemote: true,
|
|
|
|
isNew: true,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
2021-06-03 09:56:42 -05:00
|
|
|
logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags())
|
2021-06-02 03:41:46 -05:00
|
|
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|