]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/config.ts
Add new name/terms/description config options
[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 instance: {
109 name: CONFIG.INSTANCE.NAME,
110 description: CONFIG.INSTANCE.DESCRIPTION,
111 terms: CONFIG.INSTANCE.TERMS
112 },
113 cache: {
114 previews: {
115 size: CONFIG.CACHE.PREVIEWS.SIZE
116 }
117 },
118 signup: {
119 enabled: CONFIG.SIGNUP.ENABLED,
120 limit: CONFIG.SIGNUP.LIMIT
121 },
122 admin: {
123 email: CONFIG.ADMIN.EMAIL
124 },
125 user: {
126 videoQuota: CONFIG.USER.VIDEO_QUOTA
127 },
128 transcoding: {
129 enabled: CONFIG.TRANSCODING.ENABLED,
130 threads: CONFIG.TRANSCODING.THREADS,
131 resolutions: {
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' ]
137 }
138 }
139 }
140 }