1 import * as express from 'express'
2 import { snakeCase } from 'lodash'
3 import { ServerConfig, ServerConfigPlugin, UserRight } from '../../../shared'
4 import { About } from '../../../shared/models/server/about.model'
5 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
7 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
8 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
10 import { ClientHtml } from '../../lib/client-html'
11 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12 import { remove, writeJSON } from 'fs-extra'
13 import { getServerCommit } from '../../helpers/utils'
14 import { Emailer } from '../../lib/emailer'
15 import { isNumeric } from 'validator'
16 import { objectConverter } from '../../helpers/core-utils'
17 import { CONFIG, reloadConfig } from '../../initializers/config'
18 import { PluginManager } from '../../lib/plugins/plugin-manager'
19 import { PluginType } from '../../../shared/models/plugins/plugin.type'
21 const packageJSON = require('../../../../package.json')
22 const configRouter = express.Router()
24 const auditLogger = auditLoggerFactory('config')
26 configRouter.get('/about', getAbout)
28 asyncMiddleware(getConfig)
31 configRouter.get('/custom',
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 asyncMiddleware(getCustomConfig)
36 configRouter.put('/custom',
38 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
39 asyncMiddleware(customConfigUpdateValidator),
40 asyncMiddleware(updateCustomConfig)
42 configRouter.delete('/custom',
44 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
45 asyncMiddleware(deleteCustomConfig)
48 let serverCommit: string
49 async function getConfig (req: express.Request, res: express.Response) {
50 const allowed = await isSignupAllowed()
51 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
53 if (serverCommit === undefined) serverCommit = await getServerCommit()
55 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
56 .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
57 .map(r => parseInt(r, 10))
59 const plugins: ServerConfigPlugin[] = []
60 const registeredPlugins = PluginManager.Instance.getRegisteredPlugins()
61 for (const pluginName of Object.keys(registeredPlugins)) {
62 const plugin = registeredPlugins[ pluginName ]
63 if (plugin.type !== PluginType.PLUGIN) continue
67 version: plugin.version,
68 description: plugin.description,
69 clientScripts: plugin.clientScripts
73 const json: ServerConfig = {
75 name: CONFIG.INSTANCE.NAME,
76 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
77 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
78 isNSFW: CONFIG.INSTANCE.IS_NSFW,
79 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
81 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
82 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
87 enabled: Emailer.isEnabled()
90 enabled: CONFIG.CONTACT_FORM.ENABLED
92 serverVersion: packageJSON.version,
97 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
101 enabled: CONFIG.TRANSCODING.HLS.ENABLED
108 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
111 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
118 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
125 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
127 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
132 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
134 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
138 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
144 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
146 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
150 videoQuota: CONFIG.USER.VIDEO_QUOTA,
151 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
155 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
159 enabled: CONFIG.TRACKER.ENABLED
163 return res.json(json)
166 function getAbout (req: express.Request, res: express.Response) {
167 const about: About = {
169 name: CONFIG.INSTANCE.NAME,
170 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
171 description: CONFIG.INSTANCE.DESCRIPTION,
172 terms: CONFIG.INSTANCE.TERMS
176 return res.json(about).end()
179 async function getCustomConfig (req: express.Request, res: express.Response) {
180 const data = customConfig()
182 return res.json(data).end()
185 async function deleteCustomConfig (req: express.Request, res: express.Response) {
186 await remove(CONFIG.CUSTOM_FILE)
188 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
191 ClientHtml.invalidCache()
193 const data = customConfig()
195 return res.json(data).end()
198 async function updateCustomConfig (req: express.Request, res: express.Response) {
199 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
201 // camelCase to snake_case key + Force number conversion
202 const toUpdateJSON = convertCustomConfigBody(req.body)
204 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
207 ClientHtml.invalidCache()
209 const data = customConfig()
212 getAuditIdFromRes(res),
213 new CustomConfigAuditView(data),
214 oldCustomConfigAuditKeys
217 return res.json(data).end()
220 // ---------------------------------------------------------------------------
226 // ---------------------------------------------------------------------------
228 function customConfig (): CustomConfig {
231 name: CONFIG.INSTANCE.NAME,
232 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
233 description: CONFIG.INSTANCE.DESCRIPTION,
234 terms: CONFIG.INSTANCE.TERMS,
235 isNSFW: CONFIG.INSTANCE.IS_NSFW,
236 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
237 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
239 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
240 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
245 username: CONFIG.SERVICES.TWITTER.USERNAME,
246 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
251 size: CONFIG.CACHE.PREVIEWS.SIZE
254 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
258 enabled: CONFIG.SIGNUP.ENABLED,
259 limit: CONFIG.SIGNUP.LIMIT,
260 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
263 email: CONFIG.ADMIN.EMAIL
266 enabled: CONFIG.CONTACT_FORM.ENABLED
269 videoQuota: CONFIG.USER.VIDEO_QUOTA,
270 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
273 enabled: CONFIG.TRANSCODING.ENABLED,
274 allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
275 allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES,
276 threads: CONFIG.TRANSCODING.THREADS,
278 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
279 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
280 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
281 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
282 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ],
283 '2160p': CONFIG.TRANSCODING.RESOLUTIONS[ '2160p' ]
286 enabled: CONFIG.TRANSCODING.HLS.ENABLED
292 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
295 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
302 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
308 enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED,
309 manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
315 function convertCustomConfigBody (body: CustomConfig) {
316 function keyConverter (k: string) {
317 // Transcoding resolutions exception
318 if (/^\d{3,4}p$/.exec(k)) return k
323 function valueConverter (v: any) {
324 if (isNumeric(v + '')) return parseInt('' + v, 10)
329 return objectConverter(body, keyConverter, valueConverter)