]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
a4101923 | 2 | import { omit, snakeCase } from 'lodash' |
fd206f0b | 3 | import { ServerConfig, 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' |
fd206f0b C |
7 | import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' |
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' | |
65fcc311 | 17 | |
915c5bbe | 18 | const packageJSON = require('../../../../package.json') |
65fcc311 C |
19 | const configRouter = express.Router() |
20 | ||
80e36cd9 AB |
21 | const auditLogger = auditLoggerFactory('config') |
22 | ||
36f9424f | 23 | configRouter.get('/about', getAbout) |
eb080476 C |
24 | configRouter.get('/', |
25 | asyncMiddleware(getConfig) | |
26 | ) | |
36f9424f | 27 | |
fd206f0b C |
28 | configRouter.get('/custom', |
29 | authenticate, | |
30 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
31 | asyncMiddleware(getCustomConfig) | |
32 | ) | |
33 | configRouter.put('/custom', | |
34 | authenticate, | |
35 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
36 | asyncMiddleware(customConfigUpdateValidator), | |
37 | asyncMiddleware(updateCustomConfig) | |
38 | ) | |
39 | configRouter.delete('/custom', | |
40 | authenticate, | |
41 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
42 | asyncMiddleware(deleteCustomConfig) | |
43 | ) | |
65fcc311 | 44 | |
abb2c792 | 45 | let serverCommit: string |
1b5e2d72 | 46 | async function getConfig (req: express.Request, res: express.Response) { |
eb080476 | 47 | const allowed = await isSignupAllowed() |
ff2c1fe8 | 48 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) |
1b5e2d72 C |
49 | |
50 | if (serverCommit === undefined) serverCommit = await getServerCommit() | |
291e8d3e | 51 | |
eb080476 | 52 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) |
41a676db | 53 | .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true) |
eb080476 | 54 | .map(r => parseInt(r, 10)) |
6a84aafd | 55 | |
eb080476 | 56 | const json: ServerConfig = { |
36f9424f | 57 | instance: { |
00b5556c | 58 | name: CONFIG.INSTANCE.NAME, |
2e3a0215 | 59 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
901637bb | 60 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
0883b324 | 61 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
62 | customizations: { |
63 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
64 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
65 | } | |
36f9424f | 66 | }, |
3b3b1820 | 67 | email: { |
d3e56c0c | 68 | enabled: Emailer.isEnabled() |
3b3b1820 | 69 | }, |
a4101923 C |
70 | contactForm: { |
71 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
72 | }, | |
915c5bbe | 73 | serverVersion: packageJSON.version, |
abb2c792 | 74 | serverCommit, |
eb080476 | 75 | signup: { |
ff2c1fe8 | 76 | allowed, |
d9eaee39 JM |
77 | allowedForCurrentIP, |
78 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
eb080476 C |
79 | }, |
80 | transcoding: { | |
81 | enabledResolutions | |
01de67b9 | 82 | }, |
5d08a6a7 | 83 | import: { |
b2977eec | 84 | videos: { |
5d08a6a7 C |
85 | http: { |
86 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
a84b8fa5 C |
87 | }, |
88 | torrent: { | |
89 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
5d08a6a7 C |
90 | } |
91 | } | |
92 | }, | |
01de67b9 C |
93 | avatar: { |
94 | file: { | |
95 | size: { | |
96 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
97 | }, | |
98 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
99 | } | |
100 | }, | |
101 | video: { | |
6de36768 C |
102 | image: { |
103 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
104 | size: { | |
105 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
106 | } | |
107 | }, | |
01de67b9 C |
108 | file: { |
109 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
110 | } | |
1869c875 | 111 | }, |
40e87e9e C |
112 | videoCaption: { |
113 | file: { | |
114 | size: { | |
115 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
116 | }, | |
117 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | |
118 | } | |
119 | }, | |
1869c875 | 120 | user: { |
bee0abff FA |
121 | videoQuota: CONFIG.USER.VIDEO_QUOTA, |
122 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
9b4b15f9 AB |
123 | }, |
124 | trending: { | |
125 | videos: { | |
126 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
127 | } | |
65fcc311 | 128 | } |
eb080476 | 129 | } |
6a84aafd | 130 | |
eb080476 | 131 | return res.json(json) |
65fcc311 C |
132 | } |
133 | ||
36f9424f C |
134 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { |
135 | const about: About = { | |
136 | instance: { | |
137 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 138 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
36f9424f C |
139 | description: CONFIG.INSTANCE.DESCRIPTION, |
140 | terms: CONFIG.INSTANCE.TERMS | |
141 | } | |
142 | } | |
143 | ||
144 | return res.json(about).end() | |
145 | } | |
146 | ||
fd206f0b C |
147 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
148 | const data = customConfig() | |
149 | ||
150 | return res.json(data).end() | |
151 | } | |
152 | ||
153 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
62689b94 | 154 | await remove(CONFIG.CUSTOM_FILE) |
fd206f0b | 155 | |
993cef4b | 156 | auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig())) |
80e36cd9 | 157 | |
fd206f0b | 158 | reloadConfig() |
e032aec9 | 159 | ClientHtml.invalidCache() |
fd206f0b C |
160 | |
161 | const data = customConfig() | |
162 | ||
163 | return res.json(data).end() | |
164 | } | |
165 | ||
166 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
80e36cd9 | 167 | const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig()) |
fd206f0b | 168 | |
a4101923 C |
169 | // camelCase to snake_case key + Force number conversion |
170 | const toUpdateJSON = convertCustomConfigBody(req.body) | |
fd206f0b | 171 | |
62689b94 | 172 | await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 }) |
fd206f0b C |
173 | |
174 | reloadConfig() | |
e032aec9 | 175 | ClientHtml.invalidCache() |
fd206f0b C |
176 | |
177 | const data = customConfig() | |
80e36cd9 AB |
178 | |
179 | auditLogger.update( | |
993cef4b | 180 | getAuditIdFromRes(res), |
80e36cd9 AB |
181 | new CustomConfigAuditView(data), |
182 | oldCustomConfigAuditKeys | |
183 | ) | |
184 | ||
fd206f0b C |
185 | return res.json(data).end() |
186 | } | |
187 | ||
65fcc311 C |
188 | // --------------------------------------------------------------------------- |
189 | ||
190 | export { | |
191 | configRouter | |
192 | } | |
fd206f0b C |
193 | |
194 | // --------------------------------------------------------------------------- | |
195 | ||
196 | function customConfig (): CustomConfig { | |
197 | return { | |
66b16caf C |
198 | instance: { |
199 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 200 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
66b16caf | 201 | description: CONFIG.INSTANCE.DESCRIPTION, |
00b5556c | 202 | terms: CONFIG.INSTANCE.TERMS, |
901637bb | 203 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
0883b324 | 204 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
205 | customizations: { |
206 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
207 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
208 | } | |
66b16caf | 209 | }, |
8be1afa1 C |
210 | services: { |
211 | twitter: { | |
212 | username: CONFIG.SERVICES.TWITTER.USERNAME, | |
213 | whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED | |
214 | } | |
215 | }, | |
fd206f0b C |
216 | cache: { |
217 | previews: { | |
218 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
40e87e9e C |
219 | }, |
220 | captions: { | |
221 | size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE | |
fd206f0b C |
222 | } |
223 | }, | |
224 | signup: { | |
225 | enabled: CONFIG.SIGNUP.ENABLED, | |
d9eaee39 JM |
226 | limit: CONFIG.SIGNUP.LIMIT, |
227 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
fd206f0b C |
228 | }, |
229 | admin: { | |
230 | email: CONFIG.ADMIN.EMAIL | |
231 | }, | |
a4101923 C |
232 | contactForm: { |
233 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
234 | }, | |
fd206f0b | 235 | user: { |
bee0abff FA |
236 | videoQuota: CONFIG.USER.VIDEO_QUOTA, |
237 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
fd206f0b C |
238 | }, |
239 | transcoding: { | |
240 | enabled: CONFIG.TRANSCODING.ENABLED, | |
14e2014a | 241 | allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS, |
fd206f0b C |
242 | threads: CONFIG.TRANSCODING.THREADS, |
243 | resolutions: { | |
244 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
245 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
246 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
247 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
248 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
249 | } | |
5d08a6a7 C |
250 | }, |
251 | import: { | |
252 | videos: { | |
253 | http: { | |
254 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
a84b8fa5 C |
255 | }, |
256 | torrent: { | |
257 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
5d08a6a7 C |
258 | } |
259 | } | |
fd206f0b C |
260 | } |
261 | } | |
262 | } | |
a4101923 C |
263 | |
264 | function convertCustomConfigBody (body: CustomConfig) { | |
265 | function keyConverter (k: string) { | |
266 | // Transcoding resolutions exception | |
267 | if (/^\d{3,4}p$/.exec(k)) return k | |
268 | ||
269 | return snakeCase(k) | |
270 | } | |
271 | ||
272 | function valueConverter (v: any) { | |
273 | if (isNumeric(v + '')) return parseInt('' + v, 10) | |
274 | ||
275 | return v | |
276 | } | |
277 | ||
278 | return objectConverter(body, keyConverter, valueConverter) | |
279 | } |