]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
slightly clearer layout of transcoding configuration
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { forkJoin } from 'rxjs'
2 import { ViewportScroller } from '@angular/common'
3 import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { Notifier } from '@app/core'
6 import { ServerService } from '@app/core/server/server.service'
7 import {
8 ADMIN_EMAIL_VALIDATOR,
9 CACHE_CAPTIONS_SIZE_VALIDATOR,
10 CACHE_PREVIEWS_SIZE_VALIDATOR,
11 INDEX_URL_VALIDATOR,
12 INSTANCE_NAME_VALIDATOR,
13 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
14 SEARCH_INDEX_URL_VALIDATOR,
15 SERVICES_TWITTER_USERNAME_VALIDATOR,
16 SIGNUP_LIMIT_VALIDATOR,
17 TRANSCODING_THREADS_VALIDATOR
18 } from '@app/shared/form-validators/custom-config-validators'
19 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
20 import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
21 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
22 import { CustomConfig, ServerConfig } from '@shared/models'
23 import { pairwise } from 'rxjs/operators'
24
25 @Component({
26 selector: 'my-edit-custom-config',
27 templateUrl: './edit-custom-config.component.html',
28 styleUrls: [ './edit-custom-config.component.scss' ]
29 })
30 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
31 // FIXME: use built-in router
32 @ViewChild('nav') nav: NgbNav
33
34 initDone = false
35 customConfig: CustomConfig
36
37 resolutions: { id: string, label: string, description?: string }[] = []
38 liveResolutions: { id: string, label: string, description?: string }[] = []
39 transcodingThreadOptions: { label: string, value: number }[] = []
40 liveMaxDurationOptions: { label: string, value: number }[] = []
41
42 languageItems: SelectOptionsItem[] = []
43 categoryItems: SelectOptionsItem[] = []
44
45 signupAlertMessage: string
46
47 private serverConfig: ServerConfig
48
49 constructor (
50 private viewportScroller: ViewportScroller,
51 protected formValidatorService: FormValidatorService,
52 private notifier: Notifier,
53 private configService: ConfigService,
54 private serverService: ServerService
55 ) {
56 super()
57
58 this.resolutions = [
59 {
60 id: '0p',
61 label: $localize`Audio-only`,
62 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
63 },
64 {
65 id: '240p',
66 label: $localize`240p`
67 },
68 {
69 id: '360p',
70 label: $localize`360p`
71 },
72 {
73 id: '480p',
74 label: $localize`480p`
75 },
76 {
77 id: '720p',
78 label: $localize`720p`
79 },
80 {
81 id: '1080p',
82 label: $localize`1080p`
83 },
84 {
85 id: '2160p',
86 label: $localize`2160p`
87 }
88 ]
89
90 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
91
92 this.transcodingThreadOptions = [
93 { value: 0, label: $localize`Auto (via ffmpeg)` },
94 { value: 1, label: '1' },
95 { value: 2, label: '2' },
96 { value: 4, label: '4' },
97 { value: 8, label: '8' }
98 ]
99
100 this.liveMaxDurationOptions = [
101 { value: null, label: $localize`No limit` },
102 { value: 1000 * 3600, label: $localize`1 hour` },
103 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
104 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
105 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
106 ]
107 }
108
109 get videoQuotaOptions () {
110 return this.configService.videoQuotaOptions
111 }
112
113 get videoQuotaDailyOptions () {
114 return this.configService.videoQuotaDailyOptions
115 }
116
117 get availableThemes () {
118 return this.serverConfig.theme.registered
119 .map(t => t.name)
120 }
121
122 getTotalTranscodingThreads () {
123 const transcodingEnabled = this.form.value['transcoding']['enabled']
124 const transcodingThreads = this.form.value['transcoding']['threads']
125 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
126 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
127
128 // checks whether all enabled method are on fixed values and not on auto (= 0)
129 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
130 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
131
132 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
133 let value = 0
134 if (transcodingEnabled) value += +transcodingThreads || 1
135 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
136
137 return {
138 value,
139 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
140 unit: value > 1
141 ? $localize`threads`
142 : $localize`thread`
143 }
144 }
145
146 getResolutionKey (resolution: string) {
147 return 'transcoding.resolutions.' + resolution
148 }
149
150 ngOnInit () {
151 this.serverConfig = this.serverService.getTmpConfig()
152 this.serverService.getConfig()
153 .subscribe(config => {
154 this.serverConfig = config
155 })
156
157 const formGroupData: { [key in keyof CustomConfig ]: any } = {
158 instance: {
159 name: INSTANCE_NAME_VALIDATOR,
160 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
161 description: null,
162
163 isNSFW: false,
164 defaultNSFWPolicy: null,
165
166 terms: null,
167 codeOfConduct: null,
168
169 creationReason: null,
170 moderationInformation: null,
171 administrator: null,
172 maintenanceLifetime: null,
173 businessModel: null,
174
175 hardwareInformation: null,
176
177 categories: null,
178 languages: null,
179
180 defaultClientRoute: null,
181
182 customizations: {
183 javascript: null,
184 css: null
185 }
186 },
187 theme: {
188 default: null
189 },
190 services: {
191 twitter: {
192 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
193 whitelisted: null
194 }
195 },
196 cache: {
197 previews: {
198 size: CACHE_PREVIEWS_SIZE_VALIDATOR
199 },
200 captions: {
201 size: CACHE_CAPTIONS_SIZE_VALIDATOR
202 }
203 },
204 signup: {
205 enabled: null,
206 limit: SIGNUP_LIMIT_VALIDATOR,
207 requiresEmailVerification: null
208 },
209 import: {
210 videos: {
211 http: {
212 enabled: null
213 },
214 torrent: {
215 enabled: null
216 }
217 }
218 },
219 admin: {
220 email: ADMIN_EMAIL_VALIDATOR
221 },
222 contactForm: {
223 enabled: null
224 },
225 user: {
226 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
227 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
228 },
229 transcoding: {
230 enabled: null,
231 threads: TRANSCODING_THREADS_VALIDATOR,
232 allowAdditionalExtensions: null,
233 allowAudioFiles: null,
234 resolutions: {},
235 hls: {
236 enabled: null
237 },
238 webtorrent: {
239 enabled: null
240 }
241 },
242 live: {
243 enabled: null,
244
245 maxDuration: null,
246 maxInstanceLives: null,
247 maxUserLives: null,
248 allowReplay: null,
249
250 transcoding: {
251 enabled: null,
252 threads: TRANSCODING_THREADS_VALIDATOR,
253 resolutions: {}
254 }
255 },
256 autoBlacklist: {
257 videos: {
258 ofUsers: {
259 enabled: null
260 }
261 }
262 },
263 followers: {
264 instance: {
265 enabled: null,
266 manualApproval: null
267 }
268 },
269 followings: {
270 instance: {
271 autoFollowBack: {
272 enabled: null
273 },
274 autoFollowIndex: {
275 enabled: null,
276 indexUrl: INDEX_URL_VALIDATOR
277 }
278 }
279 },
280 broadcastMessage: {
281 enabled: null,
282 level: null,
283 dismissable: null,
284 message: null
285 },
286 search: {
287 remoteUri: {
288 users: null,
289 anonymous: null
290 },
291 searchIndex: {
292 enabled: null,
293 url: SEARCH_INDEX_URL_VALIDATOR,
294 disableLocalSearch: null,
295 isDefaultSearch: null
296 }
297 }
298 }
299
300 const defaultValues = {
301 transcoding: {
302 resolutions: {}
303 },
304 live: {
305 transcoding: {
306 resolutions: {}
307 }
308 }
309 }
310
311 for (const resolution of this.resolutions) {
312 defaultValues.transcoding.resolutions[resolution.id] = 'false'
313 formGroupData.transcoding.resolutions[resolution.id] = null
314 }
315
316 for (const resolution of this.liveResolutions) {
317 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
318 formGroupData.live.transcoding.resolutions[resolution.id] = null
319 }
320
321 this.buildForm(formGroupData)
322 this.loadForm()
323
324 this.checkTranscodingFields()
325 this.checkSignupField()
326 }
327
328 ngAfterViewChecked () {
329 if (!this.initDone) {
330 this.initDone = true
331 this.gotoAnchor()
332 }
333 }
334
335 isTranscodingEnabled () {
336 return this.form.value['transcoding']['enabled'] === true
337 }
338
339 isLiveEnabled () {
340 return this.form.value['live']['enabled'] === true
341 }
342
343 isLiveTranscodingEnabled () {
344 return this.form.value['live']['transcoding']['enabled'] === true
345 }
346
347 isSignupEnabled () {
348 return this.form.value['signup']['enabled'] === true
349 }
350
351 isSearchIndexEnabled () {
352 return this.form.value['search']['searchIndex']['enabled'] === true
353 }
354
355 isAutoFollowIndexEnabled () {
356 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
357 }
358
359 async formValidated () {
360 const value: CustomConfig = this.form.getRawValue()
361
362 // Transform "null" to null
363 const maxDuration = value.live.maxDuration as any
364 if (maxDuration === 'null') value.live.maxDuration = null
365
366 this.configService.updateCustomConfig(value)
367 .subscribe(
368 res => {
369 this.customConfig = res
370
371 // Reload general configuration
372 this.serverService.resetConfig()
373
374 this.updateForm()
375
376 this.notifier.success($localize`Configuration updated.`)
377 },
378
379 err => this.notifier.error(err.message)
380 )
381 }
382
383 gotoAnchor () {
384 const hashToNav = {
385 'customizations': 'advanced-configuration'
386 }
387 const hash = window.location.hash.replace('#', '')
388
389 if (hash && Object.keys(hashToNav).includes(hash)) {
390 this.nav.select(hashToNav[hash])
391 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
392 }
393 }
394
395 hasConsistentOptions () {
396 if (this.hasLiveAllowReplayConsistentOptions()) return true
397
398 return false
399 }
400
401 hasLiveAllowReplayConsistentOptions () {
402 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
403 return false
404 }
405
406 return true
407 }
408
409 private updateForm () {
410 this.form.patchValue(this.customConfig)
411 }
412
413 private loadForm () {
414 forkJoin([
415 this.configService.getCustomConfig(),
416 this.serverService.getVideoLanguages(),
417 this.serverService.getVideoCategories()
418 ]).subscribe(
419 ([ config, languages, categories ]) => {
420 this.customConfig = config
421
422 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
423 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
424
425 this.updateForm()
426 // Force form validation
427 this.forceCheck()
428 },
429
430 err => this.notifier.error(err.message)
431 )
432 }
433
434 private checkTranscodingFields () {
435 const hlsControl = this.form.get('transcoding.hls.enabled')
436 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
437
438 webtorrentControl.valueChanges
439 .subscribe(newValue => {
440 if (newValue === false && !hlsControl.disabled) {
441 hlsControl.disable()
442 }
443
444 if (newValue === true && !hlsControl.enabled) {
445 hlsControl.enable()
446 }
447 })
448
449 hlsControl.valueChanges
450 .subscribe(newValue => {
451 if (newValue === false && !webtorrentControl.disabled) {
452 webtorrentControl.disable()
453 }
454
455 if (newValue === true && !webtorrentControl.enabled) {
456 webtorrentControl.enable()
457 }
458 })
459 }
460
461 private checkSignupField () {
462 const signupControl = this.form.get('signup.enabled')
463
464 signupControl.valueChanges
465 .pipe(pairwise())
466 .subscribe(([ oldValue, newValue ]) => {
467 if (oldValue !== true && newValue === true) {
468 // tslint:disable:max-line-length
469 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
470
471 this.form.patchValue({
472 autoBlacklist: {
473 videos: {
474 ofUsers: {
475 enabled: true
476 }
477 }
478 }
479 })
480 }
481 })
482 }
483 }