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 configRouter = express.Router()
14 asyncMiddleware(getConfig)
16 configRouter.get('/custom',
18 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
19 asyncMiddleware(getCustomConfig)
21 configRouter.put('/custom',
23 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
24 asyncMiddleware(customConfigUpdateValidator),
25 asyncMiddleware(updateCustomConfig)
27 configRouter.delete('/custom',
29 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
30 asyncMiddleware(deleteCustomConfig)
33 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
34 const allowed = await isSignupAllowed()
36 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
37 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
38 .map(r => parseInt(r, 10))
40 const json: ServerConfig = {
50 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
52 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
57 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
65 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
66 const data = customConfig()
68 return res.json(data).end()
71 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
72 await unlinkPromise(CONFIG.CUSTOM_FILE)
76 const data = customConfig()
78 return res.json(data).end()
81 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
82 const toUpdate: CustomConfig = req.body
84 // Need to change the videoQuota key a little bit
85 const toUpdateJSON = omit(toUpdate, 'videoQuota')
86 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
88 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
92 const data = customConfig()
93 return res.json(data).end()
96 // ---------------------------------------------------------------------------
102 // ---------------------------------------------------------------------------
104 function customConfig (): CustomConfig {
108 size: CONFIG.CACHE.PREVIEWS.SIZE
112 enabled: CONFIG.SIGNUP.ENABLED,
113 limit: CONFIG.SIGNUP.LIMIT
116 email: CONFIG.ADMIN.EMAIL
119 videoQuota: CONFIG.USER.VIDEO_QUOTA
122 enabled: CONFIG.TRANSCODING.ENABLED,
123 threads: CONFIG.TRANSCODING.THREADS,
125 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
126 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
127 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
128 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
129 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]