1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ConfigService } from '@app/+admin/config/shared/config.service'
4 import { ConfirmService } from '@app/core'
5 import { ServerService } from '@app/core/server/server.service'
6 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
7 import { NotificationsService } from 'angular2-notifications'
8 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 selector: 'my-edit-custom-config',
14 templateUrl: './edit-custom-config.component.html',
15 styleUrls: [ './edit-custom-config.component.scss' ]
17 export class EditCustomConfigComponent extends FormReactive implements OnInit {
18 static videoQuotaOptions = [
19 { value: -1, label: 'Unlimited' },
20 { value: 0, label: '0' },
21 { value: 100 * 1024 * 1024, label: '100MB' },
22 { value: 500 * 1024 * 1024, label: '500MB' },
23 { value: 1024 * 1024 * 1024, label: '1GB' },
24 { value: 5 * 1024 * 1024 * 1024, label: '5GB' },
25 { value: 20 * 1024 * 1024 * 1024, label: '20GB' },
26 { value: 50 * 1024 * 1024 * 1024, label: '50GB' }
28 static videoQuotaDailyOptions = [
29 { value: -1, label: 'Unlimited' },
30 { value: 0, label: '0' },
31 { value: 10 * 1024 * 1024, label: '10MB' },
32 { value: 50 * 1024 * 1024, label: '50MB' },
33 { value: 100 * 1024 * 1024, label: '100MB' },
34 { value: 500 * 1024 * 1024, label: '500MB' },
35 { value: 2 * 1024 * 1024 * 1024, label: '2GB' },
36 { value: 5 * 1024 * 1024 * 1024, label: '5GB' }
39 customConfig: CustomConfig
40 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
42 transcodingThreadOptions = [
43 { value: 0, label: 'Auto (via ffmpeg)' },
44 { value: 1, label: '1' },
45 { value: 2, label: '2' },
46 { value: 4, label: '4' },
47 { value: 8, label: '8' }
50 private oldCustomJavascript: string
51 private oldCustomCSS: string
54 protected formValidatorService: FormValidatorService,
55 private customConfigValidatorsService: CustomConfigValidatorsService,
56 private userValidatorsService: UserValidatorsService,
57 private router: Router,
58 private notificationsService: NotificationsService,
59 private configService: ConfigService,
60 private serverService: ServerService,
61 private confirmService: ConfirmService,
67 get videoQuotaOptions () {
68 return EditCustomConfigComponent.videoQuotaOptions
71 get videoQuotaDailyOptions () {
72 return EditCustomConfigComponent.videoQuotaDailyOptions
75 getResolutionKey (resolution: string) {
76 return 'transcodingResolution' + resolution
80 const formGroupData = {
81 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
82 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
83 instanceDescription: null,
85 instanceDefaultClientRoute: null,
86 instanceDefaultNSFWPolicy: null,
87 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
88 servicesTwitterWhitelisted: null,
89 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
90 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
92 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
93 signupRequiresEmailVerification: null,
94 importVideosHttpEnabled: null,
95 importVideosTorrentEnabled: null,
96 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
97 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
98 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
99 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
100 transcodingEnabled: null,
101 customizationJavascript: null,
102 customizationCSS: null
105 const defaultValues: BuildFormDefaultValues = {}
106 for (const resolution of this.resolutions) {
107 const key = this.getResolutionKey(resolution)
108 defaultValues[key] = 'false'
109 formGroupData[key] = null
112 this.buildForm(formGroupData)
114 this.configService.getCustomConfig()
117 this.customConfig = res
119 this.oldCustomCSS = this.customConfig.instance.customizations.css
120 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
123 // Force form validation
127 err => this.notificationsService.error(this.i18n('Error'), err.message)
131 isTranscodingEnabled () {
132 return this.form.value['transcodingEnabled'] === true
136 return this.form.value['signupEnabled'] === true
139 async formValidated () {
140 const newCustomizationJavascript = this.form.value['customizationJavascript']
141 const newCustomizationCSS = this.form.value['customizationCSS']
143 const customizations = []
144 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
145 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
147 if (customizations.length !== 0) {
148 const customizationsText = customizations.join('/')
150 // FIXME: i18n service does not support string concatenation
151 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
152 this.i18n('This could lead to security issues or bugs if you do not understand it. ') +
153 this.i18n('Are you sure you want to update the configuration?')
155 const label = this.i18n('Please type') + ` "I understand the ${customizationsText} I set" ` + this.i18n('to confirm.')
156 const expectedInputValue = `I understand the ${customizationsText} I set`
158 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
159 if (confirmRes === false) return
162 const data: CustomConfig = {
164 name: this.form.value['instanceName'],
165 shortDescription: this.form.value['instanceShortDescription'],
166 description: this.form.value['instanceDescription'],
167 terms: this.form.value['instanceTerms'],
168 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
169 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
171 javascript: this.form.value['customizationJavascript'],
172 css: this.form.value['customizationCSS']
177 username: this.form.value['servicesTwitterUsername'],
178 whitelisted: this.form.value['servicesTwitterWhitelisted']
183 size: this.form.value['cachePreviewsSize']
186 size: this.form.value['cacheCaptionsSize']
190 enabled: this.form.value['signupEnabled'],
191 limit: this.form.value['signupLimit'],
192 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
195 email: this.form.value['adminEmail']
198 videoQuota: this.form.value['userVideoQuota'],
199 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
202 enabled: this.form.value['transcodingEnabled'],
203 threads: this.form.value['transcodingThreads'],
205 '240p': this.form.value[this.getResolutionKey('240p')],
206 '360p': this.form.value[this.getResolutionKey('360p')],
207 '480p': this.form.value[this.getResolutionKey('480p')],
208 '720p': this.form.value[this.getResolutionKey('720p')],
209 '1080p': this.form.value[this.getResolutionKey('1080p')]
215 enabled: this.form.value['importVideosHttpEnabled']
218 enabled: this.form.value['importVideosTorrentEnabled']
224 this.configService.updateCustomConfig(data)
227 this.customConfig = res
229 // Reload general configuration
230 this.serverService.loadConfig()
234 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
237 err => this.notificationsService.error(this.i18n('Error'), err.message)
241 private updateForm () {
243 instanceName: this.customConfig.instance.name,
244 instanceShortDescription: this.customConfig.instance.shortDescription,
245 instanceDescription: this.customConfig.instance.description,
246 instanceTerms: this.customConfig.instance.terms,
247 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
248 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
249 servicesTwitterUsername: this.customConfig.services.twitter.username,
250 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
251 cachePreviewsSize: this.customConfig.cache.previews.size,
252 cacheCaptionsSize: this.customConfig.cache.captions.size,
253 signupEnabled: this.customConfig.signup.enabled,
254 signupLimit: this.customConfig.signup.limit,
255 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
256 adminEmail: this.customConfig.admin.email,
257 userVideoQuota: this.customConfig.user.videoQuota,
258 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
259 transcodingThreads: this.customConfig.transcoding.threads,
260 transcodingEnabled: this.customConfig.transcoding.enabled,
261 customizationJavascript: this.customConfig.instance.customizations.javascript,
262 customizationCSS: this.customConfig.instance.customizations.css,
263 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
264 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
267 for (const resolution of this.resolutions) {
268 const key = this.getResolutionKey(resolution)
269 data[key] = this.customConfig.transcoding.resolutions[resolution]
272 this.form.patchValue(data)