1 import { Component, OnInit } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { ServerService } from '@app/core/server/server.service'
4 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
5 import { NotificationsService } from 'angular2-notifications'
6 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
11 selector: 'my-edit-custom-config',
12 templateUrl: './edit-custom-config.component.html',
13 styleUrls: [ './edit-custom-config.component.scss' ]
15 export class EditCustomConfigComponent extends FormReactive implements OnInit {
16 customConfig: CustomConfig
18 resolutions: string[] = []
19 transcodingThreadOptions: { label: string, value: number }[] = []
21 private oldCustomJavascript: string
22 private oldCustomCSS: string
25 protected formValidatorService: FormValidatorService,
26 private customConfigValidatorsService: CustomConfigValidatorsService,
27 private userValidatorsService: UserValidatorsService,
28 private notificationsService: NotificationsService,
29 private configService: ConfigService,
30 private serverService: ServerService,
43 this.transcodingThreadOptions = [
44 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
45 { value: 1, label: '1' },
46 { value: 2, label: '2' },
47 { value: 4, label: '4' },
48 { value: 8, label: '8' }
52 get videoQuotaOptions () {
53 return this.configService.videoQuotaOptions
56 get videoQuotaDailyOptions () {
57 return this.configService.videoQuotaDailyOptions
60 getResolutionKey (resolution: string) {
61 return 'transcodingResolution' + resolution
65 const formGroupData: { [key: string]: any } = {
66 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
67 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
68 instanceDescription: null,
70 instanceDefaultClientRoute: null,
71 instanceDefaultNSFWPolicy: null,
72 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
73 servicesTwitterWhitelisted: null,
74 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
75 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
77 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
78 signupRequiresEmailVerification: null,
79 importVideosHttpEnabled: null,
80 importVideosTorrentEnabled: null,
81 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
82 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
83 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
84 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
85 transcodingEnabled: null,
86 customizationJavascript: null,
87 customizationCSS: null
90 const defaultValues: BuildFormDefaultValues = {}
91 for (const resolution of this.resolutions) {
92 const key = this.getResolutionKey(resolution)
93 defaultValues[key] = 'false'
94 formGroupData[key] = null
97 this.buildForm(formGroupData)
99 this.configService.getCustomConfig()
102 this.customConfig = res
104 this.oldCustomCSS = this.customConfig.instance.customizations.css
105 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
108 // Force form validation
112 err => this.notificationsService.error(this.i18n('Error'), err.message)
116 isTranscodingEnabled () {
117 return this.form.value['transcodingEnabled'] === true
121 return this.form.value['signupEnabled'] === true
124 async formValidated () {
125 const data: CustomConfig = {
127 name: this.form.value['instanceName'],
128 shortDescription: this.form.value['instanceShortDescription'],
129 description: this.form.value['instanceDescription'],
130 terms: this.form.value['instanceTerms'],
131 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
132 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
134 javascript: this.form.value['customizationJavascript'],
135 css: this.form.value['customizationCSS']
140 username: this.form.value['servicesTwitterUsername'],
141 whitelisted: this.form.value['servicesTwitterWhitelisted']
146 size: this.form.value['cachePreviewsSize']
149 size: this.form.value['cacheCaptionsSize']
153 enabled: this.form.value['signupEnabled'],
154 limit: this.form.value['signupLimit'],
155 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
158 email: this.form.value['adminEmail']
161 videoQuota: this.form.value['userVideoQuota'],
162 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
165 enabled: this.form.value['transcodingEnabled'],
166 threads: this.form.value['transcodingThreads'],
168 '240p': this.form.value[this.getResolutionKey('240p')],
169 '360p': this.form.value[this.getResolutionKey('360p')],
170 '480p': this.form.value[this.getResolutionKey('480p')],
171 '720p': this.form.value[this.getResolutionKey('720p')],
172 '1080p': this.form.value[this.getResolutionKey('1080p')]
178 enabled: this.form.value['importVideosHttpEnabled']
181 enabled: this.form.value['importVideosTorrentEnabled']
187 this.configService.updateCustomConfig(data)
190 this.customConfig = res
192 // Reload general configuration
193 this.serverService.loadConfig()
197 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
200 err => this.notificationsService.error(this.i18n('Error'), err.message)
204 private updateForm () {
205 const data: { [key: string]: any } = {
206 instanceName: this.customConfig.instance.name,
207 instanceShortDescription: this.customConfig.instance.shortDescription,
208 instanceDescription: this.customConfig.instance.description,
209 instanceTerms: this.customConfig.instance.terms,
210 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
211 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
212 servicesTwitterUsername: this.customConfig.services.twitter.username,
213 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
214 cachePreviewsSize: this.customConfig.cache.previews.size,
215 cacheCaptionsSize: this.customConfig.cache.captions.size,
216 signupEnabled: this.customConfig.signup.enabled,
217 signupLimit: this.customConfig.signup.limit,
218 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
219 adminEmail: this.customConfig.admin.email,
220 userVideoQuota: this.customConfig.user.videoQuota,
221 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
222 transcodingThreads: this.customConfig.transcoding.threads,
223 transcodingEnabled: this.customConfig.transcoding.enabled,
224 customizationJavascript: this.customConfig.instance.customizations.javascript,
225 customizationCSS: this.customConfig.instance.customizations.css,
226 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
227 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
230 for (const resolution of this.resolutions) {
231 const key = this.getResolutionKey(resolution)
232 data[key] = this.customConfig.transcoding.resolutions[resolution]
235 this.form.patchValue(data)