]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
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'
14
15 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
16 return {
17 name: videoInfo.name,
18 remote: false,
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,
29 channelId: channelId,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
31 ? new Date(videoInfo.originallyPublishedAt)
32 : null
33 }
34 }
35
36 async function buildVideoThumbnailsFromReq (options: {
37 video: MVideoThumbnail
38 files: UploadFiles
39 fallback: (type: ThumbnailType) => Promise<MThumbnail>
40 automaticallyGenerated?: boolean
41 }) {
42 const { video, files, fallback, automaticallyGenerated } = options
43
44 const promises = [
45 {
46 type: ThumbnailType.MINIATURE,
47 fieldName: 'thumbnailfile'
48 },
49 {
50 type: ThumbnailType.PREVIEW,
51 fieldName: 'previewfile'
52 }
53 ].map(p => {
54 const fields = files?.[p.fieldName]
55
56 if (fields) {
57 return updateVideoMiniatureFromExisting({
58 inputPath: fields[0].path,
59 video,
60 type: p.type,
61 automaticallyGenerated: automaticallyGenerated || false
62 })
63 }
64
65 return fallback(p.type)
66 })
67
68 return Promise.all(promises)
69 }
70
71 // ---------------------------------------------------------------------------
72
73 async function setVideoTags (options: {
74 video: MVideoTag
75 tags: string[]
76 transaction?: Transaction
77 }) {
78 const { video, tags, transaction } = options
79
80 const internalTags = tags || []
81 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
82
83 await video.$set('Tags', tagInstances, { transaction })
84 video.Tags = tagInstances
85 }
86
87 // ---------------------------------------------------------------------------
88
89 async function addOptimizeOrMergeAudioJob (options: {
90 video: MVideoUUID
91 videoFile: MVideoFile
92 user: MUserId
93 isNewVideo?: boolean // Default true
94 }) {
95 const { video, videoFile, user, isNewVideo } = options
96
97 let dataInput: VideoTranscodingPayload
98
99 if (videoFile.isAudio()) {
100 dataInput = {
101 type: 'merge-audio-to-webtorrent',
102 resolution: DEFAULT_AUDIO_RESOLUTION,
103 videoUUID: video.uuid,
104 createHLSIfNeeded: true,
105 isNewVideo
106 }
107 } else {
108 dataInput = {
109 type: 'optimize-to-webtorrent',
110 videoUUID: video.uuid,
111 isNewVideo
112 }
113 }
114
115 const jobOptions = {
116 priority: await getTranscodingJobPriority(user)
117 }
118
119 return addTranscodingJob(dataInput, jobOptions)
120 }
121
122 async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
123 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
124
125 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
126 }
127
128 async function getTranscodingJobPriority (user: MUserId) {
129 const now = new Date()
130 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
131
132 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
133
134 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
135 }
136
137 // ---------------------------------------------------------------------------
138
139 async function addMoveToObjectStorageJob (options: {
140 video: MVideoUUID
141 previousVideoState: VideoState
142 isNewVideo?: boolean // Default true
143 }) {
144 const { video, previousVideoState, isNewVideo = true } = options
145
146 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
147
148 const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState }
149 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
150 }
151
152 // ---------------------------------------------------------------------------
153
154 async function getVideoDuration (videoId: number | string) {
155 const video = await VideoModel.load(videoId)
156
157 const duration = video.isLive
158 ? undefined
159 : video.duration
160
161 return { duration, isLive: video.isLive }
162 }
163
164 const getCachedVideoDuration = memoizee(getVideoDuration, {
165 promise: true,
166 max: MEMOIZE_LENGTH.VIDEO_DURATION,
167 maxAge: MEMOIZE_TTL.VIDEO_DURATION
168 })
169
170 // ---------------------------------------------------------------------------
171
172 export {
173 buildLocalVideoFromReq,
174 buildVideoThumbnailsFromReq,
175 setVideoTags,
176 addOptimizeOrMergeAudioJob,
177 addTranscodingJob,
178 addMoveToObjectStorageJob,
179 getTranscodingJobPriority,
180 getCachedVideoDuration
181 }