1 import * as express from 'express'
2 import { omit } from 'lodash'
3 import { ServerConfig, UserRight } from '../../../shared'
4 import { About } from '../../../shared/models/config/about.model'
5 import { CustomConfig } from '../../../shared/models/config/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,
48 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
49 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
52 serverVersion: packageJSON.version,
62 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
64 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
69 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
71 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
75 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
83 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const about: About = {
86 name: CONFIG.INSTANCE.NAME,
87 description: CONFIG.INSTANCE.DESCRIPTION,
88 terms: CONFIG.INSTANCE.TERMS
92 return res.json(about).end()
95 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
96 const data = customConfig()
98 return res.json(data).end()
101 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
102 await unlinkPromise(CONFIG.CUSTOM_FILE)
106 const data = customConfig()
108 return res.json(data).end()
111 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
112 const toUpdate: CustomConfig = req.body
114 // Need to change the videoQuota key a little bit
115 const toUpdateJSON = omit(toUpdate, 'videoQuota')
116 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
118 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
122 const data = customConfig()
123 return res.json(data).end()
126 // ---------------------------------------------------------------------------
132 // ---------------------------------------------------------------------------
134 function customConfig (): CustomConfig {
137 name: CONFIG.INSTANCE.NAME,
138 description: CONFIG.INSTANCE.DESCRIPTION,
139 terms: CONFIG.INSTANCE.TERMS,
141 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
142 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
147 size: CONFIG.CACHE.PREVIEWS.SIZE
151 enabled: CONFIG.SIGNUP.ENABLED,
152 limit: CONFIG.SIGNUP.LIMIT
155 email: CONFIG.ADMIN.EMAIL
158 videoQuota: CONFIG.USER.VIDEO_QUOTA
161 enabled: CONFIG.TRANSCODING.ENABLED,
162 threads: CONFIG.TRANSCODING.THREADS,
164 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
165 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
166 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
167 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
168 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]