]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
09209296 | 2 | import { snakeCase } from 'lodash' |
18a6f04c | 3 | import { ServerConfig, ServerConfigPlugin, UserRight } from '../../../shared' |
09cababd C |
4 | import { About } from '../../../shared/models/server/about.model' |
5 | import { CustomConfig } from '../../../shared/models/server/custom-config.model' | |
06215f15 | 6 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' |
74dc3bca | 7 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
fd206f0b C |
8 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' |
9 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' | |
e032aec9 | 10 | import { ClientHtml } from '../../lib/client-html' |
993cef4b | 11 | import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' |
62689b94 | 12 | import { remove, writeJSON } from 'fs-extra' |
1b5e2d72 | 13 | import { getServerCommit } from '../../helpers/utils' |
3b3b1820 | 14 | import { Emailer } from '../../lib/emailer' |
a4101923 C |
15 | import { isNumeric } from 'validator' |
16 | import { objectConverter } from '../../helpers/core-utils' | |
6dd9de95 | 17 | import { CONFIG, reloadConfig } from '../../initializers/config' |
18a6f04c C |
18 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
19 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | |
65fcc311 | 20 | |
915c5bbe | 21 | const packageJSON = require('../../../../package.json') |
65fcc311 C |
22 | const configRouter = express.Router() |
23 | ||
80e36cd9 AB |
24 | const auditLogger = auditLoggerFactory('config') |
25 | ||
36f9424f | 26 | configRouter.get('/about', getAbout) |
eb080476 C |
27 | configRouter.get('/', |
28 | asyncMiddleware(getConfig) | |
29 | ) | |
36f9424f | 30 | |
fd206f0b C |
31 | configRouter.get('/custom', |
32 | authenticate, | |
33 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
34 | asyncMiddleware(getCustomConfig) | |
35 | ) | |
36 | configRouter.put('/custom', | |
37 | authenticate, | |
38 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
39 | asyncMiddleware(customConfigUpdateValidator), | |
40 | asyncMiddleware(updateCustomConfig) | |
41 | ) | |
42 | configRouter.delete('/custom', | |
43 | authenticate, | |
44 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
45 | asyncMiddleware(deleteCustomConfig) | |
46 | ) | |
65fcc311 | 47 | |
abb2c792 | 48 | let serverCommit: string |
1b5e2d72 | 49 | async function getConfig (req: express.Request, res: express.Response) { |
eb080476 | 50 | const allowed = await isSignupAllowed() |
ff2c1fe8 | 51 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) |
1b5e2d72 C |
52 | |
53 | if (serverCommit === undefined) serverCommit = await getServerCommit() | |
291e8d3e | 54 | |
eb080476 | 55 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) |
465679ca | 56 | .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) |
eb080476 | 57 | .map(r => parseInt(r, 10)) |
6a84aafd | 58 | |
18a6f04c C |
59 | const plugins: ServerConfigPlugin[] = [] |
60 | const registeredPlugins = PluginManager.Instance.getRegisteredPlugins() | |
61 | for (const pluginName of Object.keys(registeredPlugins)) { | |
62 | const plugin = registeredPlugins[ pluginName ] | |
63 | if (plugin.type !== PluginType.PLUGIN) continue | |
64 | ||
65 | plugins.push({ | |
66 | name: plugin.name, | |
67 | version: plugin.version, | |
68 | description: plugin.description, | |
69 | clientScripts: plugin.clientScripts | |
70 | }) | |
71 | } | |
72 | ||
eb080476 | 73 | const json: ServerConfig = { |
36f9424f | 74 | instance: { |
00b5556c | 75 | name: CONFIG.INSTANCE.NAME, |
2e3a0215 | 76 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
901637bb | 77 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
f8802489 | 78 | isNSFW: CONFIG.INSTANCE.IS_NSFW, |
0883b324 | 79 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
80 | customizations: { |
81 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
82 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
83 | } | |
36f9424f | 84 | }, |
18a6f04c | 85 | plugins, |
3b3b1820 | 86 | email: { |
d3e56c0c | 87 | enabled: Emailer.isEnabled() |
3b3b1820 | 88 | }, |
a4101923 C |
89 | contactForm: { |
90 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
91 | }, | |
915c5bbe | 92 | serverVersion: packageJSON.version, |
abb2c792 | 93 | serverCommit, |
eb080476 | 94 | signup: { |
ff2c1fe8 | 95 | allowed, |
d9eaee39 JM |
96 | allowedForCurrentIP, |
97 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
eb080476 C |
98 | }, |
99 | transcoding: { | |
09209296 C |
100 | hls: { |
101 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
102 | }, | |
eb080476 | 103 | enabledResolutions |
01de67b9 | 104 | }, |
5d08a6a7 | 105 | import: { |
b2977eec | 106 | videos: { |
5d08a6a7 C |
107 | http: { |
108 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
a84b8fa5 C |
109 | }, |
110 | torrent: { | |
111 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
5d08a6a7 C |
112 | } |
113 | } | |
114 | }, | |
7ccddd7b JM |
115 | autoBlacklist: { |
116 | videos: { | |
117 | ofUsers: { | |
118 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
119 | } | |
120 | } | |
121 | }, | |
01de67b9 C |
122 | avatar: { |
123 | file: { | |
124 | size: { | |
125 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
126 | }, | |
127 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
128 | } | |
129 | }, | |
130 | video: { | |
6de36768 C |
131 | image: { |
132 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
133 | size: { | |
134 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
135 | } | |
136 | }, | |
01de67b9 C |
137 | file: { |
138 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
139 | } | |
1869c875 | 140 | }, |
40e87e9e C |
141 | videoCaption: { |
142 | file: { | |
143 | size: { | |
144 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
145 | }, | |
146 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | |
147 | } | |
148 | }, | |
1869c875 | 149 | user: { |
bee0abff FA |
150 | videoQuota: CONFIG.USER.VIDEO_QUOTA, |
151 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
9b4b15f9 AB |
152 | }, |
153 | trending: { | |
154 | videos: { | |
155 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
156 | } | |
31b6ddf8 C |
157 | }, |
158 | tracker: { | |
159 | enabled: CONFIG.TRACKER.ENABLED | |
65fcc311 | 160 | } |
eb080476 | 161 | } |
6a84aafd | 162 | |
eb080476 | 163 | return res.json(json) |
65fcc311 C |
164 | } |
165 | ||
f8802489 | 166 | function getAbout (req: express.Request, res: express.Response) { |
36f9424f C |
167 | const about: About = { |
168 | instance: { | |
169 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 170 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
36f9424f C |
171 | description: CONFIG.INSTANCE.DESCRIPTION, |
172 | terms: CONFIG.INSTANCE.TERMS | |
173 | } | |
174 | } | |
175 | ||
176 | return res.json(about).end() | |
177 | } | |
178 | ||
f8802489 | 179 | async function getCustomConfig (req: express.Request, res: express.Response) { |
fd206f0b C |
180 | const data = customConfig() |
181 | ||
182 | return res.json(data).end() | |
183 | } | |
184 | ||
f8802489 | 185 | async function deleteCustomConfig (req: express.Request, res: express.Response) { |
62689b94 | 186 | await remove(CONFIG.CUSTOM_FILE) |
fd206f0b | 187 | |
993cef4b | 188 | auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig())) |
80e36cd9 | 189 | |
fd206f0b | 190 | reloadConfig() |
e032aec9 | 191 | ClientHtml.invalidCache() |
fd206f0b C |
192 | |
193 | const data = customConfig() | |
194 | ||
195 | return res.json(data).end() | |
196 | } | |
197 | ||
f8802489 | 198 | async function updateCustomConfig (req: express.Request, res: express.Response) { |
80e36cd9 | 199 | const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig()) |
fd206f0b | 200 | |
a4101923 C |
201 | // camelCase to snake_case key + Force number conversion |
202 | const toUpdateJSON = convertCustomConfigBody(req.body) | |
fd206f0b | 203 | |
62689b94 | 204 | await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 }) |
fd206f0b C |
205 | |
206 | reloadConfig() | |
e032aec9 | 207 | ClientHtml.invalidCache() |
fd206f0b C |
208 | |
209 | const data = customConfig() | |
80e36cd9 AB |
210 | |
211 | auditLogger.update( | |
993cef4b | 212 | getAuditIdFromRes(res), |
80e36cd9 AB |
213 | new CustomConfigAuditView(data), |
214 | oldCustomConfigAuditKeys | |
215 | ) | |
216 | ||
fd206f0b C |
217 | return res.json(data).end() |
218 | } | |
219 | ||
65fcc311 C |
220 | // --------------------------------------------------------------------------- |
221 | ||
222 | export { | |
223 | configRouter | |
224 | } | |
fd206f0b C |
225 | |
226 | // --------------------------------------------------------------------------- | |
227 | ||
228 | function customConfig (): CustomConfig { | |
229 | return { | |
66b16caf C |
230 | instance: { |
231 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 232 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
66b16caf | 233 | description: CONFIG.INSTANCE.DESCRIPTION, |
00b5556c | 234 | terms: CONFIG.INSTANCE.TERMS, |
f8802489 | 235 | isNSFW: CONFIG.INSTANCE.IS_NSFW, |
901637bb | 236 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
0883b324 | 237 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
238 | customizations: { |
239 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
240 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
241 | } | |
66b16caf | 242 | }, |
8be1afa1 C |
243 | services: { |
244 | twitter: { | |
245 | username: CONFIG.SERVICES.TWITTER.USERNAME, | |
246 | whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED | |
247 | } | |
248 | }, | |
fd206f0b C |
249 | cache: { |
250 | previews: { | |
251 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
40e87e9e C |
252 | }, |
253 | captions: { | |
254 | size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE | |
fd206f0b C |
255 | } |
256 | }, | |
257 | signup: { | |
258 | enabled: CONFIG.SIGNUP.ENABLED, | |
d9eaee39 JM |
259 | limit: CONFIG.SIGNUP.LIMIT, |
260 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
fd206f0b C |
261 | }, |
262 | admin: { | |
263 | email: CONFIG.ADMIN.EMAIL | |
264 | }, | |
a4101923 C |
265 | contactForm: { |
266 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
267 | }, | |
fd206f0b | 268 | user: { |
bee0abff FA |
269 | videoQuota: CONFIG.USER.VIDEO_QUOTA, |
270 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
fd206f0b C |
271 | }, |
272 | transcoding: { | |
273 | enabled: CONFIG.TRANSCODING.ENABLED, | |
14e2014a | 274 | allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS, |
536598cf | 275 | allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES, |
fd206f0b C |
276 | threads: CONFIG.TRANSCODING.THREADS, |
277 | resolutions: { | |
278 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
279 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
280 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
281 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
00aa1f0d C |
282 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ], |
283 | '2160p': CONFIG.TRANSCODING.RESOLUTIONS[ '2160p' ] | |
09209296 C |
284 | }, |
285 | hls: { | |
286 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
fd206f0b | 287 | } |
5d08a6a7 C |
288 | }, |
289 | import: { | |
290 | videos: { | |
291 | http: { | |
292 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
a84b8fa5 C |
293 | }, |
294 | torrent: { | |
295 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
5d08a6a7 C |
296 | } |
297 | } | |
7ccddd7b JM |
298 | }, |
299 | autoBlacklist: { | |
300 | videos: { | |
301 | ofUsers: { | |
302 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
303 | } | |
304 | } | |
5b9c965d C |
305 | }, |
306 | followers: { | |
307 | instance: { | |
14893eb7 C |
308 | enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED, |
309 | manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL | |
5b9c965d | 310 | } |
fd206f0b C |
311 | } |
312 | } | |
313 | } | |
a4101923 C |
314 | |
315 | function convertCustomConfigBody (body: CustomConfig) { | |
316 | function keyConverter (k: string) { | |
317 | // Transcoding resolutions exception | |
318 | if (/^\d{3,4}p$/.exec(k)) return k | |
319 | ||
320 | return snakeCase(k) | |
321 | } | |
322 | ||
323 | function valueConverter (v: any) { | |
324 | if (isNumeric(v + '')) return parseInt('' + v, 10) | |
325 | ||
326 | return v | |
327 | } | |
328 | ||
329 | return objectConverter(body, keyConverter, valueConverter) | |
330 | } |