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