]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Update g++ needed version (#338)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
36f9424f 2import { omit } from 'lodash'
fd206f0b 3import { ServerConfig, UserRight } from '../../../shared'
09cababd
C
4import { About } from '../../../shared/models/server/about.model'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
fd206f0b 6import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
da854ddd 7import { isSignupAllowed } from '../../helpers/utils'
fd206f0b
C
8import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10import { customConfigUpdateValidator } from '../../middlewares/validators/config'
65fcc311 11
915c5bbe 12const packageJSON = require('../../../../package.json')
65fcc311
C
13const configRouter = express.Router()
14
36f9424f 15configRouter.get('/about', getAbout)
eb080476
C
16configRouter.get('/',
17 asyncMiddleware(getConfig)
18)
36f9424f 19
fd206f0b
C
20configRouter.get('/custom',
21 authenticate,
22 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
23 asyncMiddleware(getCustomConfig)
24)
25configRouter.put('/custom',
26 authenticate,
27 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
28 asyncMiddleware(customConfigUpdateValidator),
29 asyncMiddleware(updateCustomConfig)
30)
31configRouter.delete('/custom',
32 authenticate,
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 asyncMiddleware(deleteCustomConfig)
35)
65fcc311 36
eb080476
C
37async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const allowed = await isSignupAllowed()
291e8d3e 39
eb080476
C
40 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
41 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
42 .map(r => parseInt(r, 10))
6a84aafd 43
eb080476 44 const json: ServerConfig = {
36f9424f 45 instance: {
00b5556c 46 name: CONFIG.INSTANCE.NAME,
901637bb 47 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
00b5556c
C
48 customizations: {
49 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
50 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
51 }
36f9424f 52 },
915c5bbe 53 serverVersion: packageJSON.version,
eb080476
C
54 signup: {
55 allowed
56 },
57 transcoding: {
58 enabledResolutions
01de67b9
C
59 },
60 avatar: {
61 file: {
62 size: {
63 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
64 },
65 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
66 }
67 },
68 video: {
6de36768
C
69 image: {
70 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
71 size: {
72 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
73 }
74 },
01de67b9
C
75 file: {
76 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
77 }
65fcc311 78 }
eb080476 79 }
6a84aafd 80
eb080476 81 return res.json(json)
65fcc311
C
82}
83
36f9424f
C
84function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
85 const about: About = {
86 instance: {
87 name: CONFIG.INSTANCE.NAME,
88 description: CONFIG.INSTANCE.DESCRIPTION,
89 terms: CONFIG.INSTANCE.TERMS
90 }
91 }
92
93 return res.json(about).end()
94}
95
fd206f0b
C
96async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
97 const data = customConfig()
98
99 return res.json(data).end()
100}
101
102async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
103 await unlinkPromise(CONFIG.CUSTOM_FILE)
104
105 reloadConfig()
106
107 const data = customConfig()
108
109 return res.json(data).end()
110}
111
112async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
113 const toUpdate: CustomConfig = req.body
114
115 // Need to change the videoQuota key a little bit
116 const toUpdateJSON = omit(toUpdate, 'videoQuota')
117 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
901637bb 118 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
509cd56a 119 delete toUpdate.user.videoQuota
901637bb 120 delete toUpdate.instance.defaultClientRoute
fd206f0b 121
2ad42952 122 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
fd206f0b
C
123
124 reloadConfig()
125
126 const data = customConfig()
127 return res.json(data).end()
128}
129
65fcc311
C
130// ---------------------------------------------------------------------------
131
132export {
133 configRouter
134}
fd206f0b
C
135
136// ---------------------------------------------------------------------------
137
138function customConfig (): CustomConfig {
139 return {
66b16caf
C
140 instance: {
141 name: CONFIG.INSTANCE.NAME,
142 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c 143 terms: CONFIG.INSTANCE.TERMS,
901637bb 144 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
00b5556c
C
145 customizations: {
146 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
147 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
148 }
66b16caf 149 },
fd206f0b
C
150 cache: {
151 previews: {
152 size: CONFIG.CACHE.PREVIEWS.SIZE
153 }
154 },
155 signup: {
156 enabled: CONFIG.SIGNUP.ENABLED,
157 limit: CONFIG.SIGNUP.LIMIT
158 },
159 admin: {
160 email: CONFIG.ADMIN.EMAIL
161 },
162 user: {
163 videoQuota: CONFIG.USER.VIDEO_QUOTA
164 },
165 transcoding: {
166 enabled: CONFIG.TRANSCODING.ENABLED,
167 threads: CONFIG.TRANSCODING.THREADS,
168 resolutions: {
169 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
170 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
171 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
172 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
173 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
174 }
175 }
176 }
177}