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, VideoTranscodingPayload } from '@shared/models'
10 import { CreateJobOptions, JobQueue } from './job-queue/job-queue'
11 import { updateVideoMiniatureFromExisting } from './thumbnail'
13 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
17 category: videoInfo.category,
18 licence: videoInfo.licence,
19 language: videoInfo.language,
20 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
21 downloadEnabled: videoInfo.downloadEnabled !== false,
22 waitTranscoding: videoInfo.waitTranscoding || false,
23 nsfw: videoInfo.nsfw || false,
24 description: videoInfo.description,
25 support: videoInfo.support,
26 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
28 originallyPublishedAt: videoInfo.originallyPublishedAt
29 ? new Date(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 updateVideoMiniatureFromExisting({
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 addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId) {
84 let dataInput: VideoTranscodingPayload
86 if (videoFile.isAudio()) {
88 type: 'merge-audio-to-webtorrent',
89 resolution: DEFAULT_AUDIO_RESOLUTION,
90 videoUUID: video.uuid,
95 type: 'optimize-to-webtorrent',
96 videoUUID: video.uuid,
102 priority: await getTranscodingJobPriority(user)
105 return addTranscodingJob(dataInput, jobOptions)
108 async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions) {
109 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
111 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
114 async function addMoveToObjectStorageJob (video: MVideoUUID, isNewVideo = true) {
115 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
117 const dataInput = { videoUUID: video.uuid, isNewVideo }
118 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
121 async function getTranscodingJobPriority (user: MUserId) {
122 const now = new Date()
123 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
125 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
127 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
130 // ---------------------------------------------------------------------------
133 buildLocalVideoFromReq,
134 buildVideoThumbnailsFromReq,
136 addOptimizeOrMergeAudioJob,
138 addMoveToObjectStorageJob,
139 getTranscodingJobPriority