1 import { UploadFiles } from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } 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'
13 import memoizee from 'memoizee'
15 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
19 category: videoInfo.category,
20 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
21 language: videoInfo.language,
22 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
23 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
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
31 ? new Date(videoInfo.originallyPublishedAt)
36 async function buildVideoThumbnailsFromReq (options: {
37 video: MVideoThumbnail
39 fallback: (type: ThumbnailType) => Promise<MThumbnail>
40 automaticallyGenerated?: boolean
42 const { video, files, fallback, automaticallyGenerated } = options
46 type: ThumbnailType.MINIATURE,
47 fieldName: 'thumbnailfile'
50 type: ThumbnailType.PREVIEW,
51 fieldName: 'previewfile'
54 const fields = files?.[p.fieldName]
57 return updateVideoMiniatureFromExisting({
58 inputPath: fields[0].path,
61 automaticallyGenerated: automaticallyGenerated || false
65 return fallback(p.type)
68 return Promise.all(promises)
71 // ---------------------------------------------------------------------------
73 async function setVideoTags (options: {
76 transaction?: Transaction
78 const { video, tags, transaction } = options
80 const internalTags = tags || []
81 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
83 await video.$set('Tags', tagInstances, { transaction })
84 video.Tags = tagInstances
87 // ---------------------------------------------------------------------------
89 async function addOptimizeOrMergeAudioJob (options: {
93 isNewVideo?: boolean // Default true
95 const { video, videoFile, user, isNewVideo } = options
97 let dataInput: VideoTranscodingPayload
99 if (videoFile.isAudio()) {
101 type: 'merge-audio-to-webtorrent',
102 resolution: DEFAULT_AUDIO_RESOLUTION,
103 videoUUID: video.uuid,
104 createHLSIfNeeded: true,
109 type: 'optimize-to-webtorrent',
110 videoUUID: video.uuid,
116 priority: await getTranscodingJobPriority(user)
119 return addTranscodingJob(dataInput, jobOptions)
122 async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
123 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
125 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload }, options)
128 async function getTranscodingJobPriority (user: MUserId) {
129 const now = new Date()
130 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
132 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
134 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
137 // ---------------------------------------------------------------------------
139 async function addMoveToObjectStorageJob (options: {
141 previousVideoState: VideoState
142 isNewVideo?: boolean // Default true
144 const { video, previousVideoState, isNewVideo = true } = options
146 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
148 const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState }
149 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
152 // ---------------------------------------------------------------------------
154 async function getVideoDuration (videoId: number | string) {
155 const video = await VideoModel.load(videoId)
157 const duration = video.isLive
161 return { duration, isLive: video.isLive }
164 const getCachedVideoDuration = memoizee(getVideoDuration, {
166 max: MEMOIZE_LENGTH.VIDEO_DURATION,
167 maxAge: MEMOIZE_TTL.VIDEO_DURATION
170 // ---------------------------------------------------------------------------
173 buildLocalVideoFromReq,
174 buildVideoThumbnailsFromReq,
176 addOptimizeOrMergeAudioJob,
178 addMoveToObjectStorageJob,
179 getTranscodingJobPriority,
180 getCachedVideoDuration