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