]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/server/config-command.ts
Fix print transcode command test
[github/Chocobozzz/PeerTube.git] / shared / server-commands / server / config-command.ts
1 import { merge } from 'lodash'
2 import { About, CustomConfig, HttpStatusCode, ServerConfig } from '@shared/models'
3 import { DeepPartial } from '@shared/typescript-utils'
4 import { AbstractCommand, OverrideCommandOptions } from '../shared/abstract-command'
5
6 export class ConfigCommand extends AbstractCommand {
7
8 static getCustomConfigResolutions (enabled: boolean) {
9 return {
10 '144p': enabled,
11 '240p': enabled,
12 '360p': enabled,
13 '480p': enabled,
14 '720p': enabled,
15 '1080p': enabled,
16 '1440p': enabled,
17 '2160p': enabled
18 }
19 }
20
21 enableImports () {
22 return this.updateExistingSubConfig({
23 newConfig: {
24 import: {
25 videos: {
26 http: {
27 enabled: true
28 },
29
30 torrent: {
31 enabled: true
32 }
33 }
34 }
35 }
36 })
37 }
38
39 enableLive (options: {
40 allowReplay?: boolean
41 transcoding?: boolean
42 resolutions?: 'min' | 'max' // Default max
43 } = {}) {
44 const { allowReplay, transcoding, resolutions = 'max' } = options
45
46 return this.updateExistingSubConfig({
47 newConfig: {
48 live: {
49 enabled: true,
50 allowReplay: allowReplay ?? true,
51 transcoding: {
52 enabled: transcoding ?? true,
53 resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
54 }
55 }
56 }
57 })
58 }
59
60 disableTranscoding () {
61 return this.updateExistingSubConfig({
62 newConfig: {
63 transcoding: {
64 enabled: false
65 },
66 videoStudio: {
67 enabled: false
68 }
69 }
70 })
71 }
72
73 enableTranscoding (webtorrent = true, hls = true) {
74 return this.updateExistingSubConfig({
75 newConfig: {
76 transcoding: {
77 enabled: true,
78
79 allowAudioFiles: true,
80 allowAdditionalExtensions: true,
81
82 resolutions: ConfigCommand.getCustomConfigResolutions(true),
83
84 webtorrent: {
85 enabled: webtorrent
86 },
87 hls: {
88 enabled: hls
89 }
90 }
91 }
92 })
93 }
94
95 enableMinimumTranscoding (webtorrent = true, hls = true) {
96 return this.updateExistingSubConfig({
97 newConfig: {
98 transcoding: {
99 enabled: true,
100 resolutions: {
101 ...ConfigCommand.getCustomConfigResolutions(false),
102
103 '240p': true
104 },
105
106 webtorrent: {
107 enabled: webtorrent
108 },
109 hls: {
110 enabled: hls
111 }
112 }
113 }
114 })
115 }
116
117 enableStudio () {
118 return this.updateExistingSubConfig({
119 newConfig: {
120 videoStudio: {
121 enabled: true
122 }
123 }
124 })
125 }
126
127 getConfig (options: OverrideCommandOptions = {}) {
128 const path = '/api/v1/config'
129
130 return this.getRequestBody<ServerConfig>({
131 ...options,
132
133 path,
134 implicitToken: false,
135 defaultExpectedStatus: HttpStatusCode.OK_200
136 })
137 }
138
139 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
140 const text = await this.getRequestText({
141 ...options,
142
143 path: '/',
144 implicitToken: false,
145 defaultExpectedStatus: HttpStatusCode.OK_200
146 })
147
148 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
149
150 // We parse the string twice, first to extract the string and then to extract the JSON
151 return JSON.parse(JSON.parse(match[1])) as ServerConfig
152 }
153
154 getAbout (options: OverrideCommandOptions = {}) {
155 const path = '/api/v1/config/about'
156
157 return this.getRequestBody<About>({
158 ...options,
159
160 path,
161 implicitToken: false,
162 defaultExpectedStatus: HttpStatusCode.OK_200
163 })
164 }
165
166 getCustomConfig (options: OverrideCommandOptions = {}) {
167 const path = '/api/v1/config/custom'
168
169 return this.getRequestBody<CustomConfig>({
170 ...options,
171
172 path,
173 implicitToken: true,
174 defaultExpectedStatus: HttpStatusCode.OK_200
175 })
176 }
177
178 updateCustomConfig (options: OverrideCommandOptions & {
179 newCustomConfig: CustomConfig
180 }) {
181 const path = '/api/v1/config/custom'
182
183 return this.putBodyRequest({
184 ...options,
185
186 path,
187 fields: options.newCustomConfig,
188 implicitToken: true,
189 defaultExpectedStatus: HttpStatusCode.OK_200
190 })
191 }
192
193 deleteCustomConfig (options: OverrideCommandOptions = {}) {
194 const path = '/api/v1/config/custom'
195
196 return this.deleteRequest({
197 ...options,
198
199 path,
200 implicitToken: true,
201 defaultExpectedStatus: HttpStatusCode.OK_200
202 })
203 }
204
205 async updateExistingSubConfig (options: OverrideCommandOptions & {
206 newConfig: DeepPartial<CustomConfig>
207 }) {
208 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
209
210 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
211 }
212
213 updateCustomSubConfig (options: OverrideCommandOptions & {
214 newConfig: DeepPartial<CustomConfig>
215 }) {
216 const newCustomConfig: CustomConfig = {
217 instance: {
218 name: 'PeerTube updated',
219 shortDescription: 'my short description',
220 description: 'my super description',
221 terms: 'my super terms',
222 codeOfConduct: 'my super coc',
223
224 creationReason: 'my super creation reason',
225 moderationInformation: 'my super moderation information',
226 administrator: 'Kuja',
227 maintenanceLifetime: 'forever',
228 businessModel: 'my super business model',
229 hardwareInformation: '2vCore 3GB RAM',
230
231 languages: [ 'en', 'es' ],
232 categories: [ 1, 2 ],
233
234 isNSFW: true,
235 defaultNSFWPolicy: 'blur',
236
237 defaultClientRoute: '/videos/recently-added',
238
239 customizations: {
240 javascript: 'alert("coucou")',
241 css: 'body { background-color: red; }'
242 }
243 },
244 theme: {
245 default: 'default'
246 },
247 services: {
248 twitter: {
249 username: '@MySuperUsername',
250 whitelisted: true
251 }
252 },
253 client: {
254 videos: {
255 miniature: {
256 preferAuthorDisplayName: false
257 }
258 },
259 menu: {
260 login: {
261 redirectOnSingleExternalAuth: false
262 }
263 }
264 },
265 cache: {
266 previews: {
267 size: 2
268 },
269 captions: {
270 size: 3
271 },
272 torrents: {
273 size: 4
274 }
275 },
276 signup: {
277 enabled: false,
278 limit: 5,
279 requiresEmailVerification: false,
280 minimumAge: 16
281 },
282 admin: {
283 email: 'superadmin1@example.com'
284 },
285 contactForm: {
286 enabled: true
287 },
288 user: {
289 videoQuota: 5242881,
290 videoQuotaDaily: 318742
291 },
292 videoChannels: {
293 maxPerUser: 20
294 },
295 transcoding: {
296 enabled: true,
297 allowAdditionalExtensions: true,
298 allowAudioFiles: true,
299 threads: 1,
300 concurrency: 3,
301 profile: 'default',
302 resolutions: {
303 '0p': false,
304 '144p': false,
305 '240p': false,
306 '360p': true,
307 '480p': true,
308 '720p': false,
309 '1080p': false,
310 '1440p': false,
311 '2160p': false
312 },
313 alwaysTranscodeOriginalResolution: true,
314 webtorrent: {
315 enabled: true
316 },
317 hls: {
318 enabled: false
319 }
320 },
321 live: {
322 enabled: true,
323 allowReplay: false,
324 latencySetting: {
325 enabled: false
326 },
327 maxDuration: -1,
328 maxInstanceLives: -1,
329 maxUserLives: 50,
330 transcoding: {
331 enabled: true,
332 threads: 4,
333 profile: 'default',
334 resolutions: {
335 '144p': true,
336 '240p': true,
337 '360p': true,
338 '480p': true,
339 '720p': true,
340 '1080p': true,
341 '1440p': true,
342 '2160p': true
343 },
344 alwaysTranscodeOriginalResolution: true
345 }
346 },
347 videoStudio: {
348 enabled: false
349 },
350 import: {
351 videos: {
352 concurrency: 3,
353 http: {
354 enabled: false
355 },
356 torrent: {
357 enabled: false
358 }
359 }
360 },
361 trending: {
362 videos: {
363 algorithms: {
364 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
365 default: 'hot'
366 }
367 }
368 },
369 autoBlacklist: {
370 videos: {
371 ofUsers: {
372 enabled: false
373 }
374 }
375 },
376 followers: {
377 instance: {
378 enabled: true,
379 manualApproval: false
380 }
381 },
382 followings: {
383 instance: {
384 autoFollowBack: {
385 enabled: false
386 },
387 autoFollowIndex: {
388 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
389 enabled: false
390 }
391 }
392 },
393 broadcastMessage: {
394 enabled: true,
395 level: 'warning',
396 message: 'hello',
397 dismissable: true
398 },
399 search: {
400 remoteUri: {
401 users: true,
402 anonymous: true
403 },
404 searchIndex: {
405 enabled: true,
406 url: 'https://search.joinpeertube.org',
407 disableLocalSearch: true,
408 isDefaultSearch: true
409 }
410 }
411 }
412
413 merge(newCustomConfig, options.newConfig)
414
415 return this.updateCustomConfig({ ...options, newCustomConfig })
416 }
417 }