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 { auditLoggerFactory, CustomConfigAuditView } 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
74 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
81 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
83 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
88 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
90 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
94 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
100 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
102 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
106 videoQuota: CONFIG.USER.VIDEO_QUOTA
110 return res.json(json)
113 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
114 const about: About = {
116 name: CONFIG.INSTANCE.NAME,
117 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
118 description: CONFIG.INSTANCE.DESCRIPTION,
119 terms: CONFIG.INSTANCE.TERMS
123 return res.json(about).end()
126 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
127 const data = customConfig()
129 return res.json(data).end()
132 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
133 await unlinkPromise(CONFIG.CUSTOM_FILE)
136 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
137 new CustomConfigAuditView(customConfig())
141 ClientHtml.invalidCache()
143 const data = customConfig()
145 return res.json(data).end()
148 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
149 const toUpdate: CustomConfig = req.body
150 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
152 // Force number conversion
153 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
154 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
155 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
156 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
157 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
159 // camelCase to snake_case key
160 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
161 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
162 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
163 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
164 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
166 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
169 ClientHtml.invalidCache()
171 const data = customConfig()
174 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
175 new CustomConfigAuditView(data),
176 oldCustomConfigAuditKeys
179 return res.json(data).end()
182 // ---------------------------------------------------------------------------
188 // ---------------------------------------------------------------------------
190 function customConfig (): CustomConfig {
193 name: CONFIG.INSTANCE.NAME,
194 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
195 description: CONFIG.INSTANCE.DESCRIPTION,
196 terms: CONFIG.INSTANCE.TERMS,
197 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
198 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
200 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
201 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
206 username: CONFIG.SERVICES.TWITTER.USERNAME,
207 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
212 size: CONFIG.CACHE.PREVIEWS.SIZE
215 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
219 enabled: CONFIG.SIGNUP.ENABLED,
220 limit: CONFIG.SIGNUP.LIMIT
223 email: CONFIG.ADMIN.EMAIL
226 videoQuota: CONFIG.USER.VIDEO_QUOTA
229 enabled: CONFIG.TRANSCODING.ENABLED,
230 threads: CONFIG.TRANSCODING.THREADS,
232 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
233 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
234 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
235 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
236 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
242 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
245 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED