]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/runners/jobs.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / runners / jobs.ts
1 import { UploadFilesForCheck } from 'express'
2 import validator from 'validator'
3 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4 import {
5 LiveRTMPHLSTranscodingSuccess,
6 RunnerJobSuccessPayload,
7 RunnerJobType,
8 RunnerJobUpdatePayload,
9 VideoStudioTranscodingSuccess,
10 VODAudioMergeTranscodingSuccess,
11 VODHLSTranscodingSuccess,
12 VODWebVideoTranscodingSuccess
13 } from '@shared/models'
14 import { exists, isFileValid, isSafeFilename } from '../misc'
15
16 const RUNNER_JOBS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.RUNNER_JOBS
17
18 const runnerJobTypes = new Set([ 'vod-hls-transcoding', 'vod-web-video-transcoding', 'vod-audio-merge-transcoding' ])
19 function isRunnerJobTypeValid (value: RunnerJobType) {
20 return runnerJobTypes.has(value)
21 }
22
23 function isRunnerJobSuccessPayloadValid (value: RunnerJobSuccessPayload, type: RunnerJobType, files: UploadFilesForCheck) {
24 return isRunnerJobVODWebVideoResultPayloadValid(value as VODWebVideoTranscodingSuccess, type, files) ||
25 isRunnerJobVODHLSResultPayloadValid(value as VODHLSTranscodingSuccess, type, files) ||
26 isRunnerJobVODAudioMergeResultPayloadValid(value as VODHLSTranscodingSuccess, type, files) ||
27 isRunnerJobLiveRTMPHLSResultPayloadValid(value as LiveRTMPHLSTranscodingSuccess, type) ||
28 isRunnerJobVideoStudioResultPayloadValid(value as VideoStudioTranscodingSuccess, type, files)
29 }
30
31 // ---------------------------------------------------------------------------
32
33 function isRunnerJobProgressValid (value: string) {
34 return validator.isInt(value + '', RUNNER_JOBS_CONSTRAINTS_FIELDS.PROGRESS)
35 }
36
37 function isRunnerJobUpdatePayloadValid (value: RunnerJobUpdatePayload, type: RunnerJobType, files: UploadFilesForCheck) {
38 return isRunnerJobVODWebVideoUpdatePayloadValid(value, type, files) ||
39 isRunnerJobVODHLSUpdatePayloadValid(value, type, files) ||
40 isRunnerJobVideoStudioUpdatePayloadValid(value, type, files) ||
41 isRunnerJobVODAudioMergeUpdatePayloadValid(value, type, files) ||
42 isRunnerJobLiveRTMPHLSUpdatePayloadValid(value, type, files)
43 }
44
45 // ---------------------------------------------------------------------------
46
47 function isRunnerJobTokenValid (value: string) {
48 return exists(value) && validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.TOKEN)
49 }
50
51 function isRunnerJobAbortReasonValid (value: string) {
52 return validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.REASON)
53 }
54
55 function isRunnerJobErrorMessageValid (value: string) {
56 return validator.isLength(value, RUNNER_JOBS_CONSTRAINTS_FIELDS.ERROR_MESSAGE)
57 }
58
59 // ---------------------------------------------------------------------------
60
61 export {
62 isRunnerJobTypeValid,
63 isRunnerJobSuccessPayloadValid,
64 isRunnerJobUpdatePayloadValid,
65 isRunnerJobTokenValid,
66 isRunnerJobErrorMessageValid,
67 isRunnerJobProgressValid,
68 isRunnerJobAbortReasonValid
69 }
70
71 // ---------------------------------------------------------------------------
72
73 function isRunnerJobVODWebVideoResultPayloadValid (
74 _value: VODWebVideoTranscodingSuccess,
75 type: RunnerJobType,
76 files: UploadFilesForCheck
77 ) {
78 return type === 'vod-web-video-transcoding' &&
79 isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null })
80 }
81
82 function isRunnerJobVODHLSResultPayloadValid (
83 _value: VODHLSTranscodingSuccess,
84 type: RunnerJobType,
85 files: UploadFilesForCheck
86 ) {
87 return type === 'vod-hls-transcoding' &&
88 isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null }) &&
89 isFileValid({ files, field: 'payload[resolutionPlaylistFile]', mimeTypeRegex: null, maxSize: null })
90 }
91
92 function isRunnerJobVODAudioMergeResultPayloadValid (
93 _value: VODAudioMergeTranscodingSuccess,
94 type: RunnerJobType,
95 files: UploadFilesForCheck
96 ) {
97 return type === 'vod-audio-merge-transcoding' &&
98 isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null })
99 }
100
101 function isRunnerJobLiveRTMPHLSResultPayloadValid (
102 value: LiveRTMPHLSTranscodingSuccess,
103 type: RunnerJobType
104 ) {
105 return type === 'live-rtmp-hls-transcoding' && (!value || (typeof value === 'object' && Object.keys(value).length === 0))
106 }
107
108 function isRunnerJobVideoStudioResultPayloadValid (
109 _value: VideoStudioTranscodingSuccess,
110 type: RunnerJobType,
111 files: UploadFilesForCheck
112 ) {
113 return type === 'video-studio-transcoding' &&
114 isFileValid({ files, field: 'payload[videoFile]', mimeTypeRegex: null, maxSize: null })
115 }
116
117 // ---------------------------------------------------------------------------
118
119 function isRunnerJobVODWebVideoUpdatePayloadValid (
120 value: RunnerJobUpdatePayload,
121 type: RunnerJobType,
122 _files: UploadFilesForCheck
123 ) {
124 return type === 'vod-web-video-transcoding' &&
125 (!value || (typeof value === 'object' && Object.keys(value).length === 0))
126 }
127
128 function isRunnerJobVODHLSUpdatePayloadValid (
129 value: RunnerJobUpdatePayload,
130 type: RunnerJobType,
131 _files: UploadFilesForCheck
132 ) {
133 return type === 'vod-hls-transcoding' &&
134 (!value || (typeof value === 'object' && Object.keys(value).length === 0))
135 }
136
137 function isRunnerJobVODAudioMergeUpdatePayloadValid (
138 value: RunnerJobUpdatePayload,
139 type: RunnerJobType,
140 _files: UploadFilesForCheck
141 ) {
142 return type === 'vod-audio-merge-transcoding' &&
143 (!value || (typeof value === 'object' && Object.keys(value).length === 0))
144 }
145
146 function isRunnerJobLiveRTMPHLSUpdatePayloadValid (
147 value: RunnerJobUpdatePayload,
148 type: RunnerJobType,
149 files: UploadFilesForCheck
150 ) {
151 let result = type === 'live-rtmp-hls-transcoding' && !!value && !!files
152
153 result &&= isFileValid({ files, field: 'payload[masterPlaylistFile]', mimeTypeRegex: null, maxSize: null, optional: true })
154
155 result &&= isFileValid({
156 files,
157 field: 'payload[resolutionPlaylistFile]',
158 mimeTypeRegex: null,
159 maxSize: null,
160 optional: !value.resolutionPlaylistFilename
161 })
162
163 if (files['payload[resolutionPlaylistFile]']) {
164 result &&= isSafeFilename(value.resolutionPlaylistFilename, 'm3u8')
165 }
166
167 return result &&
168 isSafeFilename(value.videoChunkFilename, 'ts') &&
169 (
170 (
171 value.type === 'remove-chunk'
172 ) ||
173 (
174 value.type === 'add-chunk' &&
175 isFileValid({ files, field: 'payload[videoChunkFile]', mimeTypeRegex: null, maxSize: null })
176 )
177 )
178 }
179
180 function isRunnerJobVideoStudioUpdatePayloadValid (
181 value: RunnerJobUpdatePayload,
182 type: RunnerJobType,
183 _files: UploadFilesForCheck
184 ) {
185 return type === 'video-studio-transcoding' &&
186 (!value || (typeof value === 'object' && Object.keys(value).length === 0))
187 }