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