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