]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Merge branch 'develop' into 'develop'
[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
C
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
1f30a185
C
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
901637bb 161 const data: CustomConfig = {
66b16caf
C
162 instance: {
163 name: this.form.value['instanceName'],
2e3a0215 164 shortDescription: this.form.value['instanceShortDescription'],
66b16caf 165 description: this.form.value['instanceDescription'],
00b5556c 166 terms: this.form.value['instanceTerms'],
901637bb 167 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
00b5556c
C
168 customizations: {
169 javascript: this.form.value['customizationJavascript'],
170 css: this.form.value['customizationCSS']
171 }
66b16caf 172 },
fd206f0b
C
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()
66b16caf
C
210
211 this.notificationsService.success('Success', 'Configuration updated.')
fd206f0b
C
212 },
213
214 err => this.notificationsService.error('Error', err.message)
215 )
216 }
217
218 private updateForm () {
219 const data = {
66b16caf 220 instanceName: this.customConfig.instance.name,
2e3a0215 221 instanceShortDescription: this.customConfig.instance.shortDescription,
66b16caf
C
222 instanceDescription: this.customConfig.instance.description,
223 instanceTerms: this.customConfig.instance.terms,
901637bb 224 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
fd206f0b
C
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,
00b5556c
C
231 transcodingEnabled: this.customConfig.transcoding.enabled,
232 customizationJavascript: this.customConfig.instance.customizations.javascript,
233 customizationCSS: this.customConfig.instance.customizations.css
fd206f0b
C
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}