]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
d73ee71e4513c4bcba7629d175da1549030b8138
[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 // Force form validation
127 this.forceCheck()
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
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
163 const data: CustomConfig = {
164 instance: {
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'],
170 customizations: {
171 javascript: this.form.value['customizationJavascript'],
172 css: this.form.value['customizationCSS']
173 }
174 },
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()
212
213 this.notificationsService.success('Success', 'Configuration updated.')
214 },
215
216 err => this.notificationsService.error('Error', err.message)
217 )
218 }
219
220 private updateForm () {
221 const data = {
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
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 }