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