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