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'
12 INSTANCE_SHORT_DESCRIPTION,
13 SERVICES_TWITTER_USERNAME,
16 } from '@app/shared/forms/form-validators/custom-config'
17 import { NotificationsService } from 'angular2-notifications'
18 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
19 import { I18n } from '@ngx-translate/i18n-polyfill'
20 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
23 selector: 'my-edit-custom-config',
24 templateUrl: './edit-custom-config.component.html',
25 styleUrls: [ './edit-custom-config.component.scss' ]
27 export class EditCustomConfigComponent extends FormReactive implements OnInit {
28 customConfig: CustomConfig
29 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
32 { value: -1, label: 'Unlimited' },
33 { value: 0, label: '0' },
34 { value: 100 * 1024 * 1024, label: '100MB' },
35 { value: 500 * 1024 * 1024, label: '500MB' },
36 { value: 1024 * 1024 * 1024, label: '1GB' },
37 { value: 5 * 1024 * 1024 * 1024, label: '5GB' },
38 { value: 20 * 1024 * 1024 * 1024, label: '20GB' },
39 { value: 50 * 1024 * 1024 * 1024, label: '50GB' }
41 transcodingThreadOptions = [
42 { value: 1, label: '1' },
43 { value: 2, label: '2' },
44 { value: 4, label: '4' },
45 { value: 8, label: '8' }
48 private oldCustomJavascript: string
49 private oldCustomCSS: string
52 protected formValidatorService: FormValidatorService,
53 private router: Router,
54 private notificationsService: NotificationsService,
55 private configService: ConfigService,
56 private serverService: ServerService,
57 private confirmService: ConfirmService,
63 getResolutionKey (resolution: string) {
64 return 'transcodingResolution' + resolution
68 const formGroupData = {
69 instanceName: INSTANCE_NAME,
70 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION,
71 instanceDescription: null,
73 instanceDefaultClientRoute: null,
74 instanceDefaultNSFWPolicy: null,
75 servicesTwitterUsername: SERVICES_TWITTER_USERNAME,
76 servicesTwitterWhitelisted: null,
77 cachePreviewsSize: CACHE_PREVIEWS_SIZE,
79 signupLimit: SIGNUP_LIMIT,
80 adminEmail: ADMIN_EMAIL,
81 userVideoQuota: USER_VIDEO_QUOTA,
82 transcodingThreads: TRANSCODING_THREADS,
83 transcodingEnabled: null,
84 customizationJavascript: null,
85 customizationCSS: null
88 const defaultValues: BuildFormDefaultValues = {}
89 for (const resolution of this.resolutions) {
90 const key = this.getResolutionKey(resolution)
91 defaultValues[key] = 'false'
92 formGroupData[key] = null
95 this.buildForm(formGroupData)
97 this.configService.getCustomConfig()
100 this.customConfig = res
102 this.oldCustomCSS = this.customConfig.instance.customizations.css
103 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
106 // Force form validation
110 err => this.notificationsService.error(this.i18n('Error'), err.message)
114 isTranscodingEnabled () {
115 return this.form.value['transcodingEnabled'] === true
119 return this.form.value['signupEnabled'] === true
122 async formValidated () {
123 const newCustomizationJavascript = this.form.value['customizationJavascript']
124 const newCustomizationCSS = this.form.value['customizationCSS']
126 const customizations = []
127 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
128 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
130 if (customizations.length !== 0) {
131 const customizationsText = customizations.join('/')
133 // FIXME: i18n service does not support string concatenation
134 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
135 this.i18n('This could lead to security issues or bugs if you do not understand it. ') +
136 this.i18n('Are you sure you want to update the configuration?')
137 const label = this.i18n(
138 'Please type "I understand the {{customizationsText}} I set" to confirm.',
139 { customizationsText }
141 const expectedInputValue = this.i18n('I understand the {{customizationsText}} I set', { customizationsText })
143 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
144 if (confirmRes === false) return
147 const data: CustomConfig = {
149 name: this.form.value['instanceName'],
150 shortDescription: this.form.value['instanceShortDescription'],
151 description: this.form.value['instanceDescription'],
152 terms: this.form.value['instanceTerms'],
153 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
154 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
156 javascript: this.form.value['customizationJavascript'],
157 css: this.form.value['customizationCSS']
162 username: this.form.value['servicesTwitterUsername'],
163 whitelisted: this.form.value['servicesTwitterWhitelisted']
168 size: this.form.value['cachePreviewsSize']
172 enabled: this.form.value['signupEnabled'],
173 limit: this.form.value['signupLimit']
176 email: this.form.value['adminEmail']
179 videoQuota: this.form.value['userVideoQuota']
182 enabled: this.form.value['transcodingEnabled'],
183 threads: this.form.value['transcodingThreads'],
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')]
194 this.configService.updateCustomConfig(data)
197 this.customConfig = res
199 // Reload general configuration
200 this.serverService.loadConfig()
204 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
207 err => this.notificationsService.error(this.i18n('Error'), err.message)
211 private updateForm () {
213 instanceName: this.customConfig.instance.name,
214 instanceShortDescription: this.customConfig.instance.shortDescription,
215 instanceDescription: this.customConfig.instance.description,
216 instanceTerms: this.customConfig.instance.terms,
217 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
218 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
219 servicesTwitterUsername: this.customConfig.services.twitter.username,
220 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
221 cachePreviewsSize: this.customConfig.cache.previews.size,
222 signupEnabled: this.customConfig.signup.enabled,
223 signupLimit: this.customConfig.signup.limit,
224 adminEmail: this.customConfig.admin.email,
225 userVideoQuota: this.customConfig.user.videoQuota,
226 transcodingThreads: this.customConfig.transcoding.threads,
227 transcodingEnabled: this.customConfig.transcoding.enabled,
228 customizationJavascript: this.customConfig.instance.customizations.javascript,
229 customizationCSS: this.customConfig.instance.customizations.css
232 for (const resolution of this.resolutions) {
233 const key = this.getResolutionKey(resolution)
234 data[key] = this.customConfig.transcoding.resolutions[resolution]
237 this.form.patchValue(data)