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