]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Fix live ending job that breaks new live session
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
f6d6e7f8 1import { UploadFiles } from 'express'
1ef65f4c 2import { Transaction } from 'sequelize/types'
77d7e851 3import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
1ef65f4c 4import { TagModel } from '@server/models/video/tag'
c6c0fa6c 5import { VideoModel } from '@server/models/video/video'
0305db28 6import { VideoJobInfoModel } from '@server/models/video/video-job-info'
c6c0fa6c 7import { FilteredModelAttributes } from '@server/types'
764b1a14 8import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
1808a1f8 9import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
0305db28 10import { CreateJobOptions, JobQueue } from './job-queue/job-queue'
91f8f8db 11import { updateVideoMiniatureFromExisting } from './thumbnail'
3cf68b86 12import { CONFIG } from '@server/initializers/config'
c6c0fa6c 13
1ef65f4c 14function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
c6c0fa6c
C
15 return {
16 name: videoInfo.name,
17 remote: false,
18 category: videoInfo.category,
3cf68b86 19 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
c6c0fa6c 20 language: videoInfo.language,
3cf68b86
C
21 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
22 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
c6c0fa6c 23 waitTranscoding: videoInfo.waitTranscoding || false,
c6c0fa6c
C
24 nsfw: videoInfo.nsfw || false,
25 description: videoInfo.description,
26 support: videoInfo.support,
27 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
c6c0fa6c
C
28 channelId: channelId,
29 originallyPublishedAt: videoInfo.originallyPublishedAt
16c016e8
C
30 ? new Date(videoInfo.originallyPublishedAt)
31 : null
c6c0fa6c
C
32 }
33}
34
1ef65f4c
C
35async function buildVideoThumbnailsFromReq (options: {
36 video: MVideoThumbnail
f6d6e7f8 37 files: UploadFiles
1ef65f4c
C
38 fallback: (type: ThumbnailType) => Promise<MThumbnail>
39 automaticallyGenerated?: boolean
40}) {
41 const { video, files, fallback, automaticallyGenerated } = options
42
43 const promises = [
44 {
45 type: ThumbnailType.MINIATURE,
46 fieldName: 'thumbnailfile'
47 },
48 {
49 type: ThumbnailType.PREVIEW,
50 fieldName: 'previewfile'
51 }
52 ].map(p => {
53 const fields = files?.[p.fieldName]
54
55 if (fields) {
91f8f8db 56 return updateVideoMiniatureFromExisting({
1ef65f4c
C
57 inputPath: fields[0].path,
58 video,
59 type: p.type,
60 automaticallyGenerated: automaticallyGenerated || false
61 })
62 }
63
64 return fallback(p.type)
65 })
66
67 return Promise.all(promises)
68}
69
1808a1f8
C
70// ---------------------------------------------------------------------------
71
1ef65f4c
C
72async function setVideoTags (options: {
73 video: MVideoTag
74 tags: string[]
75 transaction?: Transaction
1ef65f4c 76}) {
6c9c3b7b 77 const { video, tags, transaction } = options
1ef65f4c 78
6c9c3b7b
C
79 const internalTags = tags || []
80 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
81
82 await video.$set('Tags', tagInstances, { transaction })
83 video.Tags = tagInstances
1ef65f4c
C
84}
85
1808a1f8
C
86// ---------------------------------------------------------------------------
87
88async function addOptimizeOrMergeAudioJob (options: {
89 video: MVideoUUID
90 videoFile: MVideoFile
91 user: MUserId
92 isNewVideo?: boolean // Default true
93}) {
94 const { video, videoFile, user, isNewVideo } = options
95
77d7e851
C
96 let dataInput: VideoTranscodingPayload
97
98 if (videoFile.isAudio()) {
99 dataInput = {
100 type: 'merge-audio-to-webtorrent',
101 resolution: DEFAULT_AUDIO_RESOLUTION,
102 videoUUID: video.uuid,
0f11ec8d 103 createHLSIfNeeded: true,
c729caf6 104 isNewVideo
77d7e851
C
105 }
106 } else {
107 dataInput = {
108 type: 'optimize-to-webtorrent',
109 videoUUID: video.uuid,
c729caf6 110 isNewVideo
77d7e851
C
111 }
112 }
113
114 const jobOptions = {
a6e37eeb 115 priority: await getTranscodingJobPriority(user)
77d7e851
C
116 }
117
0305db28
JB
118 return addTranscodingJob(dataInput, jobOptions)
119}
120
ad5db104 121async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
0305db28
JB
122 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
123
124 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
125}
126
a6e37eeb 127async function getTranscodingJobPriority (user: MUserId) {
77d7e851
C
128 const now = new Date()
129 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
130
131 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
132
a6e37eeb 133 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
77d7e851
C
134}
135
c6c0fa6c
C
136// ---------------------------------------------------------------------------
137
1808a1f8
C
138async function addMoveToObjectStorageJob (options: {
139 video: MVideoUUID
140 previousVideoState: VideoState
141 isNewVideo?: boolean // Default true
142}) {
143 const { video, previousVideoState, isNewVideo = true } = options
144
145 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
146
147 const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState }
148 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
149}
150
151// ---------------------------------------------------------------------------
152
c6c0fa6c 153export {
1ef65f4c
C
154 buildLocalVideoFromReq,
155 buildVideoThumbnailsFromReq,
77d7e851
C
156 setVideoTags,
157 addOptimizeOrMergeAudioJob,
0305db28
JB
158 addTranscodingJob,
159 addMoveToObjectStorageJob,
a6e37eeb 160 getTranscodingJobPriority
c6c0fa6c 161}