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