1 import * as express from 'express'
2 import { omit } from 'lodash'
3 import { ServerConfig, UserRight } from '../../../shared'
4 import { About } from '../../../shared/models/server/about.model'
5 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6 import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
7 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils'
8 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
12 const packageJSON = require('../../../../package.json')
13 const configRouter = express.Router()
15 configRouter.get('/about', getAbout)
17 asyncMiddleware(getConfig)
20 configRouter.get('/custom',
22 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
23 asyncMiddleware(getCustomConfig)
25 configRouter.put('/custom',
27 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
28 asyncMiddleware(customConfigUpdateValidator),
29 asyncMiddleware(updateCustomConfig)
31 configRouter.delete('/custom',
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 asyncMiddleware(deleteCustomConfig)
37 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const allowed = await isSignupAllowed()
39 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
41 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
42 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
43 .map(r => parseInt(r, 10))
45 const json: ServerConfig = {
47 name: CONFIG.INSTANCE.NAME,
48 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
49 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
50 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
52 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
53 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
56 serverVersion: packageJSON.version,
67 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
69 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
74 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
76 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
80 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
84 videoQuota: CONFIG.USER.VIDEO_QUOTA
91 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
92 const about: About = {
94 name: CONFIG.INSTANCE.NAME,
95 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
96 description: CONFIG.INSTANCE.DESCRIPTION,
97 terms: CONFIG.INSTANCE.TERMS
101 return res.json(about).end()
104 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
105 const data = customConfig()
107 return res.json(data).end()
110 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
111 await unlinkPromise(CONFIG.CUSTOM_FILE)
115 const data = customConfig()
117 return res.json(data).end()
120 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
121 const toUpdate: CustomConfig = req.body
123 // Force number conversion
124 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
125 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
126 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
127 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
129 // camelCase to snake_case key
130 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription')
131 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
132 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
133 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
134 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
136 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
140 const data = customConfig()
141 return res.json(data).end()
144 // ---------------------------------------------------------------------------
150 // ---------------------------------------------------------------------------
152 function customConfig (): CustomConfig {
155 name: CONFIG.INSTANCE.NAME,
156 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
157 description: CONFIG.INSTANCE.DESCRIPTION,
158 terms: CONFIG.INSTANCE.TERMS,
159 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
160 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
162 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
163 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
168 username: CONFIG.SERVICES.TWITTER.USERNAME,
169 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
174 size: CONFIG.CACHE.PREVIEWS.SIZE
178 enabled: CONFIG.SIGNUP.ENABLED,
179 limit: CONFIG.SIGNUP.LIMIT
182 email: CONFIG.ADMIN.EMAIL
185 videoQuota: CONFIG.USER.VIDEO_QUOTA
188 enabled: CONFIG.TRANSCODING.ENABLED,
189 threads: CONFIG.TRANSCODING.THREADS,
191 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
192 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
193 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
194 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
195 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]