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',
176 'transcoding.allowAdditionalExtensions'
178 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
179 toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
180 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
181 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
182 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
183 toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
184 toUpdateJSON.transcoding['allow_additional_extensions'] = toUpdate.transcoding.allowAdditionalExtensions
186 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
189 ClientHtml.invalidCache()
191 const data = customConfig()
194 getAuditIdFromRes(res),
195 new CustomConfigAuditView(data),
196 oldCustomConfigAuditKeys
199 return res.json(data).end()
202 // ---------------------------------------------------------------------------
208 // ---------------------------------------------------------------------------
210 function customConfig (): CustomConfig {
213 name: CONFIG.INSTANCE.NAME,
214 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
215 description: CONFIG.INSTANCE.DESCRIPTION,
216 terms: CONFIG.INSTANCE.TERMS,
217 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
218 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
220 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
221 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
226 username: CONFIG.SERVICES.TWITTER.USERNAME,
227 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
232 size: CONFIG.CACHE.PREVIEWS.SIZE
235 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
239 enabled: CONFIG.SIGNUP.ENABLED,
240 limit: CONFIG.SIGNUP.LIMIT,
241 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
244 email: CONFIG.ADMIN.EMAIL
247 videoQuota: CONFIG.USER.VIDEO_QUOTA,
248 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
251 enabled: CONFIG.TRANSCODING.ENABLED,
252 allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
253 threads: CONFIG.TRANSCODING.THREADS,
255 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
256 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
257 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
258 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
259 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
265 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
268 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED