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 { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
7 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
8 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
10 import { ClientHtml } from '../../lib/client-html'
11 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12 import { remove, writeJSON } from 'fs-extra'
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.ENABLED === 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,
64 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
72 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
75 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
82 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
84 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
89 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
91 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
95 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
101 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
103 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
107 videoQuota: CONFIG.USER.VIDEO_QUOTA,
108 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
112 return res.json(json)
115 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
116 const about: About = {
118 name: CONFIG.INSTANCE.NAME,
119 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
120 description: CONFIG.INSTANCE.DESCRIPTION,
121 terms: CONFIG.INSTANCE.TERMS
125 return res.json(about).end()
128 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const data = customConfig()
131 return res.json(data).end()
134 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
135 await remove(CONFIG.CUSTOM_FILE)
137 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
140 ClientHtml.invalidCache()
142 const data = customConfig()
144 return res.json(data).end()
147 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
148 const toUpdate: CustomConfig = req.body
149 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
151 // Force number conversion
152 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
153 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
154 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
155 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
156 toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
157 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
159 // camelCase to snake_case key
160 const toUpdateJSON = omit(
163 'instance.defaultClientRoute',
164 'instance.shortDescription',
165 'cache.videoCaptions',
166 'signup.requiresEmailVerification'
168 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
169 toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
170 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
171 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
172 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
173 toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
175 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
178 ClientHtml.invalidCache()
180 const data = customConfig()
183 getAuditIdFromRes(res),
184 new CustomConfigAuditView(data),
185 oldCustomConfigAuditKeys
188 return res.json(data).end()
191 // ---------------------------------------------------------------------------
197 // ---------------------------------------------------------------------------
199 function customConfig (): CustomConfig {
202 name: CONFIG.INSTANCE.NAME,
203 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
204 description: CONFIG.INSTANCE.DESCRIPTION,
205 terms: CONFIG.INSTANCE.TERMS,
206 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
207 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
209 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
210 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
215 username: CONFIG.SERVICES.TWITTER.USERNAME,
216 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
221 size: CONFIG.CACHE.PREVIEWS.SIZE
224 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
228 enabled: CONFIG.SIGNUP.ENABLED,
229 limit: CONFIG.SIGNUP.LIMIT,
230 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
233 email: CONFIG.ADMIN.EMAIL
236 videoQuota: CONFIG.USER.VIDEO_QUOTA,
237 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
240 enabled: CONFIG.TRANSCODING.ENABLED,
241 threads: CONFIG.TRANSCODING.THREADS,
243 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
244 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
245 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
246 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
247 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
253 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
256 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED