1 import { UploadFiles } from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
4 import { sequelizeTypescript } from '@server/initializers/database'
5 import { TagModel } from '@server/models/video/tag'
6 import { VideoModel } from '@server/models/video/video'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
9 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
10 import { federateVideoIfNeeded } from './activitypub/videos'
11 import { JobQueue } from './job-queue/job-queue'
12 import { Notifier } from './notifier'
13 import { createVideoMiniatureFromExisting } from './thumbnail'
15 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
19 category: videoInfo.category,
20 licence: videoInfo.licence,
21 language: videoInfo.language,
22 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
23 downloadEnabled: videoInfo.downloadEnabled !== false,
24 waitTranscoding: videoInfo.waitTranscoding || false,
25 nsfw: videoInfo.nsfw || false,
26 description: videoInfo.description,
27 support: videoInfo.support,
28 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
34 async function buildVideoThumbnailsFromReq (options: {
35 video: MVideoThumbnail
37 fallback: (type: ThumbnailType) => Promise<MThumbnail>
38 automaticallyGenerated?: boolean
40 const { video, files, fallback, automaticallyGenerated } = options
44 type: ThumbnailType.MINIATURE,
45 fieldName: 'thumbnailfile'
48 type: ThumbnailType.PREVIEW,
49 fieldName: 'previewfile'
52 const fields = files?.[p.fieldName]
55 return createVideoMiniatureFromExisting({
56 inputPath: fields[0].path,
59 automaticallyGenerated: automaticallyGenerated || false
63 return fallback(p.type)
66 return Promise.all(promises)
69 async function setVideoTags (options: {
72 transaction?: Transaction
74 const { video, tags, transaction } = options
76 const internalTags = tags || []
77 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
79 await video.$set('Tags', tagInstances, { transaction })
80 video.Tags = tagInstances
83 async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
84 const result = await sequelizeTypescript.transaction(async t => {
85 // Maybe the video changed in database, refresh it
86 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
87 // Video does not exist anymore
88 if (!videoDatabase) return undefined
90 // We transcoded the video file in another format, now we can publish it
91 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
93 // If the video was not published, we consider it is a new one for other instances
94 // Live videos are always federated, so it's not a new video
95 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
97 return { videoDatabase, videoPublished }
100 if (result?.videoPublished) {
101 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
102 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
106 async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) {
107 let dataInput: VideoTranscodingPayload
109 if (videoFile.isAudio()) {
111 type: 'merge-audio-to-webtorrent',
112 resolution: DEFAULT_AUDIO_RESOLUTION,
113 videoUUID: video.uuid,
118 type: 'optimize-to-webtorrent',
119 videoUUID: video.uuid,
125 priority: await getTranscodingJobPriority(user)
128 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
131 async function getTranscodingJobPriority (user: MUserId) {
132 const now = new Date()
133 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
135 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
137 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
140 // ---------------------------------------------------------------------------
143 buildLocalVideoFromReq,
144 publishAndFederateIfNeeded,
145 buildVideoThumbnailsFromReq,
147 addOptimizeOrMergeAudioJob,
148 getTranscodingJobPriority