1 import { UploadFiles } from 'express'
2 import memoizee from 'memoizee'
3 import { Transaction } from 'sequelize/types'
4 import { CONFIG } from '@server/initializers/config'
5 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants'
6 import { TagModel } from '@server/models/video/tag'
7 import { VideoModel } from '@server/models/video/video'
8 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
9 import { FilteredModelAttributes } from '@server/types'
10 import { MThumbnail, MUserId, MVideoFile, MVideoFullLight, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
11 import { ManageVideoTorrentPayload, ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
12 import { CreateJobArgument, CreateJobOptions, JobQueue } from './job-queue/job-queue'
13 import { updateVideoMiniatureFromExisting } from './thumbnail'
14 import { moveFilesIfPrivacyChanged } from './video-privacy'
16 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
20 category: videoInfo.category,
21 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
22 language: videoInfo.language,
23 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
24 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
25 waitTranscoding: videoInfo.waitTranscoding || false,
26 nsfw: videoInfo.nsfw || false,
27 description: videoInfo.description,
28 support: videoInfo.support,
29 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
31 originallyPublishedAt: videoInfo.originallyPublishedAt
32 ? new Date(videoInfo.originallyPublishedAt)
37 async function buildVideoThumbnailsFromReq (options: {
38 video: MVideoThumbnail
40 fallback: (type: ThumbnailType) => Promise<MThumbnail>
41 automaticallyGenerated?: boolean
43 const { video, files, fallback, automaticallyGenerated } = options
47 type: ThumbnailType.MINIATURE,
48 fieldName: 'thumbnailfile'
51 type: ThumbnailType.PREVIEW,
52 fieldName: 'previewfile'
55 const fields = files?.[p.fieldName]
58 return updateVideoMiniatureFromExisting({
59 inputPath: fields[0].path,
62 automaticallyGenerated: automaticallyGenerated || false
66 return fallback(p.type)
69 return Promise.all(promises)
72 // ---------------------------------------------------------------------------
74 async function setVideoTags (options: {
77 transaction?: Transaction
79 const { video, tags, transaction } = options
81 const internalTags = tags || []
82 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
84 await video.$set('Tags', tagInstances, { transaction })
85 video.Tags = tagInstances
88 // ---------------------------------------------------------------------------
90 async function buildOptimizeOrMergeAudioJob (options: {
94 isNewVideo?: boolean // Default true
96 const { video, videoFile, user, isNewVideo } = options
98 let payload: VideoTranscodingPayload
100 if (videoFile.isAudio()) {
102 type: 'merge-audio-to-webtorrent',
103 resolution: DEFAULT_AUDIO_RESOLUTION,
104 videoUUID: video.uuid,
105 createHLSIfNeeded: true,
110 type: 'optimize-to-webtorrent',
111 videoUUID: video.uuid,
116 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
119 type: 'video-transcoding' as 'video-transcoding',
120 priority: await getTranscodingJobPriority(user),
125 async function buildTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
126 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
128 return { type: 'video-transcoding' as 'video-transcoding', payload, ...options }
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 // ---------------------------------------------------------------------------
142 async function buildMoveToObjectStorageJob (options: {
144 previousVideoState: VideoState
145 isNewVideo?: boolean // Default true
147 const { video, previousVideoState, isNewVideo = true } = options
149 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
152 type: 'move-to-object-storage' as 'move-to-object-storage',
154 videoUUID: video.uuid,
161 // ---------------------------------------------------------------------------
163 async function getVideoDuration (videoId: number | string) {
164 const video = await VideoModel.load(videoId)
166 const duration = video.isLive
170 return { duration, isLive: video.isLive }
173 const getCachedVideoDuration = memoizee(getVideoDuration, {
175 max: MEMOIZE_LENGTH.VIDEO_DURATION,
176 maxAge: MEMOIZE_TTL.VIDEO_DURATION
179 // ---------------------------------------------------------------------------
181 async function addVideoJobsAfterUpdate (options: {
182 video: MVideoFullLight
186 oldPrivacy: VideoPrivacy
188 const { video, nameChanged, oldPrivacy, isNewVideo } = options
189 const jobs: CreateJobArgument[] = []
191 const filePathChanged = await moveFilesIfPrivacyChanged(video, oldPrivacy)
193 if (!video.isLive && (nameChanged || filePathChanged)) {
194 for (const file of (video.VideoFiles || [])) {
195 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
197 jobs.push({ type: 'manage-video-torrent', payload })
200 const hls = video.getHLSPlaylist()
202 for (const file of (hls?.VideoFiles || [])) {
203 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
205 jobs.push({ type: 'manage-video-torrent', payload })
210 type: 'federate-video',
212 videoUUID: video.uuid,
217 const wasConfidentialVideo = new Set([ VideoPrivacy.PRIVATE, VideoPrivacy.UNLISTED, VideoPrivacy.INTERNAL ]).has(oldPrivacy)
219 if (wasConfidentialVideo) {
224 videoUUID: video.uuid
229 return JobQueue.Instance.createSequentialJobFlow(...jobs)
232 // ---------------------------------------------------------------------------
235 buildLocalVideoFromReq,
236 buildVideoThumbnailsFromReq,
238 buildOptimizeOrMergeAudioJob,
240 buildMoveToObjectStorageJob,
241 getTranscodingJobPriority,
242 addVideoJobsAfterUpdate,
243 getCachedVideoDuration