]>
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: '', | |
52 | transcodingThreads: '' | |
53 | } | |
54 | validationMessages = { | |
66b16caf | 55 | instanceName: INSTANCE_NAME.MESSAGES, |
fd206f0b C |
56 | cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES, |
57 | signupLimit: SIGNUP_LIMIT.MESSAGES, | |
58 | adminEmail: ADMIN_EMAIL.MESSAGES, | |
59 | userVideoQuota: USER_VIDEO_QUOTA.MESSAGES | |
60 | } | |
61 | ||
62 | constructor ( | |
63 | private formBuilder: FormBuilder, | |
64 | private router: Router, | |
65 | private notificationsService: NotificationsService, | |
66 | private configService: ConfigService, | |
67 | private serverService: ServerService | |
68 | ) { | |
69 | super() | |
70 | } | |
71 | ||
72 | getResolutionKey (resolution: string) { | |
73 | return 'transcodingResolution' + resolution | |
74 | } | |
75 | ||
76 | buildForm () { | |
77 | const formGroupData = { | |
66b16caf C |
78 | instanceName: [ '', INSTANCE_NAME.VALIDATORS ], |
79 | instanceDescription: [ '' ], | |
80 | instanceTerms: [ '' ], | |
fd206f0b C |
81 | cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ], |
82 | signupEnabled: [ ], | |
83 | signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ], | |
84 | adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ], | |
85 | userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ], | |
86 | transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ], | |
87 | transcodingEnabled: [ ] | |
88 | } | |
89 | ||
90 | for (const resolution of this.resolutions) { | |
91 | const key = this.getResolutionKey(resolution) | |
92 | formGroupData[key] = [ false ] | |
93 | } | |
94 | ||
95 | this.form = this.formBuilder.group(formGroupData) | |
96 | ||
97 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
98 | } | |
99 | ||
100 | ngOnInit () { | |
101 | this.buildForm() | |
102 | ||
103 | this.configService.getCustomConfig() | |
104 | .subscribe( | |
105 | res => { | |
106 | this.customConfig = res | |
107 | ||
108 | this.updateForm() | |
109 | }, | |
110 | ||
111 | err => this.notificationsService.error('Error', err.message) | |
112 | ) | |
113 | } | |
114 | ||
115 | isTranscodingEnabled () { | |
116 | return this.form.value['transcodingEnabled'] === true | |
117 | } | |
118 | ||
119 | isSignupEnabled () { | |
120 | return this.form.value['signupEnabled'] === true | |
121 | } | |
122 | ||
123 | formValidated () { | |
124 | const data = { | |
66b16caf C |
125 | instance: { |
126 | name: this.form.value['instanceName'], | |
127 | description: this.form.value['instanceDescription'], | |
128 | terms: this.form.value['instanceTerms'] | |
129 | }, | |
fd206f0b C |
130 | cache: { |
131 | previews: { | |
132 | size: this.form.value['cachePreviewsSize'] | |
133 | } | |
134 | }, | |
135 | signup: { | |
136 | enabled: this.form.value['signupEnabled'], | |
137 | limit: this.form.value['signupLimit'] | |
138 | }, | |
139 | admin: { | |
140 | email: this.form.value['adminEmail'] | |
141 | }, | |
142 | user: { | |
143 | videoQuota: this.form.value['userVideoQuota'] | |
144 | }, | |
145 | transcoding: { | |
146 | enabled: this.form.value['transcodingEnabled'], | |
147 | threads: this.form.value['transcodingThreads'], | |
148 | resolutions: { | |
149 | '240p': this.form.value[this.getResolutionKey('240p')], | |
150 | '360p': this.form.value[this.getResolutionKey('360p')], | |
151 | '480p': this.form.value[this.getResolutionKey('480p')], | |
152 | '720p': this.form.value[this.getResolutionKey('720p')], | |
153 | '1080p': this.form.value[this.getResolutionKey('1080p')] | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | this.configService.updateCustomConfig(data) | |
159 | .subscribe( | |
160 | res => { | |
161 | this.customConfig = res | |
162 | ||
163 | // Reload general configuration | |
164 | this.serverService.loadConfig() | |
165 | ||
166 | this.updateForm() | |
66b16caf C |
167 | |
168 | this.notificationsService.success('Success', 'Configuration updated.') | |
fd206f0b C |
169 | }, |
170 | ||
171 | err => this.notificationsService.error('Error', err.message) | |
172 | ) | |
173 | } | |
174 | ||
175 | private updateForm () { | |
176 | const data = { | |
66b16caf C |
177 | instanceName: this.customConfig.instance.name, |
178 | instanceDescription: this.customConfig.instance.description, | |
179 | instanceTerms: this.customConfig.instance.terms, | |
fd206f0b C |
180 | cachePreviewsSize: this.customConfig.cache.previews.size, |
181 | signupEnabled: this.customConfig.signup.enabled, | |
182 | signupLimit: this.customConfig.signup.limit, | |
183 | adminEmail: this.customConfig.admin.email, | |
184 | userVideoQuota: this.customConfig.user.videoQuota, | |
185 | transcodingThreads: this.customConfig.transcoding.threads, | |
186 | transcodingEnabled: this.customConfig.transcoding.enabled | |
187 | } | |
188 | ||
189 | for (const resolution of this.resolutions) { | |
190 | const key = this.getResolutionKey(resolution) | |
191 | data[key] = this.customConfig.transcoding.resolutions[resolution] | |
192 | } | |
193 | ||
194 | this.form.patchValue(data) | |
195 | } | |
196 | ||
197 | } |