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'
16 const configRouter = express.Router()
18 const auditLogger = auditLoggerFactory('config')
21 openapiOperationDoc({ operationId: 'getConfig' }),
22 asyncMiddleware(getConfig)
25 configRouter.get('/about',
26 openapiOperationDoc({ operationId: 'getAbout' }),
30 configRouter.get('/custom',
31 openapiOperationDoc({ operationId: 'getCustomConfig' }),
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
37 configRouter.put('/custom',
38 openapiOperationDoc({ operationId: 'putCustomConfig' }),
40 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
41 customConfigUpdateValidator,
42 asyncMiddleware(updateCustomConfig)
45 configRouter.delete('/custom',
46 openapiOperationDoc({ operationId: 'delCustomConfig' }),
48 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
49 asyncMiddleware(deleteCustomConfig)
52 async function getConfig (req: express.Request, res: express.Response) {
53 const json = await ServerConfigManager.Instance.getServerConfig(req.ip)
58 function getAbout (req: express.Request, res: express.Response) {
59 const about: About = {
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,
67 hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION,
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,
75 languages: CONFIG.INSTANCE.LANGUAGES,
76 categories: CONFIG.INSTANCE.CATEGORIES
80 return res.json(about)
83 function getCustomConfig (req: express.Request, res: express.Response) {
84 const data = customConfig()
89 async function deleteCustomConfig (req: express.Request, res: express.Response) {
90 await remove(CONFIG.CUSTOM_FILE)
92 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
95 ClientHtml.invalidCache()
97 const data = customConfig()
102 async function updateCustomConfig (req: express.Request, res: express.Response) {
103 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
105 // camelCase to snake_case key + Force number conversion
106 const toUpdateJSON = convertCustomConfigBody(req.body)
108 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
111 ClientHtml.invalidCache()
113 const data = customConfig()
116 getAuditIdFromRes(res),
117 new CustomConfigAuditView(data),
118 oldCustomConfigAuditKeys
121 return res.json(data)
124 // ---------------------------------------------------------------------------
130 // ---------------------------------------------------------------------------
132 function customConfig (): CustomConfig {
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,
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,
148 languages: CONFIG.INSTANCE.LANGUAGES,
149 categories: CONFIG.INSTANCE.CATEGORIES,
151 isNSFW: CONFIG.INSTANCE.IS_NSFW,
152 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
154 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
157 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
158 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
162 default: CONFIG.THEME.DEFAULT
166 username: CONFIG.SERVICES.TWITTER.USERNAME,
167 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
172 size: CONFIG.CACHE.PREVIEWS.SIZE
175 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
178 size: CONFIG.CACHE.TORRENTS.SIZE
182 enabled: CONFIG.SIGNUP.ENABLED,
183 limit: CONFIG.SIGNUP.LIMIT,
184 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION,
185 minimumAge: CONFIG.SIGNUP.MINIMUM_AGE
188 email: CONFIG.ADMIN.EMAIL
191 enabled: CONFIG.CONTACT_FORM.ENABLED
194 videoQuota: CONFIG.USER.VIDEO_QUOTA,
195 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
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,
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']
215 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
218 enabled: CONFIG.TRANSCODING.HLS.ENABLED
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,
228 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
229 threads: CONFIG.LIVE.TRANSCODING.THREADS,
230 profile: CONFIG.LIVE.TRANSCODING.PROFILE,
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']
244 concurrency: CONFIG.IMPORT.VIDEOS.CONCURRENCY,
246 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
249 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
256 enabled: CONFIG.TRENDING.VIDEOS.ALGORITHMS.ENABLED,
257 default: CONFIG.TRENDING.VIDEOS.ALGORITHMS.DEFAULT
264 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
270 enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED,
271 manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
277 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED
281 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED,
282 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
287 enabled: CONFIG.BROADCAST_MESSAGE.ENABLED,
288 message: CONFIG.BROADCAST_MESSAGE.MESSAGE,
289 level: CONFIG.BROADCAST_MESSAGE.LEVEL,
290 dismissable: CONFIG.BROADCAST_MESSAGE.DISMISSABLE
294 users: CONFIG.SEARCH.REMOTE_URI.USERS,
295 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
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
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
316 function valueConverter (v: any) {
317 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
322 return objectConverter(body, keyConverter, valueConverter)