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 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
73 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
78 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
80 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
84 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
90 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
92 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
96 videoQuota: CONFIG.USER.VIDEO_QUOTA
100 return res.json(json)
103 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
104 const about: About = {
106 name: CONFIG.INSTANCE.NAME,
107 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
108 description: CONFIG.INSTANCE.DESCRIPTION,
109 terms: CONFIG.INSTANCE.TERMS
113 return res.json(about).end()
116 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const data = customConfig()
119 return res.json(data).end()
122 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
123 await unlinkPromise(CONFIG.CUSTOM_FILE)
126 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
127 new CustomConfigAuditView(customConfig())
131 ClientHtml.invalidCache()
133 const data = customConfig()
135 return res.json(data).end()
138 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
139 const toUpdate: CustomConfig = req.body
140 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
142 // Force number conversion
143 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
144 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
145 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
146 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
147 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
149 // camelCase to snake_case key
150 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
151 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
152 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
153 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
154 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
156 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
159 ClientHtml.invalidCache()
161 const data = customConfig()
164 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
165 new CustomConfigAuditView(data),
166 oldCustomConfigAuditKeys
169 return res.json(data).end()
172 // ---------------------------------------------------------------------------
178 // ---------------------------------------------------------------------------
180 function customConfig (): CustomConfig {
183 name: CONFIG.INSTANCE.NAME,
184 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
185 description: CONFIG.INSTANCE.DESCRIPTION,
186 terms: CONFIG.INSTANCE.TERMS,
187 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
188 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
190 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
191 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
196 username: CONFIG.SERVICES.TWITTER.USERNAME,
197 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
202 size: CONFIG.CACHE.PREVIEWS.SIZE
205 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
209 enabled: CONFIG.SIGNUP.ENABLED,
210 limit: CONFIG.SIGNUP.LIMIT
213 email: CONFIG.ADMIN.EMAIL
216 videoQuota: CONFIG.USER.VIDEO_QUOTA
219 enabled: CONFIG.TRANSCODING.ENABLED,
220 threads: CONFIG.TRANSCODING.THREADS,
222 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
223 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
224 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
225 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
226 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]