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