PeerTube/client/src/app/+admin/config/edit-custom-config/edit-custom-config.componen...

242 lines
8.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core'
import { ConfigService } from '@app/+admin/config/shared/config.service'
import { ServerService } from '@app/core/server/server.service'
2018-06-05 15:01:45 +02:00
import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
import { Notifier } from '@app/core'
2018-02-28 18:04:46 +01:00
import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
2018-06-04 16:21:17 +02:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-06-05 10:58:45 +02:00
import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
@Component({
selector: 'my-edit-custom-config',
templateUrl: './edit-custom-config.component.html',
styleUrls: [ './edit-custom-config.component.scss' ]
})
export class EditCustomConfigComponent extends FormReactive implements OnInit {
customConfig: CustomConfig
2018-09-26 14:46:54 +02:00
resolutions: string[] = []
transcodingThreadOptions: { label: string, value: number }[] = []
private oldCustomJavascript: string
private oldCustomCSS: string
constructor (
2018-06-05 10:58:45 +02:00
protected formValidatorService: FormValidatorService,
2018-06-05 15:01:45 +02:00
private customConfigValidatorsService: CustomConfigValidatorsService,
private userValidatorsService: UserValidatorsService,
private notifier: Notifier,
private configService: ConfigService,
private serverService: ServerService,
2018-06-04 16:21:17 +02:00
private i18n: I18n
) {
super()
2018-09-26 14:46:54 +02:00
this.resolutions = [
this.i18n('240p'),
this.i18n('360p'),
this.i18n('480p'),
this.i18n('720p'),
this.i18n('1080p')
]
this.transcodingThreadOptions = [
{ value: 0, label: this.i18n('Auto (via ffmpeg)') },
{ value: 1, label: '1' },
{ value: 2, label: '2' },
{ value: 4, label: '4' },
{ value: 8, label: '8' }
]
}
2018-08-28 17:39:29 +02:00
get videoQuotaOptions () {
2018-09-26 14:46:54 +02:00
return this.configService.videoQuotaOptions
2018-08-28 17:39:29 +02:00
}
get videoQuotaDailyOptions () {
2018-09-26 14:46:54 +02:00
return this.configService.videoQuotaDailyOptions
2018-08-28 17:39:29 +02:00
}
getResolutionKey (resolution: string) {
return 'transcodingResolution' + resolution
}
2018-06-05 10:58:45 +02:00
ngOnInit () {
2018-10-18 14:35:31 +02:00
const formGroupData: { [key: string]: any } = {
2018-06-05 15:01:45 +02:00
instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
2018-06-05 10:58:45 +02:00
instanceDescription: null,
instanceTerms: null,
instanceDefaultClientRoute: null,
instanceDefaultNSFWPolicy: null,
2018-06-05 15:01:45 +02:00
servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
2018-06-05 10:58:45 +02:00
servicesTwitterWhitelisted: null,
2018-06-05 15:01:45 +02:00
cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
2018-07-12 19:02:00 +02:00
cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
2018-06-05 10:58:45 +02:00
signupEnabled: null,
2018-06-05 15:01:45 +02:00
signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
signupRequiresEmailVerification: null,
2018-08-03 11:10:31 +02:00
importVideosHttpEnabled: null,
2018-08-07 10:07:53 +02:00
importVideosTorrentEnabled: null,
2018-06-05 15:01:45 +02:00
adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
2018-06-05 15:01:45 +02:00
transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
2018-12-11 14:52:50 +01:00
transcodingAllowAdditionalExtensions: null,
2018-06-05 10:58:45 +02:00
transcodingEnabled: null,
customizationJavascript: null,
customizationCSS: null
}
2018-06-05 10:58:45 +02:00
const defaultValues: BuildFormDefaultValues = {}
for (const resolution of this.resolutions) {
const key = this.getResolutionKey(resolution)
2018-06-05 10:58:45 +02:00
defaultValues[key] = 'false'
formGroupData[key] = null
}
2018-06-05 10:58:45 +02:00
this.buildForm(formGroupData)
this.configService.getCustomConfig()
.subscribe(
res => {
this.customConfig = res
this.oldCustomCSS = this.customConfig.instance.customizations.css
this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
this.updateForm()
2018-03-23 09:31:42 +01:00
// Force form validation
this.forceCheck()
},
err => this.notifier.error(err.message)
)
}
isTranscodingEnabled () {
return this.form.value['transcodingEnabled'] === true
}
isSignupEnabled () {
return this.form.value['signupEnabled'] === true
}
async formValidated () {
2018-03-01 13:57:29 +01:00
const data: CustomConfig = {
instance: {
name: this.form.value['instanceName'],
2018-03-15 14:31:08 +01:00
shortDescription: this.form.value['instanceShortDescription'],
description: this.form.value['instanceDescription'],
terms: this.form.value['instanceTerms'],
2018-03-01 13:57:29 +01:00
defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
customizations: {
javascript: this.form.value['customizationJavascript'],
css: this.form.value['customizationCSS']
}
},
services: {
twitter: {
username: this.form.value['servicesTwitterUsername'],
whitelisted: this.form.value['servicesTwitterWhitelisted']
}
},
cache: {
previews: {
size: this.form.value['cachePreviewsSize']
2018-07-12 19:02:00 +02:00
},
captions: {
size: this.form.value['cacheCaptionsSize']
}
},
signup: {
enabled: this.form.value['signupEnabled'],
limit: this.form.value['signupLimit'],
requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
},
admin: {
email: this.form.value['adminEmail']
},
user: {
videoQuota: this.form.value['userVideoQuota'],
videoQuotaDaily: this.form.value['userVideoQuotaDaily']
},
transcoding: {
enabled: this.form.value['transcodingEnabled'],
2018-12-11 14:52:50 +01:00
allowAdditionalExtensions: this.form.value['transcodingAllowAdditionalExtensions'],
threads: this.form.value['transcodingThreads'],
resolutions: {
'240p': this.form.value[this.getResolutionKey('240p')],
'360p': this.form.value[this.getResolutionKey('360p')],
'480p': this.form.value[this.getResolutionKey('480p')],
'720p': this.form.value[this.getResolutionKey('720p')],
'1080p': this.form.value[this.getResolutionKey('1080p')]
}
2018-08-03 11:10:31 +02:00
},
import: {
videos: {
http: {
enabled: this.form.value['importVideosHttpEnabled']
2018-08-07 10:07:53 +02:00
},
torrent: {
enabled: this.form.value['importVideosTorrentEnabled']
2018-08-03 11:10:31 +02:00
}
}
}
}
this.configService.updateCustomConfig(data)
.subscribe(
res => {
this.customConfig = res
// Reload general configuration
this.serverService.loadConfig()
this.updateForm()
this.notifier.success(this.i18n('Configuration updated.'))
},
err => this.notifier.error(err.message)
)
}
private updateForm () {
2018-10-18 14:35:31 +02:00
const data: { [key: string]: any } = {
instanceName: this.customConfig.instance.name,
2018-03-15 14:31:08 +01:00
instanceShortDescription: this.customConfig.instance.shortDescription,
instanceDescription: this.customConfig.instance.description,
instanceTerms: this.customConfig.instance.terms,
2018-03-01 13:57:29 +01:00
instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
servicesTwitterUsername: this.customConfig.services.twitter.username,
servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
cachePreviewsSize: this.customConfig.cache.previews.size,
2018-07-13 18:21:19 +02:00
cacheCaptionsSize: this.customConfig.cache.captions.size,
signupEnabled: this.customConfig.signup.enabled,
signupLimit: this.customConfig.signup.limit,
signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
adminEmail: this.customConfig.admin.email,
userVideoQuota: this.customConfig.user.videoQuota,
userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
transcodingThreads: this.customConfig.transcoding.threads,
transcodingEnabled: this.customConfig.transcoding.enabled,
2018-12-11 14:52:50 +01:00
transcodingAllowAdditionalExtensions: this.customConfig.transcoding.allowAdditionalExtensions,
customizationJavascript: this.customConfig.instance.customizations.javascript,
2018-08-03 11:10:31 +02:00
customizationCSS: this.customConfig.instance.customizations.css,
2018-08-07 10:07:53 +02:00
importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
}
for (const resolution of this.resolutions) {
const key = this.getResolutionKey(resolution)
data[key] = this.customConfig.transcoding.resolutions[resolution]
}
this.form.patchValue(data)
}
}