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