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 { 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, MVideoFullLight, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
11 import { ManageVideoTorrentPayload, ThumbnailType, VideoCreate, VideoPrivacy, VideoState } from '@shared/models'
12 import { CreateJobArgument, 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 buildMoveToObjectStorageJob (options: {
92 previousVideoState: VideoState
93 isNewVideo?: boolean // Default true
95 const { video, previousVideoState, isNewVideo = true } = options
97 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
100 type: 'move-to-object-storage' as 'move-to-object-storage',
102 videoUUID: video.uuid,
109 // ---------------------------------------------------------------------------
111 async function getVideoDuration (videoId: number | string) {
112 const video = await VideoModel.load(videoId)
114 const duration = video.isLive
118 return { duration, isLive: video.isLive }
121 const getCachedVideoDuration = memoizee(getVideoDuration, {
123 max: MEMOIZE_LENGTH.VIDEO_DURATION,
124 maxAge: MEMOIZE_TTL.VIDEO_DURATION
127 // ---------------------------------------------------------------------------
129 async function addVideoJobsAfterUpdate (options: {
130 video: MVideoFullLight
134 oldPrivacy: VideoPrivacy
136 const { video, nameChanged, oldPrivacy, isNewVideo } = options
137 const jobs: CreateJobArgument[] = []
139 const filePathChanged = await moveFilesIfPrivacyChanged(video, oldPrivacy)
141 if (!video.isLive && (nameChanged || filePathChanged)) {
142 for (const file of (video.VideoFiles || [])) {
143 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
145 jobs.push({ type: 'manage-video-torrent', payload })
148 const hls = video.getHLSPlaylist()
150 for (const file of (hls?.VideoFiles || [])) {
151 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
153 jobs.push({ type: 'manage-video-torrent', payload })
158 type: 'federate-video',
160 videoUUID: video.uuid,
165 const wasConfidentialVideo = new Set([ VideoPrivacy.PRIVATE, VideoPrivacy.UNLISTED, VideoPrivacy.INTERNAL ]).has(oldPrivacy)
167 if (wasConfidentialVideo) {
172 videoUUID: video.uuid
177 return JobQueue.Instance.createSequentialJobFlow(...jobs)
180 // ---------------------------------------------------------------------------
183 buildLocalVideoFromReq,
184 buildVideoThumbnailsFromReq,
186 buildMoveToObjectStorageJob,
187 addVideoJobsAfterUpdate,
188 getCachedVideoDuration