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