]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Fix error display on edit config in admin
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
CommitLineData
fd206f0b
C
1import { Component, OnInit } from '@angular/core'
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { Router } from '@angular/router'
4import { ConfigService } from '@app/+admin/config/shared/config.service'
1f30a185 5import { ConfirmService } from '@app/core'
fd206f0b
C
6import { ServerService } from '@app/core/server/server.service'
7import { FormReactive, USER_VIDEO_QUOTA } from '@app/shared'
66b16caf
C
8import {
9 ADMIN_EMAIL,
10 CACHE_PREVIEWS_SIZE,
2e3a0215 11 INSTANCE_NAME, INSTANCE_SHORT_DESCRIPTION,
66b16caf
C
12 SIGNUP_LIMIT,
13 TRANSCODING_THREADS
14} from '@app/shared/forms/form-validators/custom-config'
fd206f0b 15import { NotificationsService } from 'angular2-notifications'
09cababd 16import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
fd206f0b
C
17
18@Component({
19 selector: 'my-edit-custom-config',
20 templateUrl: './edit-custom-config.component.html',
21 styleUrls: [ './edit-custom-config.component.scss' ]
22})
23export class EditCustomConfigComponent extends FormReactive implements OnInit {
24 customConfig: CustomConfig
25 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
26
27 videoQuotaOptions = [
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' }
36 ]
37 transcodingThreadOptions = [
38 { value: 1, label: '1' },
39 { value: 2, label: '2' },
40 { value: 4, label: '4' },
41 { value: 8, label: '8' }
42 ]
43
44 form: FormGroup
45 formErrors = {
66b16caf 46 instanceName: '',
2e3a0215 47 instanceShortDescription: '',
66b16caf
C
48 instanceDescription: '',
49 instanceTerms: '',
901637bb 50 instanceDefaultClientRoute: '',
fd206f0b
C
51 cachePreviewsSize: '',
52 signupLimit: '',
53 adminEmail: '',
54 userVideoQuota: '',
00b5556c
C
55 transcodingThreads: '',
56 customizationJavascript: '',
57 customizationCSS: ''
fd206f0b
C
58 }
59 validationMessages = {
2e3a0215 60 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES,
66b16caf 61 instanceName: INSTANCE_NAME.MESSAGES,
fd206f0b
C
62 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
63 signupLimit: SIGNUP_LIMIT.MESSAGES,
64 adminEmail: ADMIN_EMAIL.MESSAGES,
65 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
66 }
67
1f30a185
C
68 private oldCustomJavascript: string
69 private oldCustomCSS: string
70
fd206f0b
C
71 constructor (
72 private formBuilder: FormBuilder,
73 private router: Router,
74 private notificationsService: NotificationsService,
75 private configService: ConfigService,
1f30a185
C
76 private serverService: ServerService,
77 private confirmService: ConfirmService
fd206f0b
C
78 ) {
79 super()
80 }
81
82 getResolutionKey (resolution: string) {
83 return 'transcodingResolution' + resolution
84 }
85
86 buildForm () {
87 const formGroupData = {
66b16caf 88 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
2e3a0215 89 instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ],
66b16caf
C
90 instanceDescription: [ '' ],
91 instanceTerms: [ '' ],
901637bb 92 instanceDefaultClientRoute: [ '' ],
fd206f0b
C
93 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
94 signupEnabled: [ ],
95 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
96 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
97 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
98 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
00b5556c
C
99 transcodingEnabled: [ ],
100 customizationJavascript: [ '' ],
101 customizationCSS: [ '' ]
fd206f0b
C
102 }
103
104 for (const resolution of this.resolutions) {
105 const key = this.getResolutionKey(resolution)
106 formGroupData[key] = [ false ]
107 }
108
109 this.form = this.formBuilder.group(formGroupData)
110
111 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
112 }
113
114 ngOnInit () {
115 this.buildForm()
116
117 this.configService.getCustomConfig()
118 .subscribe(
119 res => {
120 this.customConfig = res
121
1f30a185
C
122 this.oldCustomCSS = this.customConfig.instance.customizations.css
123 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
124
fd206f0b 125 this.updateForm()
d63fd4f7
C
126 // Force form validation
127 this.forceCheck()
fd206f0b
C
128 },
129
130 err => this.notificationsService.error('Error', err.message)
131 )
132 }
133
134 isTranscodingEnabled () {
135 return this.form.value['transcodingEnabled'] === true
136 }
137
138 isSignupEnabled () {
139 return this.form.value['signupEnabled'] === true
140 }
141
1f30a185
C
142 async formValidated () {
143 const newCustomizationJavascript = this.form.value['customizationJavascript']
144 const newCustomizationCSS = this.form.value['customizationCSS']
145
146 const customizations = []
147 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
148 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
149
150 if (customizations.length !== 0) {
151 const customizationsText = customizations.join('/')
152
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`
158
159 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
160 if (confirmRes === false) return
161 }
162
901637bb 163 const data: CustomConfig = {
66b16caf
C
164 instance: {
165 name: this.form.value['instanceName'],
2e3a0215 166 shortDescription: this.form.value['instanceShortDescription'],
66b16caf 167 description: this.form.value['instanceDescription'],
00b5556c 168 terms: this.form.value['instanceTerms'],
901637bb 169 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
00b5556c
C
170 customizations: {
171 javascript: this.form.value['customizationJavascript'],
172 css: this.form.value['customizationCSS']
173 }
66b16caf 174 },
fd206f0b
C
175 cache: {
176 previews: {
177 size: this.form.value['cachePreviewsSize']
178 }
179 },
180 signup: {
181 enabled: this.form.value['signupEnabled'],
182 limit: this.form.value['signupLimit']
183 },
184 admin: {
185 email: this.form.value['adminEmail']
186 },
187 user: {
188 videoQuota: this.form.value['userVideoQuota']
189 },
190 transcoding: {
191 enabled: this.form.value['transcodingEnabled'],
192 threads: this.form.value['transcodingThreads'],
193 resolutions: {
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')]
199 }
200 }
201 }
202
203 this.configService.updateCustomConfig(data)
204 .subscribe(
205 res => {
206 this.customConfig = res
207
208 // Reload general configuration
209 this.serverService.loadConfig()
210
211 this.updateForm()
66b16caf
C
212
213 this.notificationsService.success('Success', 'Configuration updated.')
fd206f0b
C
214 },
215
216 err => this.notificationsService.error('Error', err.message)
217 )
218 }
219
220 private updateForm () {
221 const data = {
66b16caf 222 instanceName: this.customConfig.instance.name,
2e3a0215 223 instanceShortDescription: this.customConfig.instance.shortDescription,
66b16caf
C
224 instanceDescription: this.customConfig.instance.description,
225 instanceTerms: this.customConfig.instance.terms,
901637bb 226 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
fd206f0b
C
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,
00b5556c
C
233 transcodingEnabled: this.customConfig.transcoding.enabled,
234 customizationJavascript: this.customConfig.instance.customizations.javascript,
235 customizationCSS: this.customConfig.instance.customizations.css
fd206f0b
C
236 }
237
238 for (const resolution of this.resolutions) {
239 const key = this.getResolutionKey(resolution)
240 data[key] = this.customConfig.transcoding.resolutions[resolution]
241 }
242
243 this.form.patchValue(data)
244 }
245
246}