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 } 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()
40 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
41 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
42 .map(r => parseInt(r, 10))
44 const json: ServerConfig = {
46 name: CONFIG.INSTANCE.NAME,
47 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
48 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
49 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
51 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
52 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
55 serverVersion: packageJSON.version,
65 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
67 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
72 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
74 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
78 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
82 videoQuota: CONFIG.USER.VIDEO_QUOTA
89 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
90 const about: About = {
92 name: CONFIG.INSTANCE.NAME,
93 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
94 description: CONFIG.INSTANCE.DESCRIPTION,
95 terms: CONFIG.INSTANCE.TERMS
99 return res.json(about).end()
102 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
103 const data = customConfig()
105 return res.json(data).end()
108 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
109 await unlinkPromise(CONFIG.CUSTOM_FILE)
113 const data = customConfig()
115 return res.json(data).end()
118 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
119 const toUpdate: CustomConfig = req.body
121 // Force number conversion
122 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
123 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
124 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
125 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
127 // camelCase to snake_case key
128 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription')
129 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
130 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
131 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
132 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
134 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
138 const data = customConfig()
139 return res.json(data).end()
142 // ---------------------------------------------------------------------------
148 // ---------------------------------------------------------------------------
150 function customConfig (): CustomConfig {
153 name: CONFIG.INSTANCE.NAME,
154 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
155 description: CONFIG.INSTANCE.DESCRIPTION,
156 terms: CONFIG.INSTANCE.TERMS,
157 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
158 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
160 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
161 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
166 size: CONFIG.CACHE.PREVIEWS.SIZE
170 enabled: CONFIG.SIGNUP.ENABLED,
171 limit: CONFIG.SIGNUP.LIMIT
174 email: CONFIG.ADMIN.EMAIL
177 videoQuota: CONFIG.USER.VIDEO_QUOTA
180 enabled: CONFIG.TRANSCODING.ENABLED,
181 threads: CONFIG.TRANSCODING.THREADS,
183 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
184 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
185 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
186 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
187 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]