]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Merge branch 'feature/audio-upload' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
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
10 @Component({
11 selector: 'my-edit-custom-config',
12 templateUrl: './edit-custom-config.component.html',
13 styleUrls: [ './edit-custom-config.component.scss' ]
14 })
15 export class EditCustomConfigComponent extends FormReactive implements OnInit {
16 customConfig: CustomConfig
17
18 resolutions: string[] = []
19 transcodingThreadOptions: { label: string, value: number }[] = []
20
21 constructor (
22 protected formValidatorService: FormValidatorService,
23 private customConfigValidatorsService: CustomConfigValidatorsService,
24 private userValidatorsService: UserValidatorsService,
25 private notifier: Notifier,
26 private configService: ConfigService,
27 private serverService: ServerService,
28 private i18n: I18n
29 ) {
30 super()
31
32 this.resolutions = [
33 this.i18n('240p'),
34 this.i18n('360p'),
35 this.i18n('480p'),
36 this.i18n('720p'),
37 this.i18n('1080p')
38 ]
39
40 this.transcodingThreadOptions = [
41 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
42 { value: 1, label: '1' },
43 { value: 2, label: '2' },
44 { value: 4, label: '4' },
45 { value: 8, label: '8' }
46 ]
47 }
48
49 get videoQuotaOptions () {
50 return this.configService.videoQuotaOptions
51 }
52
53 get videoQuotaDailyOptions () {
54 return this.configService.videoQuotaDailyOptions
55 }
56
57 getResolutionKey (resolution: string) {
58 return 'transcoding.resolutions.' + resolution
59 }
60
61 ngOnInit () {
62 const formGroupData: { [key in keyof CustomConfig ]: any } = {
63 instance: {
64 name: this.customConfigValidatorsService.INSTANCE_NAME,
65 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
66 description: null,
67 terms: null,
68 defaultClientRoute: null,
69 isNSFW: false,
70 defaultNSFWPolicy: null,
71 customizations: {
72 javascript: null,
73 css: null
74 }
75 },
76 services: {
77 twitter: {
78 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
79 whitelisted: null
80 }
81 },
82 cache: {
83 previews: {
84 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
85 },
86 captions: {
87 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
88 }
89 },
90 signup: {
91 enabled: null,
92 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
93 requiresEmailVerification: null
94 },
95 import: {
96 videos: {
97 http: {
98 enabled: null
99 },
100 torrent: {
101 enabled: null
102 }
103 }
104 },
105 admin: {
106 email: this.customConfigValidatorsService.ADMIN_EMAIL
107 },
108 contactForm: {
109 enabled: null
110 },
111 user: {
112 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
113 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
114 },
115 transcoding: {
116 enabled: null,
117 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
118 allowAdditionalExtensions: null,
119 allowAudioFiles: null,
120 resolutions: {}
121 },
122 autoBlacklist: {
123 videos: {
124 ofUsers: {
125 enabled: null
126 }
127 }
128 },
129 followers: {
130 instance: {
131 enabled: null,
132 manualApproval: null
133 }
134 }
135 }
136
137 const defaultValues = {
138 transcoding: {
139 resolutions: {}
140 }
141 }
142 for (const resolution of this.resolutions) {
143 defaultValues.transcoding.resolutions[resolution] = 'false'
144 formGroupData.transcoding.resolutions[resolution] = null
145 }
146
147 this.buildForm(formGroupData)
148
149 this.configService.getCustomConfig()
150 .subscribe(
151 res => {
152 this.customConfig = res
153
154 this.updateForm()
155 // Force form validation
156 this.forceCheck()
157 },
158
159 err => this.notifier.error(err.message)
160 )
161 }
162
163 isTranscodingEnabled () {
164 return this.form.value['transcoding']['enabled'] === true
165 }
166
167 isSignupEnabled () {
168 return this.form.value['signup']['enabled'] === true
169 }
170
171 async formValidated () {
172 this.configService.updateCustomConfig(this.form.value)
173 .subscribe(
174 res => {
175 this.customConfig = res
176
177 // Reload general configuration
178 this.serverService.loadConfig()
179
180 this.updateForm()
181
182 this.notifier.success(this.i18n('Configuration updated.'))
183 },
184
185 err => this.notifier.error(err.message)
186 )
187 }
188
189 private updateForm () {
190 this.form.patchValue(this.customConfig)
191 }
192
193 }