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,
50 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
51 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
54 serverVersion: packageJSON.version,
64 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
66 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
71 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
73 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
77 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
81 videoQuota: CONFIG.USER.VIDEO_QUOTA
88 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
89 const about: About = {
91 name: CONFIG.INSTANCE.NAME,
92 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
93 description: CONFIG.INSTANCE.DESCRIPTION,
94 terms: CONFIG.INSTANCE.TERMS
98 return res.json(about).end()
101 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
102 const data = customConfig()
104 return res.json(data).end()
107 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
108 await unlinkPromise(CONFIG.CUSTOM_FILE)
112 const data = customConfig()
114 return res.json(data).end()
117 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
118 const toUpdate: CustomConfig = req.body
120 // Need to change the videoQuota key a little bit
121 const toUpdateJSON = omit(toUpdate, 'videoQuota')
122 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
123 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
124 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
125 delete toUpdate.user.videoQuota
126 delete toUpdate.instance.defaultClientRoute
127 delete toUpdate.instance.shortDescription
129 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
133 const data = customConfig()
134 return res.json(data).end()
137 // ---------------------------------------------------------------------------
143 // ---------------------------------------------------------------------------
145 function customConfig (): CustomConfig {
148 name: CONFIG.INSTANCE.NAME,
149 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
150 description: CONFIG.INSTANCE.DESCRIPTION,
151 terms: CONFIG.INSTANCE.TERMS,
152 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
154 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
155 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
160 size: CONFIG.CACHE.PREVIEWS.SIZE
164 enabled: CONFIG.SIGNUP.ENABLED,
165 limit: CONFIG.SIGNUP.LIMIT
168 email: CONFIG.ADMIN.EMAIL
171 videoQuota: CONFIG.USER.VIDEO_QUOTA
174 enabled: CONFIG.TRANSCODING.ENABLED,
175 threads: CONFIG.TRANSCODING.THREADS,
177 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
178 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
179 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
180 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
181 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]