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