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 private serverConfig: ServerConfig
30 protected formValidatorService: FormValidatorService,
31 private customConfigValidatorsService: CustomConfigValidatorsService,
32 private userValidatorsService: UserValidatorsService,
33 private notifier: Notifier,
34 private configService: ConfigService,
35 private serverService: ServerService,
43 label: this.i18n('Audio-only'),
44 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
48 label: this.i18n('240p')
52 label: this.i18n('360p')
56 label: this.i18n('480p')
60 label: this.i18n('720p')
64 label: this.i18n('1080p')
68 label: this.i18n('2160p')
72 this.transcodingThreadOptions = [
73 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
74 { value: 1, label: '1' },
75 { value: 2, label: '2' },
76 { value: 4, label: '4' },
77 { value: 8, label: '8' }
81 get videoQuotaOptions () {
82 return this.configService.videoQuotaOptions
85 get videoQuotaDailyOptions () {
86 return this.configService.videoQuotaDailyOptions
89 get availableThemes () {
90 return this.serverConfig.theme.registered
94 getResolutionKey (resolution: string) {
95 return 'transcoding.resolutions.' + resolution
99 this.serverConfig = this.serverService.getTmpConfig()
100 this.serverService.getConfig()
101 .subscribe(config => this.serverConfig = config)
103 const formGroupData: { [key in keyof CustomConfig ]: any } = {
105 name: this.customConfigValidatorsService.INSTANCE_NAME,
106 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
110 defaultNSFWPolicy: null,
115 creationReason: null,
116 moderationInformation: null,
118 maintenanceLifetime: null,
121 hardwareInformation: null,
126 defaultClientRoute: null,
138 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
144 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
147 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
152 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
153 requiresEmailVerification: null
166 email: this.customConfigValidatorsService.ADMIN_EMAIL
172 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
173 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
177 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
178 allowAdditionalExtensions: null,
179 allowAudioFiles: null,
208 indexUrl: this.customConfigValidatorsService.INDEX_URL
214 const defaultValues = {
219 for (const resolution of this.resolutions) {
220 defaultValues.transcoding.resolutions[resolution.id] = 'false'
221 formGroupData.transcoding.resolutions[resolution.id] = null
224 this.buildForm(formGroupData)
226 this.checkTranscodingFields()
229 isTranscodingEnabled () {
230 return this.form.value['transcoding']['enabled'] === true
234 return this.form.value['signup']['enabled'] === true
237 isAutoFollowIndexEnabled () {
238 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
241 async formValidated () {
242 this.configService.updateCustomConfig(this.form.getRawValue())
245 this.customConfig = res
247 // Reload general configuration
248 this.serverService.resetConfig()
252 this.notifier.success(this.i18n('Configuration updated.'))
255 err => this.notifier.error(err.message)
259 getSelectedLanguageLabel () {
260 return this.i18n('{{\'{0} languages selected')
263 getDefaultLanguageLabel () {
264 return this.i18n('No language')
267 getSelectedCategoryLabel () {
268 return this.i18n('{{\'{0} categories selected')
271 getDefaultCategoryLabel () {
272 return this.i18n('No category')
275 private updateForm () {
276 this.form.patchValue(this.customConfig)
279 private loadForm () {
281 this.configService.getCustomConfig(),
282 this.serverService.getVideoLanguages(),
283 this.serverService.getVideoCategories()
285 ([ config, languages, categories ]) => {
286 this.customConfig = config
288 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
289 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
292 // Force form validation
296 err => this.notifier.error(err.message)
300 private checkTranscodingFields () {
301 const hlsControl = this.form.get('transcoding.hls.enabled')
302 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
304 webtorrentControl.valueChanges
305 .subscribe(newValue => {
306 if (newValue === false && !hlsControl.disabled) {
310 if (newValue === true && !hlsControl.enabled) {
315 hlsControl.valueChanges
316 .subscribe(newValue => {
317 if (newValue === false && !webtorrentControl.disabled) {
318 webtorrentControl.disable()
321 if (newValue === true && !webtorrentControl.enabled) {
322 webtorrentControl.enable()