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