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