]>
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' | |
5 | import { ServerService } from '@app/core/server/server.service' | |
6 | import { FormReactive, USER_VIDEO_QUOTA } from '@app/shared' | |
66b16caf C |
7 | import { |
8 | ADMIN_EMAIL, | |
9 | CACHE_PREVIEWS_SIZE, | |
10 | INSTANCE_NAME, | |
11 | SIGNUP_LIMIT, | |
12 | TRANSCODING_THREADS | |
13 | } from '@app/shared/forms/form-validators/custom-config' | |
fd206f0b C |
14 | import { NotificationsService } from 'angular2-notifications' |
15 | import { CustomConfig } from '../../../../../../shared/models/config/custom-config.model' | |
16 | ||
17 | @Component({ | |
18 | selector: 'my-edit-custom-config', | |
19 | templateUrl: './edit-custom-config.component.html', | |
20 | styleUrls: [ './edit-custom-config.component.scss' ] | |
21 | }) | |
22 | export class EditCustomConfigComponent extends FormReactive implements OnInit { | |
23 | customConfig: CustomConfig | |
24 | resolutions = [ '240p', '360p', '480p', '720p', '1080p' ] | |
25 | ||
26 | videoQuotaOptions = [ | |
27 | { value: -1, label: 'Unlimited' }, | |
28 | { value: 0, label: '0' }, | |
29 | { value: 100 * 1024 * 1024, label: '100MB' }, | |
30 | { value: 500 * 1024 * 1024, label: '500MB' }, | |
31 | { value: 1024 * 1024 * 1024, label: '1GB' }, | |
32 | { value: 5 * 1024 * 1024 * 1024, label: '5GB' }, | |
33 | { value: 20 * 1024 * 1024 * 1024, label: '20GB' }, | |
34 | { value: 50 * 1024 * 1024 * 1024, label: '50GB' } | |
35 | ] | |
36 | transcodingThreadOptions = [ | |
37 | { value: 1, label: '1' }, | |
38 | { value: 2, label: '2' }, | |
39 | { value: 4, label: '4' }, | |
40 | { value: 8, label: '8' } | |
41 | ] | |
42 | ||
43 | form: FormGroup | |
44 | formErrors = { | |
66b16caf C |
45 | instanceName: '', |
46 | instanceDescription: '', | |
47 | instanceTerms: '', | |
fd206f0b C |
48 | cachePreviewsSize: '', |
49 | signupLimit: '', | |
50 | adminEmail: '', | |
51 | userVideoQuota: '', | |
00b5556c C |
52 | transcodingThreads: '', |
53 | customizationJavascript: '', | |
54 | customizationCSS: '' | |
fd206f0b C |
55 | } |
56 | validationMessages = { | |
66b16caf | 57 | instanceName: INSTANCE_NAME.MESSAGES, |
fd206f0b C |
58 | cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES, |
59 | signupLimit: SIGNUP_LIMIT.MESSAGES, | |
60 | adminEmail: ADMIN_EMAIL.MESSAGES, | |
61 | userVideoQuota: USER_VIDEO_QUOTA.MESSAGES | |
62 | } | |
63 | ||
64 | constructor ( | |
65 | private formBuilder: FormBuilder, | |
66 | private router: Router, | |
67 | private notificationsService: NotificationsService, | |
68 | private configService: ConfigService, | |
69 | private serverService: ServerService | |
70 | ) { | |
71 | super() | |
72 | } | |
73 | ||
74 | getResolutionKey (resolution: string) { | |
75 | return 'transcodingResolution' + resolution | |
76 | } | |
77 | ||
78 | buildForm () { | |
79 | const formGroupData = { | |
66b16caf C |
80 | instanceName: [ '', INSTANCE_NAME.VALIDATORS ], |
81 | instanceDescription: [ '' ], | |
82 | instanceTerms: [ '' ], | |
fd206f0b C |
83 | cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ], |
84 | signupEnabled: [ ], | |
85 | signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ], | |
86 | adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ], | |
87 | userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ], | |
88 | transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ], | |
00b5556c C |
89 | transcodingEnabled: [ ], |
90 | customizationJavascript: [ '' ], | |
91 | customizationCSS: [ '' ] | |
fd206f0b C |
92 | } |
93 | ||
94 | for (const resolution of this.resolutions) { | |
95 | const key = this.getResolutionKey(resolution) | |
96 | formGroupData[key] = [ false ] | |
97 | } | |
98 | ||
99 | this.form = this.formBuilder.group(formGroupData) | |
100 | ||
101 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
102 | } | |
103 | ||
104 | ngOnInit () { | |
105 | this.buildForm() | |
106 | ||
107 | this.configService.getCustomConfig() | |
108 | .subscribe( | |
109 | res => { | |
110 | this.customConfig = res | |
111 | ||
112 | this.updateForm() | |
113 | }, | |
114 | ||
115 | err => this.notificationsService.error('Error', err.message) | |
116 | ) | |
117 | } | |
118 | ||
119 | isTranscodingEnabled () { | |
120 | return this.form.value['transcodingEnabled'] === true | |
121 | } | |
122 | ||
123 | isSignupEnabled () { | |
124 | return this.form.value['signupEnabled'] === true | |
125 | } | |
126 | ||
127 | formValidated () { | |
128 | const data = { | |
66b16caf C |
129 | instance: { |
130 | name: this.form.value['instanceName'], | |
131 | description: this.form.value['instanceDescription'], | |
00b5556c C |
132 | terms: this.form.value['instanceTerms'], |
133 | customizations: { | |
134 | javascript: this.form.value['customizationJavascript'], | |
135 | css: this.form.value['customizationCSS'] | |
136 | } | |
66b16caf | 137 | }, |
fd206f0b C |
138 | cache: { |
139 | previews: { | |
140 | size: this.form.value['cachePreviewsSize'] | |
141 | } | |
142 | }, | |
143 | signup: { | |
144 | enabled: this.form.value['signupEnabled'], | |
145 | limit: this.form.value['signupLimit'] | |
146 | }, | |
147 | admin: { | |
148 | email: this.form.value['adminEmail'] | |
149 | }, | |
150 | user: { | |
151 | videoQuota: this.form.value['userVideoQuota'] | |
152 | }, | |
153 | transcoding: { | |
154 | enabled: this.form.value['transcodingEnabled'], | |
155 | threads: this.form.value['transcodingThreads'], | |
156 | resolutions: { | |
157 | '240p': this.form.value[this.getResolutionKey('240p')], | |
158 | '360p': this.form.value[this.getResolutionKey('360p')], | |
159 | '480p': this.form.value[this.getResolutionKey('480p')], | |
160 | '720p': this.form.value[this.getResolutionKey('720p')], | |
161 | '1080p': this.form.value[this.getResolutionKey('1080p')] | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | this.configService.updateCustomConfig(data) | |
167 | .subscribe( | |
168 | res => { | |
169 | this.customConfig = res | |
170 | ||
171 | // Reload general configuration | |
172 | this.serverService.loadConfig() | |
173 | ||
174 | this.updateForm() | |
66b16caf C |
175 | |
176 | this.notificationsService.success('Success', 'Configuration updated.') | |
fd206f0b C |
177 | }, |
178 | ||
179 | err => this.notificationsService.error('Error', err.message) | |
180 | ) | |
181 | } | |
182 | ||
183 | private updateForm () { | |
184 | const data = { | |
66b16caf C |
185 | instanceName: this.customConfig.instance.name, |
186 | instanceDescription: this.customConfig.instance.description, | |
187 | instanceTerms: this.customConfig.instance.terms, | |
fd206f0b C |
188 | cachePreviewsSize: this.customConfig.cache.previews.size, |
189 | signupEnabled: this.customConfig.signup.enabled, | |
190 | signupLimit: this.customConfig.signup.limit, | |
191 | adminEmail: this.customConfig.admin.email, | |
192 | userVideoQuota: this.customConfig.user.videoQuota, | |
193 | transcodingThreads: this.customConfig.transcoding.threads, | |
00b5556c C |
194 | transcodingEnabled: this.customConfig.transcoding.enabled, |
195 | customizationJavascript: this.customConfig.instance.customizations.javascript, | |
196 | customizationCSS: this.customConfig.instance.customizations.css | |
fd206f0b C |
197 | } |
198 | ||
199 | for (const resolution of this.resolutions) { | |
200 | const key = this.getResolutionKey(resolution) | |
201 | data[key] = this.customConfig.transcoding.resolutions[resolution] | |
202 | } | |
203 | ||
204 | this.form.patchValue(data) | |
205 | } | |
206 | ||
207 | } |