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,
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 cachePreviewsSize: '',
56 transcodingThreads: '',
57 customizationJavascript: '',
60 validationMessages = {
61 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES,
62 instanceName: INSTANCE_NAME.MESSAGES,
63 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
64 signupLimit: SIGNUP_LIMIT.MESSAGES,
65 adminEmail: ADMIN_EMAIL.MESSAGES,
66 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
69 private oldCustomJavascript: string
70 private oldCustomCSS: string
73 private formBuilder: FormBuilder,
74 private router: Router,
75 private notificationsService: NotificationsService,
76 private configService: ConfigService,
77 private serverService: ServerService,
78 private confirmService: ConfirmService
83 getResolutionKey (resolution: string) {
84 return 'transcodingResolution' + resolution
88 const formGroupData = {
89 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
90 instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ],
91 instanceDescription: [ '' ],
92 instanceTerms: [ '' ],
93 instanceDefaultClientRoute: [ '' ],
94 instanceDefaultNSFWPolicy: [ '' ],
95 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
97 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
98 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
99 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
100 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
101 transcodingEnabled: [ ],
102 customizationJavascript: [ '' ],
103 customizationCSS: [ '' ]
106 for (const resolution of this.resolutions) {
107 const key = this.getResolutionKey(resolution)
108 formGroupData[key] = [ false ]
111 this.form = this.formBuilder.group(formGroupData)
113 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
119 this.configService.getCustomConfig()
122 this.customConfig = res
124 this.oldCustomCSS = this.customConfig.instance.customizations.css
125 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
128 // Force form validation
132 err => this.notificationsService.error('Error', err.message)
136 isTranscodingEnabled () {
137 return this.form.value['transcodingEnabled'] === true
141 return this.form.value['signupEnabled'] === true
144 async formValidated () {
145 const newCustomizationJavascript = this.form.value['customizationJavascript']
146 const newCustomizationCSS = this.form.value['customizationCSS']
148 const customizations = []
149 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
150 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
152 if (customizations.length !== 0) {
153 const customizationsText = customizations.join('/')
155 const message = `You set custom ${customizationsText}. ` +
156 'This could lead to security issues or bugs if you do not understand it. ' +
157 'Are you sure you want to update the configuration?'
158 const label = `Please type "I understand the ${customizationsText} I set" to confirm.`
159 const expectedInputValue = `I understand the ${customizationsText} I set`
161 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
162 if (confirmRes === false) return
165 const data: CustomConfig = {
167 name: this.form.value['instanceName'],
168 shortDescription: this.form.value['instanceShortDescription'],
169 description: this.form.value['instanceDescription'],
170 terms: this.form.value['instanceTerms'],
171 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
172 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
174 javascript: this.form.value['customizationJavascript'],
175 css: this.form.value['customizationCSS']
180 size: this.form.value['cachePreviewsSize']
184 enabled: this.form.value['signupEnabled'],
185 limit: this.form.value['signupLimit']
188 email: this.form.value['adminEmail']
191 videoQuota: this.form.value['userVideoQuota']
194 enabled: this.form.value['transcodingEnabled'],
195 threads: this.form.value['transcodingThreads'],
197 '240p': this.form.value[this.getResolutionKey('240p')],
198 '360p': this.form.value[this.getResolutionKey('360p')],
199 '480p': this.form.value[this.getResolutionKey('480p')],
200 '720p': this.form.value[this.getResolutionKey('720p')],
201 '1080p': this.form.value[this.getResolutionKey('1080p')]
206 this.configService.updateCustomConfig(data)
209 this.customConfig = res
211 // Reload general configuration
212 this.serverService.loadConfig()
216 this.notificationsService.success('Success', 'Configuration updated.')
219 err => this.notificationsService.error('Error', err.message)
223 private updateForm () {
225 instanceName: this.customConfig.instance.name,
226 instanceShortDescription: this.customConfig.instance.shortDescription,
227 instanceDescription: this.customConfig.instance.description,
228 instanceTerms: this.customConfig.instance.terms,
229 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
230 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
231 cachePreviewsSize: this.customConfig.cache.previews.size,
232 signupEnabled: this.customConfig.signup.enabled,
233 signupLimit: this.customConfig.signup.limit,
234 adminEmail: this.customConfig.admin.email,
235 userVideoQuota: this.customConfig.user.videoQuota,
236 transcodingThreads: this.customConfig.transcoding.threads,
237 transcodingEnabled: this.customConfig.transcoding.enabled,
238 customizationJavascript: this.customConfig.instance.customizations.javascript,
239 customizationCSS: this.customConfig.instance.customizations.css
242 for (const resolution of this.resolutions) {
243 const key = this.getResolutionKey(resolution)
244 data[key] = this.customConfig.transcoding.resolutions[resolution]
247 this.form.patchValue(data)