Add ability to update some configuration keys
[oweals/peertube.git] / server / controllers / api / config.ts
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'
10
11 const configRouter = express.Router()
12
13 configRouter.get('/',
14   asyncMiddleware(getConfig)
15 )
16 configRouter.get('/custom',
17   authenticate,
18   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
19   asyncMiddleware(getCustomConfig)
20 )
21 configRouter.put('/custom',
22   authenticate,
23   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
24   asyncMiddleware(customConfigUpdateValidator),
25   asyncMiddleware(updateCustomConfig)
26 )
27 configRouter.delete('/custom',
28   authenticate,
29   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
30   asyncMiddleware(deleteCustomConfig)
31 )
32
33 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
34   const allowed = await isSignupAllowed()
35
36   const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
37    .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
38    .map(r => parseInt(r, 10))
39
40   const json: ServerConfig = {
41     signup: {
42       allowed
43     },
44     transcoding: {
45       enabledResolutions
46     },
47     avatar: {
48       file: {
49         size: {
50           max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
51         },
52         extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
53       }
54     },
55     video: {
56       file: {
57         extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
58       }
59     }
60   }
61
62   return res.json(json)
63 }
64
65 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
66   const data = customConfig()
67
68   return res.json(data).end()
69 }
70
71 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
72   await unlinkPromise(CONFIG.CUSTOM_FILE)
73
74   reloadConfig()
75
76   const data = customConfig()
77
78   return res.json(data).end()
79 }
80
81 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
82   const toUpdate: CustomConfig = req.body
83
84   // Need to change the videoQuota key a little bit
85   const toUpdateJSON = omit(toUpdate, 'videoQuota')
86   toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
87
88   await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
89
90   reloadConfig()
91
92   const data = customConfig()
93   return res.json(data).end()
94 }
95
96 // ---------------------------------------------------------------------------
97
98 export {
99   configRouter
100 }
101
102 // ---------------------------------------------------------------------------
103
104 function customConfig (): CustomConfig {
105   return {
106     cache: {
107       previews: {
108         size: CONFIG.CACHE.PREVIEWS.SIZE
109       }
110     },
111     signup: {
112       enabled: CONFIG.SIGNUP.ENABLED,
113       limit: CONFIG.SIGNUP.LIMIT
114     },
115     admin: {
116       email: CONFIG.ADMIN.EMAIL
117     },
118     user: {
119       videoQuota: CONFIG.USER.VIDEO_QUOTA
120     },
121     transcoding: {
122       enabled: CONFIG.TRANSCODING.ENABLED,
123       threads: CONFIG.TRANSCODING.THREADS,
124       resolutions: {
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' ]
130       }
131     }
132   }
133 }