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'
13 import { getServerCommit } from '../../helpers/utils'
14 import { Emailer } from '../../lib/emailer'
16 const packageJSON = require('../../../../package.json')
17 const configRouter = express.Router()
19 const auditLogger = auditLoggerFactory('config')
21 configRouter.get('/about', getAbout)
23 asyncMiddleware(getConfig)
26 configRouter.get('/custom',
28 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
29 asyncMiddleware(getCustomConfig)
31 configRouter.put('/custom',
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 asyncMiddleware(customConfigUpdateValidator),
35 asyncMiddleware(updateCustomConfig)
37 configRouter.delete('/custom',
39 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
40 asyncMiddleware(deleteCustomConfig)
43 let serverCommit: string
44 async function getConfig (req: express.Request, res: express.Response) {
45 const allowed = await isSignupAllowed()
46 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
48 if (serverCommit === undefined) serverCommit = await getServerCommit()
50 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
51 .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
52 .map(r => parseInt(r, 10))
54 const json: ServerConfig = {
56 name: CONFIG.INSTANCE.NAME,
57 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
58 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
59 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
61 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
62 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
66 enabled: Emailer.Instance.isEnabled()
68 serverVersion: packageJSON.version,
73 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
81 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
84 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
91 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
93 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
98 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
100 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
104 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
110 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
112 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
116 videoQuota: CONFIG.USER.VIDEO_QUOTA,
117 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
121 return res.json(json)
124 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
125 const about: About = {
127 name: CONFIG.INSTANCE.NAME,
128 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
129 description: CONFIG.INSTANCE.DESCRIPTION,
130 terms: CONFIG.INSTANCE.TERMS
134 return res.json(about).end()
137 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
138 const data = customConfig()
140 return res.json(data).end()
143 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
144 await remove(CONFIG.CUSTOM_FILE)
146 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
149 ClientHtml.invalidCache()
151 const data = customConfig()
153 return res.json(data).end()
156 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
157 const toUpdate: CustomConfig = req.body
158 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
160 // Force number conversion
161 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
162 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
163 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
164 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
165 toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
166 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
168 // camelCase to snake_case key
169 const toUpdateJSON = omit(
172 'instance.defaultClientRoute',
173 'instance.shortDescription',
174 'cache.videoCaptions',
175 'signup.requiresEmailVerification'
177 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
178 toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
179 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
180 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
181 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
182 toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
184 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
187 ClientHtml.invalidCache()
189 const data = customConfig()
192 getAuditIdFromRes(res),
193 new CustomConfigAuditView(data),
194 oldCustomConfigAuditKeys
197 return res.json(data).end()
200 // ---------------------------------------------------------------------------
206 // ---------------------------------------------------------------------------
208 function customConfig (): CustomConfig {
211 name: CONFIG.INSTANCE.NAME,
212 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
213 description: CONFIG.INSTANCE.DESCRIPTION,
214 terms: CONFIG.INSTANCE.TERMS,
215 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
216 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
218 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
219 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
224 username: CONFIG.SERVICES.TWITTER.USERNAME,
225 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
230 size: CONFIG.CACHE.PREVIEWS.SIZE
233 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
237 enabled: CONFIG.SIGNUP.ENABLED,
238 limit: CONFIG.SIGNUP.LIMIT,
239 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
242 email: CONFIG.ADMIN.EMAIL
245 videoQuota: CONFIG.USER.VIDEO_QUOTA,
246 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
249 enabled: CONFIG.TRANSCODING.ENABLED,
250 threads: CONFIG.TRANSCODING.THREADS,
252 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
253 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
254 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
255 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
256 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
262 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
265 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED