1 import { Component, OnInit } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { ConfirmService } from '@app/core'
4 import { ServerService } from '@app/core/server/server.service'
5 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
6 import { NotificationsService } from 'angular2-notifications'
7 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 selector: 'my-edit-custom-config',
13 templateUrl: './edit-custom-config.component.html',
14 styleUrls: [ './edit-custom-config.component.scss' ]
16 export class EditCustomConfigComponent extends FormReactive implements OnInit {
17 static videoQuotaOptions = [
18 { value: -1, label: 'Unlimited' },
19 { value: 0, label: '0' },
20 { value: 100 * 1024 * 1024, label: '100MB' },
21 { value: 500 * 1024 * 1024, label: '500MB' },
22 { value: 1024 * 1024 * 1024, label: '1GB' },
23 { value: 5 * 1024 * 1024 * 1024, label: '5GB' },
24 { value: 20 * 1024 * 1024 * 1024, label: '20GB' },
25 { value: 50 * 1024 * 1024 * 1024, label: '50GB' }
27 static videoQuotaDailyOptions = [
28 { value: -1, label: 'Unlimited' },
29 { value: 0, label: '0' },
30 { value: 10 * 1024 * 1024, label: '10MB' },
31 { value: 50 * 1024 * 1024, label: '50MB' },
32 { value: 100 * 1024 * 1024, label: '100MB' },
33 { value: 500 * 1024 * 1024, label: '500MB' },
34 { value: 2 * 1024 * 1024 * 1024, label: '2GB' },
35 { value: 5 * 1024 * 1024 * 1024, label: '5GB' }
38 customConfig: CustomConfig
39 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
41 transcodingThreadOptions = [
42 { value: 0, label: 'Auto (via ffmpeg)' },
43 { value: 1, label: '1' },
44 { value: 2, label: '2' },
45 { value: 4, label: '4' },
46 { value: 8, label: '8' }
49 private oldCustomJavascript: string
50 private oldCustomCSS: string
53 protected formValidatorService: FormValidatorService,
54 private customConfigValidatorsService: CustomConfigValidatorsService,
55 private userValidatorsService: UserValidatorsService,
56 private notificationsService: NotificationsService,
57 private configService: ConfigService,
58 private serverService: ServerService,
59 private confirmService: ConfirmService,
65 get videoQuotaOptions () {
66 return EditCustomConfigComponent.videoQuotaOptions
69 get videoQuotaDailyOptions () {
70 return EditCustomConfigComponent.videoQuotaDailyOptions
73 getResolutionKey (resolution: string) {
74 return 'transcodingResolution' + resolution
78 const formGroupData = {
79 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
80 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
81 instanceDescription: null,
83 instanceDefaultClientRoute: null,
84 instanceDefaultNSFWPolicy: null,
85 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
86 servicesTwitterWhitelisted: null,
87 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
88 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
90 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
91 signupRequiresEmailVerification: null,
92 importVideosHttpEnabled: null,
93 importVideosTorrentEnabled: null,
94 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
95 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
96 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
97 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
98 transcodingEnabled: null,
99 customizationJavascript: null,
100 customizationCSS: null
103 const defaultValues: BuildFormDefaultValues = {}
104 for (const resolution of this.resolutions) {
105 const key = this.getResolutionKey(resolution)
106 defaultValues[key] = 'false'
107 formGroupData[key] = null
110 this.buildForm(formGroupData)
112 this.configService.getCustomConfig()
115 this.customConfig = res
117 this.oldCustomCSS = this.customConfig.instance.customizations.css
118 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
121 // Force form validation
125 err => this.notificationsService.error(this.i18n('Error'), err.message)
129 isTranscodingEnabled () {
130 return this.form.value['transcodingEnabled'] === true
134 return this.form.value['signupEnabled'] === true
137 async formValidated () {
138 const newCustomizationJavascript = this.form.value['customizationJavascript']
139 const newCustomizationCSS = this.form.value['customizationCSS']
141 const customizations = []
142 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
143 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
145 if (customizations.length !== 0) {
146 const customizationsText = customizations.join('/')
148 // FIXME: i18n service does not support string concatenation
149 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
150 this.i18n('This could lead to security issues or bugs if you do not understand it. ') +
151 this.i18n('Are you sure you want to update the configuration?')
153 const label = this.i18n('Please type') + ` "I understand the ${customizationsText} I set" ` + this.i18n('to confirm.')
154 const expectedInputValue = `I understand the ${customizationsText} I set`
156 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
157 if (confirmRes === false) return
160 const data: CustomConfig = {
162 name: this.form.value['instanceName'],
163 shortDescription: this.form.value['instanceShortDescription'],
164 description: this.form.value['instanceDescription'],
165 terms: this.form.value['instanceTerms'],
166 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
167 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
169 javascript: this.form.value['customizationJavascript'],
170 css: this.form.value['customizationCSS']
175 username: this.form.value['servicesTwitterUsername'],
176 whitelisted: this.form.value['servicesTwitterWhitelisted']
181 size: this.form.value['cachePreviewsSize']
184 size: this.form.value['cacheCaptionsSize']
188 enabled: this.form.value['signupEnabled'],
189 limit: this.form.value['signupLimit'],
190 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
193 email: this.form.value['adminEmail']
196 videoQuota: this.form.value['userVideoQuota'],
197 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
200 enabled: this.form.value['transcodingEnabled'],
201 threads: this.form.value['transcodingThreads'],
203 '240p': this.form.value[this.getResolutionKey('240p')],
204 '360p': this.form.value[this.getResolutionKey('360p')],
205 '480p': this.form.value[this.getResolutionKey('480p')],
206 '720p': this.form.value[this.getResolutionKey('720p')],
207 '1080p': this.form.value[this.getResolutionKey('1080p')]
213 enabled: this.form.value['importVideosHttpEnabled']
216 enabled: this.form.value['importVideosTorrentEnabled']
222 this.configService.updateCustomConfig(data)
225 this.customConfig = res
227 // Reload general configuration
228 this.serverService.loadConfig()
232 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
235 err => this.notificationsService.error(this.i18n('Error'), err.message)
239 private updateForm () {
241 instanceName: this.customConfig.instance.name,
242 instanceShortDescription: this.customConfig.instance.shortDescription,
243 instanceDescription: this.customConfig.instance.description,
244 instanceTerms: this.customConfig.instance.terms,
245 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
246 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
247 servicesTwitterUsername: this.customConfig.services.twitter.username,
248 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
249 cachePreviewsSize: this.customConfig.cache.previews.size,
250 cacheCaptionsSize: this.customConfig.cache.captions.size,
251 signupEnabled: this.customConfig.signup.enabled,
252 signupLimit: this.customConfig.signup.limit,
253 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
254 adminEmail: this.customConfig.admin.email,
255 userVideoQuota: this.customConfig.user.videoQuota,
256 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
257 transcodingThreads: this.customConfig.transcoding.threads,
258 transcodingEnabled: this.customConfig.transcoding.enabled,
259 customizationJavascript: this.customConfig.instance.customizations.javascript,
260 customizationCSS: this.customConfig.instance.customizations.css,
261 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
262 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
265 for (const resolution of this.resolutions) {
266 const key = this.getResolutionKey(resolution)
267 data[key] = this.customConfig.transcoding.resolutions[resolution]
270 this.form.patchValue(data)