1 import * as express from 'express'
2 import { ServerConfig, UserRight } from '../../../shared'
3 import { CustomConfig } from '../../../shared/models/config/custom-config.model'
4 import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
5 import { isSignupAllowed } from '../../helpers/utils'
6 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
7 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
8 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
9 import { omit } from 'lodash'
11 const packageJSON = require('../../../../package.json')
12 const configRouter = express.Router()
15 asyncMiddleware(getConfig)
17 configRouter.get('/custom',
19 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
20 asyncMiddleware(getCustomConfig)
22 configRouter.put('/custom',
24 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
25 asyncMiddleware(customConfigUpdateValidator),
26 asyncMiddleware(updateCustomConfig)
28 configRouter.delete('/custom',
30 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
31 asyncMiddleware(deleteCustomConfig)
34 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
35 const allowed = await isSignupAllowed()
37 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
38 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
39 .map(r => parseInt(r, 10))
41 const json: ServerConfig = {
42 serverVersion: packageJSON.version,
52 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
54 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
59 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
67 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
68 const data = customConfig()
70 return res.json(data).end()
73 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
74 await unlinkPromise(CONFIG.CUSTOM_FILE)
78 const data = customConfig()
80 return res.json(data).end()
83 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const toUpdate: CustomConfig = req.body
86 // Need to change the videoQuota key a little bit
87 const toUpdateJSON = omit(toUpdate, 'videoQuota')
88 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
90 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
94 const data = customConfig()
95 return res.json(data).end()
98 // ---------------------------------------------------------------------------
104 // ---------------------------------------------------------------------------
106 function customConfig (): CustomConfig {
109 name: CONFIG.INSTANCE.NAME,
110 description: CONFIG.INSTANCE.DESCRIPTION,
111 terms: CONFIG.INSTANCE.TERMS
115 size: CONFIG.CACHE.PREVIEWS.SIZE
119 enabled: CONFIG.SIGNUP.ENABLED,
120 limit: CONFIG.SIGNUP.LIMIT
123 email: CONFIG.ADMIN.EMAIL
126 videoQuota: CONFIG.USER.VIDEO_QUOTA
129 enabled: CONFIG.TRANSCODING.ENABLED,
130 threads: CONFIG.TRANSCODING.THREADS,
132 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
133 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
134 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
135 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
136 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]