]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
9b3bd86f197a8558008f52c200a579783c411a9c
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { ConfirmService } from '@app/core'
4 import { ServerService } from '@app/core/server/server.service'
5 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
6 import { NotificationsService } from 'angular2-notifications'
7 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
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 {
17 static videoQuotaOptions = [
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 ]
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
41 transcodingThreadOptions = [
42 { value: 0, label: 'Auto (via ffmpeg)' },
43 { value: 1, label: '1' },
44 { value: 2, label: '2' },
45 { value: 4, label: '4' },
46 { value: 8, label: '8' }
47 ]
48
49 private oldCustomJavascript: string
50 private oldCustomCSS: string
51
52 constructor (
53 protected formValidatorService: FormValidatorService,
54 private customConfigValidatorsService: CustomConfigValidatorsService,
55 private userValidatorsService: UserValidatorsService,
56 private notificationsService: NotificationsService,
57 private configService: ConfigService,
58 private serverService: ServerService,
59 private confirmService: ConfirmService,
60 private i18n: I18n
61 ) {
62 super()
63 }
64
65 get videoQuotaOptions () {
66 return EditCustomConfigComponent.videoQuotaOptions
67 }
68
69 get videoQuotaDailyOptions () {
70 return EditCustomConfigComponent.videoQuotaDailyOptions
71 }
72
73 getResolutionKey (resolution: string) {
74 return 'transcodingResolution' + resolution
75 }
76
77 ngOnInit () {
78 const formGroupData = {
79 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
80 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
81 instanceDescription: null,
82 instanceTerms: null,
83 instanceDefaultClientRoute: null,
84 instanceDefaultNSFWPolicy: null,
85 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
86 servicesTwitterWhitelisted: null,
87 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
88 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
89 signupEnabled: null,
90 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
91 signupRequiresEmailVerification: null,
92 importVideosHttpEnabled: null,
93 importVideosTorrentEnabled: null,
94 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
95 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
96 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
97 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
98 transcodingEnabled: null,
99 customizationJavascript: null,
100 customizationCSS: null
101 }
102
103 const defaultValues: BuildFormDefaultValues = {}
104 for (const resolution of this.resolutions) {
105 const key = this.getResolutionKey(resolution)
106 defaultValues[key] = 'false'
107 formGroupData[key] = null
108 }
109
110 this.buildForm(formGroupData)
111
112 this.configService.getCustomConfig()
113 .subscribe(
114 res => {
115 this.customConfig = res
116
117 this.oldCustomCSS = this.customConfig.instance.customizations.css
118 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
119
120 this.updateForm()
121 // Force form validation
122 this.forceCheck()
123 },
124
125 err => this.notificationsService.error(this.i18n('Error'), err.message)
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
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
148 // FIXME: i18n service does not support string concatenation
149 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
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?')
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`
155
156 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
157 if (confirmRes === false) return
158 }
159
160 const data: CustomConfig = {
161 instance: {
162 name: this.form.value['instanceName'],
163 shortDescription: this.form.value['instanceShortDescription'],
164 description: this.form.value['instanceDescription'],
165 terms: this.form.value['instanceTerms'],
166 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
167 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
168 customizations: {
169 javascript: this.form.value['customizationJavascript'],
170 css: this.form.value['customizationCSS']
171 }
172 },
173 services: {
174 twitter: {
175 username: this.form.value['servicesTwitterUsername'],
176 whitelisted: this.form.value['servicesTwitterWhitelisted']
177 }
178 },
179 cache: {
180 previews: {
181 size: this.form.value['cachePreviewsSize']
182 },
183 captions: {
184 size: this.form.value['cacheCaptionsSize']
185 }
186 },
187 signup: {
188 enabled: this.form.value['signupEnabled'],
189 limit: this.form.value['signupLimit'],
190 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
191 },
192 admin: {
193 email: this.form.value['adminEmail']
194 },
195 user: {
196 videoQuota: this.form.value['userVideoQuota'],
197 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
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 }
209 },
210 import: {
211 videos: {
212 http: {
213 enabled: this.form.value['importVideosHttpEnabled']
214 },
215 torrent: {
216 enabled: this.form.value['importVideosTorrentEnabled']
217 }
218 }
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()
231
232 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
233 },
234
235 err => this.notificationsService.error(this.i18n('Error'), err.message)
236 )
237 }
238
239 private updateForm () {
240 const data = {
241 instanceName: this.customConfig.instance.name,
242 instanceShortDescription: this.customConfig.instance.shortDescription,
243 instanceDescription: this.customConfig.instance.description,
244 instanceTerms: this.customConfig.instance.terms,
245 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
246 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
247 servicesTwitterUsername: this.customConfig.services.twitter.username,
248 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
249 cachePreviewsSize: this.customConfig.cache.previews.size,
250 cacheCaptionsSize: this.customConfig.cache.captions.size,
251 signupEnabled: this.customConfig.signup.enabled,
252 signupLimit: this.customConfig.signup.limit,
253 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
254 adminEmail: this.customConfig.admin.email,
255 userVideoQuota: this.customConfig.user.videoQuota,
256 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
257 transcodingThreads: this.customConfig.transcoding.threads,
258 transcodingEnabled: this.customConfig.transcoding.enabled,
259 customizationJavascript: this.customConfig.instance.customizations.javascript,
260 customizationCSS: this.customConfig.instance.customizations.css,
261 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
262 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
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 }