+ ? new Date(videoInfo.originallyPublishedAt)
+ : null
+ }
+}
+
+async function buildVideoThumbnailsFromReq (options: {
+ video: MVideoThumbnail
+ files: UploadFiles
+ fallback: (type: ThumbnailType) => Promise<MThumbnail>
+ automaticallyGenerated?: boolean
+}) {
+ const { video, files, fallback, automaticallyGenerated } = options
+
+ const promises = [
+ {
+ type: ThumbnailType.MINIATURE,
+ fieldName: 'thumbnailfile'
+ },
+ {
+ type: ThumbnailType.PREVIEW,
+ fieldName: 'previewfile'
+ }
+ ].map(p => {
+ const fields = files?.[p.fieldName]
+
+ if (fields) {
+ return updateVideoMiniatureFromExisting({
+ inputPath: fields[0].path,
+ video,
+ type: p.type,
+ automaticallyGenerated: automaticallyGenerated || false
+ })
+ }
+
+ return fallback(p.type)
+ })
+
+ return Promise.all(promises)
+}
+
+// ---------------------------------------------------------------------------
+
+async function setVideoTags (options: {
+ video: MVideoTag
+ tags: string[]
+ transaction?: Transaction
+}) {
+ const { video, tags, transaction } = options
+
+ const internalTags = tags || []
+ const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
+
+ await video.$set('Tags', tagInstances, { transaction })
+ video.Tags = tagInstances
+}
+
+// ---------------------------------------------------------------------------
+
+async function addOptimizeOrMergeAudioJob (options: {
+ video: MVideoUUID
+ videoFile: MVideoFile
+ user: MUserId
+ isNewVideo?: boolean // Default true
+}) {
+ const { video, videoFile, user, isNewVideo } = options
+
+ let dataInput: VideoTranscodingPayload
+
+ if (videoFile.isAudio()) {
+ dataInput = {
+ type: 'merge-audio-to-webtorrent',
+ resolution: DEFAULT_AUDIO_RESOLUTION,
+ videoUUID: video.uuid,
+ createHLSIfNeeded: true,
+ isNewVideo
+ }
+ } else {
+ dataInput = {
+ type: 'optimize-to-webtorrent',
+ videoUUID: video.uuid,
+ isNewVideo
+ }