1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { ConfirmService } from '@app/core'
6 import { ServerService } from '@app/core/server/server.service'
7 import { FormReactive, USER_VIDEO_QUOTA } from '@app/shared'
11 INSTANCE_NAME, INSTANCE_SHORT_DESCRIPTION,
14 } from '@app/shared/forms/form-validators/custom-config'
15 import { NotificationsService } from 'angular2-notifications'
16 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
19 selector: 'my-edit-custom-config',
20 templateUrl: './edit-custom-config.component.html',
21 styleUrls: [ './edit-custom-config.component.scss' ]
23 export class EditCustomConfigComponent extends FormReactive implements OnInit {
24 customConfig: CustomConfig
25 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
28 { value: -1, label: 'Unlimited' },
29 { value: 0, label: '0' },
30 { value: 100 * 1024 * 1024, label: '100MB' },
31 { value: 500 * 1024 * 1024, label: '500MB' },
32 { value: 1024 * 1024 * 1024, label: '1GB' },
33 { value: 5 * 1024 * 1024 * 1024, label: '5GB' },
34 { value: 20 * 1024 * 1024 * 1024, label: '20GB' },
35 { value: 50 * 1024 * 1024 * 1024, label: '50GB' }
37 transcodingThreadOptions = [
38 { value: 1, label: '1' },
39 { value: 2, label: '2' },
40 { value: 4, label: '4' },
41 { value: 8, label: '8' }
47 instanceShortDescription: '',
48 instanceDescription: '',
50 instanceDefaultClientRoute: '',
51 cachePreviewsSize: '',
55 transcodingThreads: '',
56 customizationJavascript: '',
59 validationMessages = {
60 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES,
61 instanceName: INSTANCE_NAME.MESSAGES,
62 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
63 signupLimit: SIGNUP_LIMIT.MESSAGES,
64 adminEmail: ADMIN_EMAIL.MESSAGES,
65 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
68 private oldCustomJavascript: string
69 private oldCustomCSS: string
72 private formBuilder: FormBuilder,
73 private router: Router,
74 private notificationsService: NotificationsService,
75 private configService: ConfigService,
76 private serverService: ServerService,
77 private confirmService: ConfirmService
82 getResolutionKey (resolution: string) {
83 return 'transcodingResolution' + resolution
87 const formGroupData = {
88 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
89 instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ],
90 instanceDescription: [ '' ],
91 instanceTerms: [ '' ],
92 instanceDefaultClientRoute: [ '' ],
93 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
95 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
96 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
97 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
98 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
99 transcodingEnabled: [ ],
100 customizationJavascript: [ '' ],
101 customizationCSS: [ '' ]
104 for (const resolution of this.resolutions) {
105 const key = this.getResolutionKey(resolution)
106 formGroupData[key] = [ false ]
109 this.form = this.formBuilder.group(formGroupData)
111 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
117 this.configService.getCustomConfig()
120 this.customConfig = res
122 this.oldCustomCSS = this.customConfig.instance.customizations.css
123 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
126 // Force form validation
130 err => this.notificationsService.error('Error', err.message)
134 isTranscodingEnabled () {
135 return this.form.value['transcodingEnabled'] === true
139 return this.form.value['signupEnabled'] === true
142 async formValidated () {
143 const newCustomizationJavascript = this.form.value['customizationJavascript']
144 const newCustomizationCSS = this.form.value['customizationCSS']
146 const customizations = []
147 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
148 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
150 if (customizations.length !== 0) {
151 const customizationsText = customizations.join('/')
153 const message = `You set custom ${customizationsText}. ` +
154 'This could lead to security issues or bugs if you do not understand it. ' +
155 'Are you sure you want to update the configuration?'
156 const label = `Please type "I understand the ${customizationsText} I set" to confirm.`
157 const expectedInputValue = `I understand the ${customizationsText} I set`
159 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
160 if (confirmRes === false) return
163 const data: CustomConfig = {
165 name: this.form.value['instanceName'],
166 shortDescription: this.form.value['instanceShortDescription'],
167 description: this.form.value['instanceDescription'],
168 terms: this.form.value['instanceTerms'],
169 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
171 javascript: this.form.value['customizationJavascript'],
172 css: this.form.value['customizationCSS']
177 size: this.form.value['cachePreviewsSize']
181 enabled: this.form.value['signupEnabled'],
182 limit: this.form.value['signupLimit']
185 email: this.form.value['adminEmail']
188 videoQuota: this.form.value['userVideoQuota']
191 enabled: this.form.value['transcodingEnabled'],
192 threads: this.form.value['transcodingThreads'],
194 '240p': this.form.value[this.getResolutionKey('240p')],
195 '360p': this.form.value[this.getResolutionKey('360p')],
196 '480p': this.form.value[this.getResolutionKey('480p')],
197 '720p': this.form.value[this.getResolutionKey('720p')],
198 '1080p': this.form.value[this.getResolutionKey('1080p')]
203 this.configService.updateCustomConfig(data)
206 this.customConfig = res
208 // Reload general configuration
209 this.serverService.loadConfig()
213 this.notificationsService.success('Success', 'Configuration updated.')
216 err => this.notificationsService.error('Error', err.message)
220 private updateForm () {
222 instanceName: this.customConfig.instance.name,
223 instanceShortDescription: this.customConfig.instance.shortDescription,
224 instanceDescription: this.customConfig.instance.description,
225 instanceTerms: this.customConfig.instance.terms,
226 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
227 cachePreviewsSize: this.customConfig.cache.previews.size,
228 signupEnabled: this.customConfig.signup.enabled,
229 signupLimit: this.customConfig.signup.limit,
230 adminEmail: this.customConfig.admin.email,
231 userVideoQuota: this.customConfig.user.videoQuota,
232 transcodingThreads: this.customConfig.transcoding.threads,
233 transcodingEnabled: this.customConfig.transcoding.enabled,
234 customizationJavascript: this.customConfig.instance.customizations.javascript,
235 customizationCSS: this.customConfig.instance.customizations.css
238 for (const resolution of this.resolutions) {
239 const key = this.getResolutionKey(resolution)
240 data[key] = this.customConfig.transcoding.resolutions[resolution]
243 this.form.patchValue(data)