diff options
Diffstat (limited to 'server/controllers/api/config.ts')
-rw-r--r-- | server/controllers/api/config.ts | 77 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { omit } from 'lodash' | 2 | import { snakeCase } from 'lodash' |
3 | import { ServerConfig, UserRight } from '../../../shared' | 3 | import { ServerConfig, UserRight } from '../../../shared' |
4 | import { About } from '../../../shared/models/server/about.model' | 4 | import { About } from '../../../shared/models/server/about.model' |
5 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' | 5 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' |
@@ -10,7 +10,10 @@ import { customConfigUpdateValidator } from '../../middlewares/validators/config | |||
10 | import { ClientHtml } from '../../lib/client-html' | 10 | import { ClientHtml } from '../../lib/client-html' |
11 | import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' | 11 | import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' |
12 | import { remove, writeJSON } from 'fs-extra' | 12 | import { remove, writeJSON } from 'fs-extra' |
13 | import { getVersion } from '../../helpers/utils' | 13 | import { getServerCommit } from '../../helpers/utils' |
14 | import { Emailer } from '../../lib/emailer' | ||
15 | import { isNumeric } from 'validator' | ||
16 | import { objectConverter } from '../../helpers/core-utils' | ||
14 | 17 | ||
15 | const packageJSON = require('../../../../package.json') | 18 | const packageJSON = require('../../../../package.json') |
16 | const configRouter = express.Router() | 19 | const configRouter = express.Router() |
@@ -40,11 +43,11 @@ configRouter.delete('/custom', | |||
40 | ) | 43 | ) |
41 | 44 | ||
42 | let serverCommit: string | 45 | let serverCommit: string |
43 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | 46 | async 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 | ||
152 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | 169 | async 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 | |||
270 | function 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 | } | ||