]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Add stats route
[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,
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 instanceDescription: '',
48 instanceTerms: '',
49 cachePreviewsSize: '',
50 signupLimit: '',
51 adminEmail: '',
52 userVideoQuota: '',
53 transcodingThreads: '',
54 customizationJavascript: '',
55 customizationCSS: ''
56 }
57 validationMessages = {
58 instanceName: INSTANCE_NAME.MESSAGES,
59 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
60 signupLimit: SIGNUP_LIMIT.MESSAGES,
61 adminEmail: ADMIN_EMAIL.MESSAGES,
62 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
63 }
64
65 private oldCustomJavascript: string
66 private oldCustomCSS: string
67
68 constructor (
69 private formBuilder: FormBuilder,
70 private router: Router,
71 private notificationsService: NotificationsService,
72 private configService: ConfigService,
73 private serverService: ServerService,
74 private confirmService: ConfirmService
75 ) {
76 super()
77 }
78
79 getResolutionKey (resolution: string) {
80 return 'transcodingResolution' + resolution
81 }
82
83 buildForm () {
84 const formGroupData = {
85 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
86 instanceDescription: [ '' ],
87 instanceTerms: [ '' ],
88 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
89 signupEnabled: [ ],
90 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
91 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
92 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
93 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
94 transcodingEnabled: [ ],
95 customizationJavascript: [ '' ],
96 customizationCSS: [ '' ]
97 }
98
99 for (const resolution of this.resolutions) {
100 const key = this.getResolutionKey(resolution)
101 formGroupData[key] = [ false ]
102 }
103
104 this.form = this.formBuilder.group(formGroupData)
105
106 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
107 }
108
109 ngOnInit () {
110 this.buildForm()
111
112 this.configService.getCustomConfig()
113 .subscribe(
114 res => {
115 this.customConfig = res
116
117 this.oldCustomCSS = this.customConfig.instance.customizations.css
118 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
119
120 this.updateForm()
121 },
122
123 err => this.notificationsService.error('Error', err.message)
124 )
125 }
126
127 isTranscodingEnabled () {
128 return this.form.value['transcodingEnabled'] === true
129 }
130
131 isSignupEnabled () {
132 return this.form.value['signupEnabled'] === true
133 }
134
135 async formValidated () {
136 const newCustomizationJavascript = this.form.value['customizationJavascript']
137 const newCustomizationCSS = this.form.value['customizationCSS']
138
139 const customizations = []
140 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
141 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
142
143 if (customizations.length !== 0) {
144 const customizationsText = customizations.join('/')
145
146 const message = `You set custom ${customizationsText}. ` +
147 'This could lead to security issues or bugs if you do not understand it. ' +
148 'Are you sure you want to update the configuration?'
149 const label = `Please type "I understand the ${customizationsText} I set" to confirm.`
150 const expectedInputValue = `I understand the ${customizationsText} I set`
151
152 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
153 if (confirmRes === false) return
154 }
155
156 const data = {
157 instance: {
158 name: this.form.value['instanceName'],
159 description: this.form.value['instanceDescription'],
160 terms: this.form.value['instanceTerms'],
161 customizations: {
162 javascript: this.form.value['customizationJavascript'],
163 css: this.form.value['customizationCSS']
164 }
165 },
166 cache: {
167 previews: {
168 size: this.form.value['cachePreviewsSize']
169 }
170 },
171 signup: {
172 enabled: this.form.value['signupEnabled'],
173 limit: this.form.value['signupLimit']
174 },
175 admin: {
176 email: this.form.value['adminEmail']
177 },
178 user: {
179 videoQuota: this.form.value['userVideoQuota']
180 },
181 transcoding: {
182 enabled: this.form.value['transcodingEnabled'],
183 threads: this.form.value['transcodingThreads'],
184 resolutions: {
185 '240p': this.form.value[this.getResolutionKey('240p')],
186 '360p': this.form.value[this.getResolutionKey('360p')],
187 '480p': this.form.value[this.getResolutionKey('480p')],
188 '720p': this.form.value[this.getResolutionKey('720p')],
189 '1080p': this.form.value[this.getResolutionKey('1080p')]
190 }
191 }
192 }
193
194 this.configService.updateCustomConfig(data)
195 .subscribe(
196 res => {
197 this.customConfig = res
198
199 // Reload general configuration
200 this.serverService.loadConfig()
201
202 this.updateForm()
203
204 this.notificationsService.success('Success', 'Configuration updated.')
205 },
206
207 err => this.notificationsService.error('Error', err.message)
208 )
209 }
210
211 private updateForm () {
212 const data = {
213 instanceName: this.customConfig.instance.name,
214 instanceDescription: this.customConfig.instance.description,
215 instanceTerms: this.customConfig.instance.terms,
216 cachePreviewsSize: this.customConfig.cache.previews.size,
217 signupEnabled: this.customConfig.signup.enabled,
218 signupLimit: this.customConfig.signup.limit,
219 adminEmail: this.customConfig.admin.email,
220 userVideoQuota: this.customConfig.user.videoQuota,
221 transcodingThreads: this.customConfig.transcoding.threads,
222 transcodingEnabled: this.customConfig.transcoding.enabled,
223 customizationJavascript: this.customConfig.instance.customizations.javascript,
224 customizationCSS: this.customConfig.instance.customizations.css
225 }
226
227 for (const resolution of this.resolutions) {
228 const key = this.getResolutionKey(resolution)
229 data[key] = this.customConfig.transcoding.resolutions[resolution]
230 }
231
232 this.form.patchValue(data)
233 }
234
235 }