1 import { UploadFiles } from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
4 import { TagModel } from '@server/models/video/tag'
5 import { VideoModel } from '@server/models/video/video'
6 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
9 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
10 import { CreateJobOptions, JobQueue } from './job-queue/job-queue'
11 import { updateVideoMiniatureFromExisting } from './thumbnail'
12 import { CONFIG } from '@server/initializers/config'
14 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
18 category: videoInfo.category,
19 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
20 language: videoInfo.language,
21 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
22 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
23 waitTranscoding: videoInfo.waitTranscoding || false,
24 nsfw: videoInfo.nsfw || false,
25 description: videoInfo.description,
26 support: videoInfo.support,
27 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
29 originallyPublishedAt: videoInfo.originallyPublishedAt
30 ? new Date(videoInfo.originallyPublishedAt)
35 async function buildVideoThumbnailsFromReq (options: {
36 video: MVideoThumbnail
38 fallback: (type: ThumbnailType) => Promise<MThumbnail>
39 automaticallyGenerated?: boolean
41 const { video, files, fallback, automaticallyGenerated } = options
45 type: ThumbnailType.MINIATURE,
46 fieldName: 'thumbnailfile'
49 type: ThumbnailType.PREVIEW,
50 fieldName: 'previewfile'
53 const fields = files?.[p.fieldName]
56 return updateVideoMiniatureFromExisting({
57 inputPath: fields[0].path,
60 automaticallyGenerated: automaticallyGenerated || false
64 return fallback(p.type)
67 return Promise.all(promises)
70 // ---------------------------------------------------------------------------
72 async function setVideoTags (options: {
75 transaction?: Transaction
77 const { video, tags, transaction } = options
79 const internalTags = tags || []
80 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
82 await video.$set('Tags', tagInstances, { transaction })
83 video.Tags = tagInstances
86 // ---------------------------------------------------------------------------
88 async function addOptimizeOrMergeAudioJob (options: {
92 isNewVideo?: boolean // Default true
94 const { video, videoFile, user, isNewVideo } = options
96 let dataInput: VideoTranscodingPayload
98 if (videoFile.isAudio()) {
100 type: 'merge-audio-to-webtorrent',
101 resolution: DEFAULT_AUDIO_RESOLUTION,
102 videoUUID: video.uuid,
103 createHLSIfNeeded: true,
108 type: 'optimize-to-webtorrent',
109 videoUUID: video.uuid,
115 priority: await getTranscodingJobPriority(user)
118 return addTranscodingJob(dataInput, jobOptions)
121 async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
122 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
124 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
127 async function getTranscodingJobPriority (user: MUserId) {
128 const now = new Date()
129 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
131 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
133 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
136 // ---------------------------------------------------------------------------
138 async function addMoveToObjectStorageJob (options: {
140 previousVideoState: VideoState
141 isNewVideo?: boolean // Default true
143 const { video, previousVideoState, isNewVideo = true } = options
145 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
147 const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState }
148 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
151 // ---------------------------------------------------------------------------
154 buildLocalVideoFromReq,
155 buildVideoThumbnailsFromReq,
157 addOptimizeOrMergeAudioJob,
159 addMoveToObjectStorageJob,
160 getTranscodingJobPriority