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 { Notifier } from '@app/core'
6 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { SelectItem } from 'primeng/api'
10 import { forkJoin } from 'rxjs'
11 import { ServerConfig } from '@shared/models'
14 selector: 'my-edit-custom-config',
15 templateUrl: './edit-custom-config.component.html',
16 styleUrls: [ './edit-custom-config.component.scss' ]
18 export class EditCustomConfigComponent extends FormReactive implements OnInit {
19 customConfig: CustomConfig
21 resolutions: { id: string, label: string, description?: string }[] = []
22 transcodingThreadOptions: { label: string, value: number }[] = []
24 languageItems: SelectItem[] = []
25 categoryItems: SelectItem[] = []
27 isAutoFollowIndexEnabled = false
29 private serverConfig: ServerConfig
32 protected formValidatorService: FormValidatorService,
33 private customConfigValidatorsService: CustomConfigValidatorsService,
34 private userValidatorsService: UserValidatorsService,
35 private notifier: Notifier,
36 private configService: ConfigService,
37 private serverService: ServerService,
45 label: this.i18n('Audio-only'),
46 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
50 label: this.i18n('240p')
54 label: this.i18n('360p')
58 label: this.i18n('480p')
62 label: this.i18n('720p')
66 label: this.i18n('1080p')
70 label: this.i18n('2160p')
74 this.transcodingThreadOptions = [
75 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
76 { value: 1, label: '1' },
77 { value: 2, label: '2' },
78 { value: 4, label: '4' },
79 { value: 8, label: '8' }
83 get videoQuotaOptions () {
84 return this.configService.videoQuotaOptions
87 get videoQuotaDailyOptions () {
88 return this.configService.videoQuotaDailyOptions
91 get availableThemes () {
92 return this.serverConfig.theme.registered
96 getResolutionKey (resolution: string) {
97 return 'transcoding.resolutions.' + resolution
101 this.serverConfig = this.serverService.getTmpConfig()
102 this.serverService.getConfig()
103 .subscribe(config => this.serverConfig = config)
105 const formGroupData: { [key in keyof CustomConfig ]: any } = {
107 name: this.customConfigValidatorsService.INSTANCE_NAME,
108 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
112 defaultNSFWPolicy: null,
117 creationReason: null,
118 moderationInformation: null,
120 maintenanceLifetime: null,
123 hardwareInformation: null,
128 defaultClientRoute: null,
140 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
146 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
149 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
154 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
155 requiresEmailVerification: null
168 email: this.customConfigValidatorsService.ADMIN_EMAIL
174 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
175 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
179 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
180 allowAdditionalExtensions: null,
181 allowAudioFiles: null,
210 indexUrl: this.customConfigValidatorsService.INDEX_URL
216 const defaultValues = {
221 for (const resolution of this.resolutions) {
222 defaultValues.transcoding.resolutions[resolution.id] = 'false'
223 formGroupData.transcoding.resolutions[resolution.id] = null
226 this.buildForm(formGroupData)
228 this.checkTranscodingFields()
231 isTranscodingEnabled () {
232 return this.form.value['transcoding']['enabled'] === true
236 return this.form.value['signup']['enabled'] === true
239 async formValidated () {
240 this.configService.updateCustomConfig(this.form.getRawValue())
243 this.customConfig = res
245 // Reload general configuration
246 this.serverService.resetConfig()
250 this.notifier.success(this.i18n('Configuration updated.'))
253 err => this.notifier.error(err.message)
257 getSelectedLanguageLabel () {
258 return this.i18n('{{\'{0} languages selected')
261 getDefaultLanguageLabel () {
262 return this.i18n('No language')
265 getSelectedCategoryLabel () {
266 return this.i18n('{{\'{0} categories selected')
269 getDefaultCategoryLabel () {
270 return this.i18n('No category')
273 private updateForm () {
274 this.form.patchValue(this.customConfig)
277 private loadForm () {
279 this.configService.getCustomConfig(),
280 this.serverService.getVideoLanguages(),
281 this.serverService.getVideoCategories()
283 ([ config, languages, categories ]) => {
284 this.customConfig = config
286 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
287 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
290 // Force form validation
294 err => this.notifier.error(err.message)
298 private checkTranscodingFields () {
299 const hlsControl = this.form.get('transcoding.hls.enabled')
300 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
302 webtorrentControl.valueChanges
303 .subscribe(newValue => {
304 if (newValue === false && !hlsControl.disabled) {
308 if (newValue === true && !hlsControl.enabled) {
313 hlsControl.valueChanges
314 .subscribe(newValue => {
315 if (newValue === false && !webtorrentControl.disabled) {
316 webtorrentControl.disable()
319 if (newValue === true && !webtorrentControl.enabled) {
320 webtorrentControl.enable()