]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
4983b0425d4280ef0d5459c3f527a08725c2b703
[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 { ConfirmService } from '@app/core'
4 import { ServerService } from '@app/core/server/server.service'
5 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
6 import { NotificationsService } from 'angular2-notifications'
7 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10
11 @Component({
12 selector: 'my-edit-custom-config',
13 templateUrl: './edit-custom-config.component.html',
14 styleUrls: [ './edit-custom-config.component.scss' ]
15 })
16 export class EditCustomConfigComponent extends FormReactive implements OnInit {
17 customConfig: CustomConfig
18
19 resolutions: string[] = []
20 transcodingThreadOptions: { label: string, value: number }[] = []
21
22 private oldCustomJavascript: string
23 private oldCustomCSS: string
24
25 constructor (
26 protected formValidatorService: FormValidatorService,
27 private customConfigValidatorsService: CustomConfigValidatorsService,
28 private userValidatorsService: UserValidatorsService,
29 private notificationsService: NotificationsService,
30 private configService: ConfigService,
31 private serverService: ServerService,
32 private confirmService: ConfirmService,
33 private i18n: I18n
34 ) {
35 super()
36
37 this.resolutions = [
38 this.i18n('240p'),
39 this.i18n('360p'),
40 this.i18n('480p'),
41 this.i18n('720p'),
42 this.i18n('1080p')
43 ]
44
45 this.transcodingThreadOptions = [
46 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
47 { value: 1, label: '1' },
48 { value: 2, label: '2' },
49 { value: 4, label: '4' },
50 { value: 8, label: '8' }
51 ]
52 }
53
54 get videoQuotaOptions () {
55 return this.configService.videoQuotaOptions
56 }
57
58 get videoQuotaDailyOptions () {
59 return this.configService.videoQuotaDailyOptions
60 }
61
62 getResolutionKey (resolution: string) {
63 return 'transcodingResolution' + resolution
64 }
65
66 ngOnInit () {
67 const formGroupData = {
68 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
69 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
70 instanceDescription: null,
71 instanceTerms: null,
72 instanceDefaultClientRoute: null,
73 instanceDefaultNSFWPolicy: null,
74 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
75 servicesTwitterWhitelisted: null,
76 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
77 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
78 signupEnabled: null,
79 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
80 signupRequiresEmailVerification: null,
81 importVideosHttpEnabled: null,
82 importVideosTorrentEnabled: null,
83 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
84 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
85 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
86 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
87 transcodingEnabled: null,
88 customizationJavascript: null,
89 customizationCSS: null
90 }
91
92 const defaultValues: BuildFormDefaultValues = {}
93 for (const resolution of this.resolutions) {
94 const key = this.getResolutionKey(resolution)
95 defaultValues[key] = 'false'
96 formGroupData[key] = null
97 }
98
99 this.buildForm(formGroupData)
100
101 this.configService.getCustomConfig()
102 .subscribe(
103 res => {
104 this.customConfig = res
105
106 this.oldCustomCSS = this.customConfig.instance.customizations.css
107 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
108
109 this.updateForm()
110 // Force form validation
111 this.forceCheck()
112 },
113
114 err => this.notificationsService.error(this.i18n('Error'), err.message)
115 )
116 }
117
118 isTranscodingEnabled () {
119 return this.form.value['transcodingEnabled'] === true
120 }
121
122 isSignupEnabled () {
123 return this.form.value['signupEnabled'] === true
124 }
125
126 async formValidated () {
127 const newCustomizationJavascript = this.form.value['customizationJavascript']
128 const newCustomizationCSS = this.form.value['customizationCSS']
129
130 const customizations = []
131 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
132 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
133
134 if (customizations.length !== 0) {
135 const customizationsText = customizations.join('/')
136
137 // FIXME: i18n service does not support string concatenation
138 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
139 this.i18n('This could lead to security issues or bugs if you do not understand it. ') +
140 this.i18n('Are you sure you want to update the configuration?')
141
142 const label = this.i18n('Please type') + ` "I understand the ${customizationsText} I set" ` + this.i18n('to confirm.')
143 const expectedInputValue = `I understand the ${customizationsText} I set`
144
145 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
146 if (confirmRes === false) return
147 }
148
149 const data: CustomConfig = {
150 instance: {
151 name: this.form.value['instanceName'],
152 shortDescription: this.form.value['instanceShortDescription'],
153 description: this.form.value['instanceDescription'],
154 terms: this.form.value['instanceTerms'],
155 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
156 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
157 customizations: {
158 javascript: this.form.value['customizationJavascript'],
159 css: this.form.value['customizationCSS']
160 }
161 },
162 services: {
163 twitter: {
164 username: this.form.value['servicesTwitterUsername'],
165 whitelisted: this.form.value['servicesTwitterWhitelisted']
166 }
167 },
168 cache: {
169 previews: {
170 size: this.form.value['cachePreviewsSize']
171 },
172 captions: {
173 size: this.form.value['cacheCaptionsSize']
174 }
175 },
176 signup: {
177 enabled: this.form.value['signupEnabled'],
178 limit: this.form.value['signupLimit'],
179 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
180 },
181 admin: {
182 email: this.form.value['adminEmail']
183 },
184 user: {
185 videoQuota: this.form.value['userVideoQuota'],
186 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
187 },
188 transcoding: {
189 enabled: this.form.value['transcodingEnabled'],
190 threads: this.form.value['transcodingThreads'],
191 resolutions: {
192 '240p': this.form.value[this.getResolutionKey('240p')],
193 '360p': this.form.value[this.getResolutionKey('360p')],
194 '480p': this.form.value[this.getResolutionKey('480p')],
195 '720p': this.form.value[this.getResolutionKey('720p')],
196 '1080p': this.form.value[this.getResolutionKey('1080p')]
197 }
198 },
199 import: {
200 videos: {
201 http: {
202 enabled: this.form.value['importVideosHttpEnabled']
203 },
204 torrent: {
205 enabled: this.form.value['importVideosTorrentEnabled']
206 }
207 }
208 }
209 }
210
211 this.configService.updateCustomConfig(data)
212 .subscribe(
213 res => {
214 this.customConfig = res
215
216 // Reload general configuration
217 this.serverService.loadConfig()
218
219 this.updateForm()
220
221 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
222 },
223
224 err => this.notificationsService.error(this.i18n('Error'), err.message)
225 )
226 }
227
228 private updateForm () {
229 const data = {
230 instanceName: this.customConfig.instance.name,
231 instanceShortDescription: this.customConfig.instance.shortDescription,
232 instanceDescription: this.customConfig.instance.description,
233 instanceTerms: this.customConfig.instance.terms,
234 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
235 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
236 servicesTwitterUsername: this.customConfig.services.twitter.username,
237 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
238 cachePreviewsSize: this.customConfig.cache.previews.size,
239 cacheCaptionsSize: this.customConfig.cache.captions.size,
240 signupEnabled: this.customConfig.signup.enabled,
241 signupLimit: this.customConfig.signup.limit,
242 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
243 adminEmail: this.customConfig.admin.email,
244 userVideoQuota: this.customConfig.user.videoQuota,
245 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
246 transcodingThreads: this.customConfig.transcoding.threads,
247 transcodingEnabled: this.customConfig.transcoding.enabled,
248 customizationJavascript: this.customConfig.instance.customizations.javascript,
249 customizationCSS: this.customConfig.instance.customizations.css,
250 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
251 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
252 }
253
254 for (const resolution of this.resolutions) {
255 const key = this.getResolutionKey(resolution)
256 data[key] = this.customConfig.transcoding.resolutions[resolution]
257 }
258
259 this.form.patchValue(data)
260 }
261
262 }