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'
11 INSTANCE_NAME, INSTANCE_SHORT_DESCRIPTION, SERVICES_TWITTER_USERNAME,
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'
19 selector: 'my-edit-custom-config',
20 templateUrl: './edit-custom-config.component.html',
21 styleUrls: [ './edit-custom-config.component.scss' ]
23 export class EditCustomConfigComponent extends FormReactive implements OnInit {
24 customConfig: CustomConfig
25 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
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' }
37 transcodingThreadOptions = [
38 { value: 1, label: '1' },
39 { value: 2, label: '2' },
40 { value: 4, label: '4' },
41 { value: 8, label: '8' }
47 instanceShortDescription: '',
48 instanceDescription: '',
50 instanceDefaultClientRoute: '',
51 instanceDefaultNSFWPolicy: '',
52 servicesTwitterUsername: '',
53 cachePreviewsSize: '',
57 transcodingThreads: '',
58 customizationJavascript: '',
61 validationMessages = {
62 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES,
63 instanceName: INSTANCE_NAME.MESSAGES,
64 servicesTwitterUsername: SERVICES_TWITTER_USERNAME,
65 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
66 signupLimit: SIGNUP_LIMIT.MESSAGES,
67 adminEmail: ADMIN_EMAIL.MESSAGES,
68 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
71 private oldCustomJavascript: string
72 private oldCustomCSS: string
75 private formBuilder: FormBuilder,
76 private router: Router,
77 private notificationsService: NotificationsService,
78 private configService: ConfigService,
79 private serverService: ServerService,
80 private confirmService: ConfirmService
85 getResolutionKey (resolution: string) {
86 return 'transcodingResolution' + resolution
90 const formGroupData = {
91 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
92 instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ],
93 instanceDescription: [ '' ],
94 instanceTerms: [ '' ],
95 instanceDefaultClientRoute: [ '' ],
96 instanceDefaultNSFWPolicy: [ '' ],
97 servicesTwitterUsername: [ '', SERVICES_TWITTER_USERNAME.VALIDATORS ],
98 servicesTwitterWhitelisted: [ ],
99 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
101 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
102 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
103 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
104 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
105 transcodingEnabled: [ ],
106 customizationJavascript: [ '' ],
107 customizationCSS: [ '' ]
110 for (const resolution of this.resolutions) {
111 const key = this.getResolutionKey(resolution)
112 formGroupData[key] = [ false ]
115 this.form = this.formBuilder.group(formGroupData)
117 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
123 this.configService.getCustomConfig()
126 this.customConfig = res
128 this.oldCustomCSS = this.customConfig.instance.customizations.css
129 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
132 // Force form validation
136 err => this.notificationsService.error('Error', err.message)
140 isTranscodingEnabled () {
141 return this.form.value['transcodingEnabled'] === true
145 return this.form.value['signupEnabled'] === true
148 async formValidated () {
149 const newCustomizationJavascript = this.form.value['customizationJavascript']
150 const newCustomizationCSS = this.form.value['customizationCSS']
152 const customizations = []
153 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
154 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
156 if (customizations.length !== 0) {
157 const customizationsText = customizations.join('/')
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`
165 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
166 if (confirmRes === false) return
169 const data: CustomConfig = {
171 name: this.form.value['instanceName'],
172 shortDescription: this.form.value['instanceShortDescription'],
173 description: this.form.value['instanceDescription'],
174 terms: this.form.value['instanceTerms'],
175 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
176 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
178 javascript: this.form.value['customizationJavascript'],
179 css: this.form.value['customizationCSS']
184 username: this.form.value['servicesTwitterUsername'],
185 whitelisted: this.form.value['servicesTwitterWhitelisted']
190 size: this.form.value['cachePreviewsSize']
194 enabled: this.form.value['signupEnabled'],
195 limit: this.form.value['signupLimit']
198 email: this.form.value['adminEmail']
201 videoQuota: this.form.value['userVideoQuota']
204 enabled: this.form.value['transcodingEnabled'],
205 threads: this.form.value['transcodingThreads'],
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')]
216 this.configService.updateCustomConfig(data)
219 this.customConfig = res
221 // Reload general configuration
222 this.serverService.loadConfig()
226 this.notificationsService.success('Success', 'Configuration updated.')
229 err => this.notificationsService.error('Error', err.message)
233 private updateForm () {
235 instanceName: this.customConfig.instance.name,
236 instanceShortDescription: this.customConfig.instance.shortDescription,
237 instanceDescription: this.customConfig.instance.description,
238 instanceTerms: this.customConfig.instance.terms,
239 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
240 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
241 servicesTwitterUsername: this.customConfig.services.twitter.username,
242 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
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,
249 transcodingEnabled: this.customConfig.transcoding.enabled,
250 customizationJavascript: this.customConfig.instance.customizations.javascript,
251 customizationCSS: this.customConfig.instance.customizations.css
254 for (const resolution of this.resolutions) {
255 const key = this.getResolutionKey(resolution)
256 data[key] = this.customConfig.transcoding.resolutions[resolution]
259 this.form.patchValue(data)