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