]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Add short description in config
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
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'
8 import {
9 ADMIN_EMAIL,
10 CACHE_PREVIEWS_SIZE,
11 INSTANCE_NAME, INSTANCE_SHORT_DESCRIPTION,
12 SIGNUP_LIMIT,
13 TRANSCODING_THREADS
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'
17
18 @Component({
19 selector: 'my-edit-custom-config',
20 templateUrl: './edit-custom-config.component.html',
21 styleUrls: [ './edit-custom-config.component.scss' ]
22 })
23 export 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 = {
46 instanceName: '',
47 instanceShortDescription: '',
48 instanceDescription: '',
49 instanceTerms: '',
50 instanceDefaultClientRoute: '',
51 cachePreviewsSize: '',
52 signupLimit: '',
53 adminEmail: '',
54 userVideoQuota: '',
55 transcodingThreads: '',
56 customizationJavascript: '',
57 customizationCSS: ''
58 }
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
66 }
67
68 private oldCustomJavascript: string
69 private oldCustomCSS: string
70
71 constructor (
72 private formBuilder: FormBuilder,
73 private router: Router,
74 private notificationsService: NotificationsService,
75 private configService: ConfigService,
76 private serverService: ServerService,
77 private confirmService: ConfirmService
78 ) {
79 super()
80 }
81
82 getResolutionKey (resolution: string) {
83 return 'transcodingResolution' + resolution
84 }
85
86 buildForm () {
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 ],
94 signupEnabled: [ ],
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: [ '' ]
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
122 this.oldCustomCSS = this.customConfig.instance.customizations.css
123 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
124
125 this.updateForm()
126 },
127
128 err => this.notificationsService.error('Error', err.message)
129 )
130 }
131
132 isTranscodingEnabled () {
133 return this.form.value['transcodingEnabled'] === true
134 }
135
136 isSignupEnabled () {
137 return this.form.value['signupEnabled'] === true
138 }
139
140 async formValidated () {
141 const newCustomizationJavascript = this.form.value['customizationJavascript']
142 const newCustomizationCSS = this.form.value['customizationCSS']
143
144 const customizations = []
145 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
146 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
147
148 if (customizations.length !== 0) {
149 const customizationsText = customizations.join('/')
150
151 const message = `You set custom ${customizationsText}. ` +
152 'This could lead to security issues or bugs if you do not understand it. ' +
153 'Are you sure you want to update the configuration?'
154 const label = `Please type "I understand the ${customizationsText} I set" to confirm.`
155 const expectedInputValue = `I understand the ${customizationsText} I set`
156
157 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
158 if (confirmRes === false) return
159 }
160
161 const data: CustomConfig = {
162 instance: {
163 name: this.form.value['instanceName'],
164 shortDescription: this.form.value['instanceShortDescription'],
165 description: this.form.value['instanceDescription'],
166 terms: this.form.value['instanceTerms'],
167 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
168 customizations: {
169 javascript: this.form.value['customizationJavascript'],
170 css: this.form.value['customizationCSS']
171 }
172 },
173 cache: {
174 previews: {
175 size: this.form.value['cachePreviewsSize']
176 }
177 },
178 signup: {
179 enabled: this.form.value['signupEnabled'],
180 limit: this.form.value['signupLimit']
181 },
182 admin: {
183 email: this.form.value['adminEmail']
184 },
185 user: {
186 videoQuota: this.form.value['userVideoQuota']
187 },
188 transcoding: {
189 enabled: this.form.value['transcodingEnabled'],
190 threads: this.form.value['transcodingThreads'],
191 resolutions: {
192 '240p': this.form.value[this.getResolutionKey('240p')],
193 '360p': this.form.value[this.getResolutionKey('360p')],
194 '480p': this.form.value[this.getResolutionKey('480p')],
195 '720p': this.form.value[this.getResolutionKey('720p')],
196 '1080p': this.form.value[this.getResolutionKey('1080p')]
197 }
198 }
199 }
200
201 this.configService.updateCustomConfig(data)
202 .subscribe(
203 res => {
204 this.customConfig = res
205
206 // Reload general configuration
207 this.serverService.loadConfig()
208
209 this.updateForm()
210
211 this.notificationsService.success('Success', 'Configuration updated.')
212 },
213
214 err => this.notificationsService.error('Error', err.message)
215 )
216 }
217
218 private updateForm () {
219 const data = {
220 instanceName: this.customConfig.instance.name,
221 instanceShortDescription: this.customConfig.instance.shortDescription,
222 instanceDescription: this.customConfig.instance.description,
223 instanceTerms: this.customConfig.instance.terms,
224 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
225 cachePreviewsSize: this.customConfig.cache.previews.size,
226 signupEnabled: this.customConfig.signup.enabled,
227 signupLimit: this.customConfig.signup.limit,
228 adminEmail: this.customConfig.admin.email,
229 userVideoQuota: this.customConfig.user.videoQuota,
230 transcodingThreads: this.customConfig.transcoding.threads,
231 transcodingEnabled: this.customConfig.transcoding.enabled,
232 customizationJavascript: this.customConfig.instance.customizations.javascript,
233 customizationCSS: this.customConfig.instance.customizations.css
234 }
235
236 for (const resolution of this.resolutions) {
237 const key = this.getResolutionKey(resolution)
238 data[key] = this.customConfig.transcoding.resolutions[resolution]
239 }
240
241 this.form.patchValue(data)
242 }
243
244 }