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, isSignupAllowedForCurrentIP } 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 import { ClientHtml } from '../../lib/client-html'
12 import { CustomConfigAuditView, auditLoggerFactory } from '../../helpers/audit-logger'
14 const packageJSON = require('../../../../package.json')
15 const configRouter = express.Router()
17 const auditLogger = auditLoggerFactory('config')
19 configRouter.get('/about', getAbout)
21 asyncMiddleware(getConfig)
24 configRouter.get('/custom',
26 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
27 asyncMiddleware(getCustomConfig)
29 configRouter.put('/custom',
31 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
32 asyncMiddleware(customConfigUpdateValidator),
33 asyncMiddleware(updateCustomConfig)
35 configRouter.delete('/custom',
37 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38 asyncMiddleware(deleteCustomConfig)
41 async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
42 const allowed = await isSignupAllowed()
43 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
45 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
46 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
47 .map(r => parseInt(r, 10))
49 const json: ServerConfig = {
51 name: CONFIG.INSTANCE.NAME,
52 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
53 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
54 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
56 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
57 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
60 serverVersion: packageJSON.version,
71 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
78 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
80 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
85 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
87 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
91 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
97 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
99 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
103 videoQuota: CONFIG.USER.VIDEO_QUOTA
107 return res.json(json)
110 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
111 const about: About = {
113 name: CONFIG.INSTANCE.NAME,
114 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
115 description: CONFIG.INSTANCE.DESCRIPTION,
116 terms: CONFIG.INSTANCE.TERMS
120 return res.json(about).end()
123 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
124 const data = customConfig()
126 return res.json(data).end()
129 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
130 await unlinkPromise(CONFIG.CUSTOM_FILE)
133 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
134 new CustomConfigAuditView(customConfig())
138 ClientHtml.invalidCache()
140 const data = customConfig()
142 return res.json(data).end()
145 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
146 const toUpdate: CustomConfig = req.body
147 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
149 // Force number conversion
150 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
151 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
152 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
153 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
154 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
156 // camelCase to snake_case key
157 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
158 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
159 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
160 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
161 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
163 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
166 ClientHtml.invalidCache()
168 const data = customConfig()
171 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
172 new CustomConfigAuditView(data),
173 oldCustomConfigAuditKeys
176 return res.json(data).end()
179 // ---------------------------------------------------------------------------
185 // ---------------------------------------------------------------------------
187 function customConfig (): CustomConfig {
190 name: CONFIG.INSTANCE.NAME,
191 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
192 description: CONFIG.INSTANCE.DESCRIPTION,
193 terms: CONFIG.INSTANCE.TERMS,
194 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
195 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
197 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
198 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
203 username: CONFIG.SERVICES.TWITTER.USERNAME,
204 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
209 size: CONFIG.CACHE.PREVIEWS.SIZE
212 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
216 enabled: CONFIG.SIGNUP.ENABLED,
217 limit: CONFIG.SIGNUP.LIMIT
220 email: CONFIG.ADMIN.EMAIL
223 videoQuota: CONFIG.USER.VIDEO_QUOTA
226 enabled: CONFIG.TRANSCODING.ENABLED,
227 threads: CONFIG.TRANSCODING.THREADS,
229 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
230 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
231 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
232 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
233 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
239 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED