aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/config.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-02-11 11:52:34 +0100
committerChocobozzz <me@florianbigard.com>2019-02-11 11:52:34 +0100
commit88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch)
treeb242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/controllers/api/config.ts
parent53a94c7cfa8368da4cd248d65df8346905938f0c (diff)
parent9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff)
downloadPeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz
PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst
PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/controllers/api/config.ts')
-rw-r--r--server/controllers/api/config.ts77
1 files changed, 48 insertions, 29 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts
index 03c1cec7b..1f3341bc0 100644
--- a/server/controllers/api/config.ts
+++ b/server/controllers/api/config.ts
@@ -1,5 +1,5 @@
1import * as express from 'express' 1import * as express from 'express'
2import { omit } from 'lodash' 2import { snakeCase } from 'lodash'
3import { ServerConfig, UserRight } from '../../../shared' 3import { ServerConfig, UserRight } from '../../../shared'
4import { About } from '../../../shared/models/server/about.model' 4import { About } from '../../../shared/models/server/about.model'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model' 5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
@@ -10,7 +10,10 @@ import { customConfigUpdateValidator } from '../../middlewares/validators/config
10import { ClientHtml } from '../../lib/client-html' 10import { ClientHtml } from '../../lib/client-html'
11import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' 11import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12import { remove, writeJSON } from 'fs-extra' 12import { remove, writeJSON } from 'fs-extra'
13import { getVersion } from '../../helpers/utils' 13import { getServerCommit } from '../../helpers/utils'
14import { Emailer } from '../../lib/emailer'
15import { isNumeric } from 'validator'
16import { objectConverter } from '../../helpers/core-utils'
14 17
15const packageJSON = require('../../../../package.json') 18const packageJSON = require('../../../../package.json')
16const configRouter = express.Router() 19const configRouter = express.Router()
@@ -40,11 +43,11 @@ configRouter.delete('/custom',
40) 43)
41 44
42let serverCommit: string 45let serverCommit: string
43async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { 46async function getConfig (req: express.Request, res: express.Response) {
44 const allowed = await isSignupAllowed() 47 const allowed = await isSignupAllowed()
45 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) 48 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
46 serverCommit = (serverCommit) ? serverCommit : await getVersion() 49
47 if (serverCommit === packageJSON.version) serverCommit = '' 50 if (serverCommit === undefined) serverCommit = await getServerCommit()
48 51
49 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) 52 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
50 .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true) 53 .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
@@ -61,6 +64,12 @@ async function getConfig (req: express.Request, res: express.Response, next: exp
61 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS 64 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
62 } 65 }
63 }, 66 },
67 email: {
68 enabled: Emailer.isEnabled()
69 },
70 contactForm: {
71 enabled: CONFIG.CONTACT_FORM.ENABLED
72 },
64 serverVersion: packageJSON.version, 73 serverVersion: packageJSON.version,
65 serverCommit, 74 serverCommit,
66 signup: { 75 signup: {
@@ -69,6 +78,9 @@ async function getConfig (req: express.Request, res: express.Response, next: exp
69 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION 78 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
70 }, 79 },
71 transcoding: { 80 transcoding: {
81 hls: {
82 enabled: CONFIG.TRANSCODING.HLS.ENABLED
83 },
72 enabledResolutions 84 enabledResolutions
73 }, 85 },
74 import: { 86 import: {
@@ -111,6 +123,11 @@ async function getConfig (req: express.Request, res: express.Response, next: exp
111 user: { 123 user: {
112 videoQuota: CONFIG.USER.VIDEO_QUOTA, 124 videoQuota: CONFIG.USER.VIDEO_QUOTA,
113 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY 125 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
126 },
127 trending: {
128 videos: {
129 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
130 }
114 } 131 }
115 } 132 }
116 133
@@ -150,32 +167,10 @@ async function deleteCustomConfig (req: express.Request, res: express.Response,
150} 167}
151 168
152async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { 169async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
153 const toUpdate: CustomConfig = req.body
154 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig()) 170 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
155 171
156 // Force number conversion 172 // camelCase to snake_case key + Force number conversion
157 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10) 173 const toUpdateJSON = convertCustomConfigBody(req.body)
158 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
159 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
160 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
161 toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
162 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
163
164 // camelCase to snake_case key
165 const toUpdateJSON = omit(
166 toUpdate,
167 'user.videoQuota',
168 'instance.defaultClientRoute',
169 'instance.shortDescription',
170 'cache.videoCaptions',
171 'signup.requiresEmailVerification'
172 )
173 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
174 toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
175 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
176 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
177 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
178 toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
179 174
180 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 }) 175 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
181 176
@@ -237,12 +232,16 @@ function customConfig (): CustomConfig {
237 admin: { 232 admin: {
238 email: CONFIG.ADMIN.EMAIL 233 email: CONFIG.ADMIN.EMAIL
239 }, 234 },
235 contactForm: {
236 enabled: CONFIG.CONTACT_FORM.ENABLED
237 },
240 user: { 238 user: {
241 videoQuota: CONFIG.USER.VIDEO_QUOTA, 239 videoQuota: CONFIG.USER.VIDEO_QUOTA,
242 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY 240 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
243 }, 241 },
244 transcoding: { 242 transcoding: {
245 enabled: CONFIG.TRANSCODING.ENABLED, 243 enabled: CONFIG.TRANSCODING.ENABLED,
244 allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
246 threads: CONFIG.TRANSCODING.THREADS, 245 threads: CONFIG.TRANSCODING.THREADS,
247 resolutions: { 246 resolutions: {
248 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], 247 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
@@ -250,6 +249,9 @@ function customConfig (): CustomConfig {
250 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], 249 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
251 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], 250 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
252 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] 251 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
252 },
253 hls: {
254 enabled: CONFIG.TRANSCODING.HLS.ENABLED
253 } 255 }
254 }, 256 },
255 import: { 257 import: {
@@ -264,3 +266,20 @@ function customConfig (): CustomConfig {
264 } 266 }
265 } 267 }
266} 268}
269
270function convertCustomConfigBody (body: CustomConfig) {
271 function keyConverter (k: string) {
272 // Transcoding resolutions exception
273 if (/^\d{3,4}p$/.exec(k)) return k
274
275 return snakeCase(k)
276 }
277
278 function valueConverter (v: any) {
279 if (isNumeric(v + '')) return parseInt('' + v, 10)
280
281 return v
282 }
283
284 return objectConverter(body, keyConverter, valueConverter)
285}