]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/config.ts
Try to fix mock server ports
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
1 import express from 'express'
2 import { remove, writeJSON } from 'fs-extra'
3 import { snakeCase } from 'lodash'
4 import validator from 'validator'
5 import { ServerConfigManager } from '@server/lib/server-config-manager'
6 import { UserRight } from '../../../shared'
7 import { About } from '../../../shared/models/server/about.model'
8 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
9 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
10 import { objectConverter } from '../../helpers/core-utils'
11 import { CONFIG, reloadConfig } from '../../initializers/config'
12 import { ClientHtml } from '../../lib/client-html'
13 import { asyncMiddleware, authenticate, ensureUserHasRight, openapiOperationDoc } from '../../middlewares'
14 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
15
16 const configRouter = express.Router()
17
18 const auditLogger = auditLoggerFactory('config')
19
20 configRouter.get('/',
21 openapiOperationDoc({ operationId: 'getConfig' }),
22 asyncMiddleware(getConfig)
23 )
24
25 configRouter.get('/about',
26 openapiOperationDoc({ operationId: 'getAbout' }),
27 getAbout
28 )
29
30 configRouter.get('/custom',
31 openapiOperationDoc({ operationId: 'getCustomConfig' }),
32 authenticate,
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 getCustomConfig
35 )
36
37 configRouter.put('/custom',
38 openapiOperationDoc({ operationId: 'putCustomConfig' }),
39 authenticate,
40 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
41 customConfigUpdateValidator,
42 asyncMiddleware(updateCustomConfig)
43 )
44
45 configRouter.delete('/custom',
46 openapiOperationDoc({ operationId: 'delCustomConfig' }),
47 authenticate,
48 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
49 asyncMiddleware(deleteCustomConfig)
50 )
51
52 async function getConfig (req: express.Request, res: express.Response) {
53 const json = await ServerConfigManager.Instance.getServerConfig(req.ip)
54
55 return res.json(json)
56 }
57
58 function getAbout (req: express.Request, res: express.Response) {
59 const about: About = {
60 instance: {
61 name: CONFIG.INSTANCE.NAME,
62 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
63 description: CONFIG.INSTANCE.DESCRIPTION,
64 terms: CONFIG.INSTANCE.TERMS,
65 codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT,
66
67 hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION,
68
69 creationReason: CONFIG.INSTANCE.CREATION_REASON,
70 moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION,
71 administrator: CONFIG.INSTANCE.ADMINISTRATOR,
72 maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME,
73 businessModel: CONFIG.INSTANCE.BUSINESS_MODEL,
74
75 languages: CONFIG.INSTANCE.LANGUAGES,
76 categories: CONFIG.INSTANCE.CATEGORIES
77 }
78 }
79
80 return res.json(about)
81 }
82
83 function getCustomConfig (req: express.Request, res: express.Response) {
84 const data = customConfig()
85
86 return res.json(data)
87 }
88
89 async function deleteCustomConfig (req: express.Request, res: express.Response) {
90 await remove(CONFIG.CUSTOM_FILE)
91
92 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
93
94 reloadConfig()
95 ClientHtml.invalidCache()
96
97 const data = customConfig()
98
99 return res.json(data)
100 }
101
102 async function updateCustomConfig (req: express.Request, res: express.Response) {
103 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
104
105 // camelCase to snake_case key + Force number conversion
106 const toUpdateJSON = convertCustomConfigBody(req.body)
107
108 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
109
110 reloadConfig()
111 ClientHtml.invalidCache()
112
113 const data = customConfig()
114
115 auditLogger.update(
116 getAuditIdFromRes(res),
117 new CustomConfigAuditView(data),
118 oldCustomConfigAuditKeys
119 )
120
121 return res.json(data)
122 }
123
124 // ---------------------------------------------------------------------------
125
126 export {
127 configRouter
128 }
129
130 // ---------------------------------------------------------------------------
131
132 function customConfig (): CustomConfig {
133 return {
134 instance: {
135 name: CONFIG.INSTANCE.NAME,
136 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
137 description: CONFIG.INSTANCE.DESCRIPTION,
138 terms: CONFIG.INSTANCE.TERMS,
139 codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT,
140
141 creationReason: CONFIG.INSTANCE.CREATION_REASON,
142 moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION,
143 administrator: CONFIG.INSTANCE.ADMINISTRATOR,
144 maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME,
145 businessModel: CONFIG.INSTANCE.BUSINESS_MODEL,
146 hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION,
147
148 languages: CONFIG.INSTANCE.LANGUAGES,
149 categories: CONFIG.INSTANCE.CATEGORIES,
150
151 isNSFW: CONFIG.INSTANCE.IS_NSFW,
152 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
153
154 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
155
156 customizations: {
157 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
158 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
159 }
160 },
161 theme: {
162 default: CONFIG.THEME.DEFAULT
163 },
164 services: {
165 twitter: {
166 username: CONFIG.SERVICES.TWITTER.USERNAME,
167 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
168 }
169 },
170 cache: {
171 previews: {
172 size: CONFIG.CACHE.PREVIEWS.SIZE
173 },
174 captions: {
175 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
176 },
177 torrents: {
178 size: CONFIG.CACHE.TORRENTS.SIZE
179 }
180 },
181 signup: {
182 enabled: CONFIG.SIGNUP.ENABLED,
183 limit: CONFIG.SIGNUP.LIMIT,
184 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION,
185 minimumAge: CONFIG.SIGNUP.MINIMUM_AGE
186 },
187 admin: {
188 email: CONFIG.ADMIN.EMAIL
189 },
190 contactForm: {
191 enabled: CONFIG.CONTACT_FORM.ENABLED
192 },
193 user: {
194 videoQuota: CONFIG.USER.VIDEO_QUOTA,
195 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
196 },
197 transcoding: {
198 enabled: CONFIG.TRANSCODING.ENABLED,
199 allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
200 allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES,
201 threads: CONFIG.TRANSCODING.THREADS,
202 concurrency: CONFIG.TRANSCODING.CONCURRENCY,
203 profile: CONFIG.TRANSCODING.PROFILE,
204 resolutions: {
205 '0p': CONFIG.TRANSCODING.RESOLUTIONS['0p'],
206 '240p': CONFIG.TRANSCODING.RESOLUTIONS['240p'],
207 '360p': CONFIG.TRANSCODING.RESOLUTIONS['360p'],
208 '480p': CONFIG.TRANSCODING.RESOLUTIONS['480p'],
209 '720p': CONFIG.TRANSCODING.RESOLUTIONS['720p'],
210 '1080p': CONFIG.TRANSCODING.RESOLUTIONS['1080p'],
211 '1440p': CONFIG.TRANSCODING.RESOLUTIONS['1440p'],
212 '2160p': CONFIG.TRANSCODING.RESOLUTIONS['2160p']
213 },
214 webtorrent: {
215 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
216 },
217 hls: {
218 enabled: CONFIG.TRANSCODING.HLS.ENABLED
219 }
220 },
221 live: {
222 enabled: CONFIG.LIVE.ENABLED,
223 allowReplay: CONFIG.LIVE.ALLOW_REPLAY,
224 maxDuration: CONFIG.LIVE.MAX_DURATION,
225 maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES,
226 maxUserLives: CONFIG.LIVE.MAX_USER_LIVES,
227 transcoding: {
228 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
229 threads: CONFIG.LIVE.TRANSCODING.THREADS,
230 profile: CONFIG.LIVE.TRANSCODING.PROFILE,
231 resolutions: {
232 '240p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['240p'],
233 '360p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['360p'],
234 '480p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['480p'],
235 '720p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['720p'],
236 '1080p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['1080p'],
237 '1440p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['1440p'],
238 '2160p': CONFIG.LIVE.TRANSCODING.RESOLUTIONS['2160p']
239 }
240 }
241 },
242 import: {
243 videos: {
244 concurrency: CONFIG.IMPORT.VIDEOS.CONCURRENCY,
245 http: {
246 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
247 },
248 torrent: {
249 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
250 }
251 }
252 },
253 trending: {
254 videos: {
255 algorithms: {
256 enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED,
257 default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT
258 }
259 }
260 },
261 autoBlacklist: {
262 videos: {
263 ofUsers: {
264 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
265 }
266 }
267 },
268 followers: {
269 instance: {
270 enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED,
271 manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
272 }
273 },
274 followings: {
275 instance: {
276 autoFollowBack: {
277 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED
278 },
279
280 autoFollowIndex: {
281 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED,
282 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
283 }
284 }
285 },
286 broadcastMessage: {
287 enabled: CONFIG.BROADCAST_MESSAGE.ENABLED,
288 message: CONFIG.BROADCAST_MESSAGE.MESSAGE,
289 level: CONFIG.BROADCAST_MESSAGE.LEVEL,
290 dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE
291 },
292 search: {
293 remoteUri: {
294 users: CONFIG.SEARCH.REMOTE_URI.USERS,
295 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
296 },
297 searchIndex: {
298 enabled: CONFIG.SEARCH.SEARCH_INDEX.ENABLED,
299 url: CONFIG.SEARCH.SEARCH_INDEX.URL,
300 disableLocalSearch: CONFIG.SEARCH.SEARCH_INDEX.DISABLE_LOCAL_SEARCH,
301 isDefaultSearch: CONFIG.SEARCH.SEARCH_INDEX.IS_DEFAULT_SEARCH
302 }
303 }
304 }
305 }
306
307 function convertCustomConfigBody (body: CustomConfig) {
308 function keyConverter (k: string) {
309 // Transcoding resolutions exception
310 if (/^\d{3,4}p$/.exec(k)) return k
311 if (k === '0p') return k
312
313 return snakeCase(k)
314 }
315
316 function valueConverter (v: any) {
317 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
318
319 return v
320 }
321
322 return objectConverter(body, keyConverter, valueConverter)
323 }