]>
Commit | Line | Data |
---|---|---|
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 } from '../../helpers/audit-logger' | |
12 | import { remove, writeJSON } from 'fs-extra' | |
13 | ||
14 | const packageJSON = require('../../../../package.json') | |
15 | const configRouter = express.Router() | |
16 | ||
17 | const auditLogger = auditLoggerFactory('config') | |
18 | ||
19 | configRouter.get('/about', getAbout) | |
20 | configRouter.get('/', | |
21 | asyncMiddleware(getConfig) | |
22 | ) | |
23 | ||
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 | ) | |
40 | ||
41 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
42 | const allowed = await isSignupAllowed() | |
43 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) | |
44 | ||
45 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) | |
46 | .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true) | |
47 | .map(r => parseInt(r, 10)) | |
48 | ||
49 | const json: ServerConfig = { | |
50 | instance: { | |
51 | name: CONFIG.INSTANCE.NAME, | |
52 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
53 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
54 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
55 | customizations: { | |
56 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
57 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
58 | } | |
59 | }, | |
60 | serverVersion: packageJSON.version, | |
61 | signup: { | |
62 | allowed, | |
63 | allowedForCurrentIP, | |
64 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
65 | }, | |
66 | transcoding: { | |
67 | enabledResolutions | |
68 | }, | |
69 | import: { | |
70 | videos: { | |
71 | http: { | |
72 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
73 | }, | |
74 | torrent: { | |
75 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
76 | } | |
77 | } | |
78 | }, | |
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: { | |
88 | image: { | |
89 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
90 | size: { | |
91 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
92 | } | |
93 | }, | |
94 | file: { | |
95 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
96 | } | |
97 | }, | |
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 | }, | |
106 | user: { | |
107 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | |
108 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
109 | } | |
110 | } | |
111 | ||
112 | return res.json(json) | |
113 | } | |
114 | ||
115 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { | |
116 | const about: About = { | |
117 | instance: { | |
118 | name: CONFIG.INSTANCE.NAME, | |
119 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
120 | description: CONFIG.INSTANCE.DESCRIPTION, | |
121 | terms: CONFIG.INSTANCE.TERMS | |
122 | } | |
123 | } | |
124 | ||
125 | return res.json(about).end() | |
126 | } | |
127 | ||
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) { | |
135 | await remove(CONFIG.CUSTOM_FILE) | |
136 | ||
137 | auditLogger.delete( | |
138 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
139 | new CustomConfigAuditView(customConfig()) | |
140 | ) | |
141 | ||
142 | reloadConfig() | |
143 | ClientHtml.invalidCache() | |
144 | ||
145 | const data = customConfig() | |
146 | ||
147 | return res.json(data).end() | |
148 | } | |
149 | ||
150 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
151 | const toUpdate: CustomConfig = req.body | |
152 | const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig()) | |
153 | ||
154 | // Force number conversion | |
155 | toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10) | |
156 | toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10) | |
157 | toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10) | |
158 | toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10) | |
159 | toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10) | |
160 | toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10) | |
161 | ||
162 | // camelCase to snake_case key | |
163 | const toUpdateJSON = omit( | |
164 | toUpdate, | |
165 | 'user.videoQuota', | |
166 | 'instance.defaultClientRoute', | |
167 | 'instance.shortDescription', | |
168 | 'cache.videoCaptions', | |
169 | 'signup.requiresEmailVerification' | |
170 | ) | |
171 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | |
172 | toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily | |
173 | toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute | |
174 | toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription | |
175 | toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy | |
176 | toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification | |
177 | ||
178 | await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 }) | |
179 | ||
180 | reloadConfig() | |
181 | ClientHtml.invalidCache() | |
182 | ||
183 | const data = customConfig() | |
184 | ||
185 | auditLogger.update( | |
186 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
187 | new CustomConfigAuditView(data), | |
188 | oldCustomConfigAuditKeys | |
189 | ) | |
190 | ||
191 | return res.json(data).end() | |
192 | } | |
193 | ||
194 | // --------------------------------------------------------------------------- | |
195 | ||
196 | export { | |
197 | configRouter | |
198 | } | |
199 | ||
200 | // --------------------------------------------------------------------------- | |
201 | ||
202 | function customConfig (): CustomConfig { | |
203 | return { | |
204 | instance: { | |
205 | name: CONFIG.INSTANCE.NAME, | |
206 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
207 | description: CONFIG.INSTANCE.DESCRIPTION, | |
208 | terms: CONFIG.INSTANCE.TERMS, | |
209 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
210 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
211 | customizations: { | |
212 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
213 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
214 | } | |
215 | }, | |
216 | services: { | |
217 | twitter: { | |
218 | username: CONFIG.SERVICES.TWITTER.USERNAME, | |
219 | whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED | |
220 | } | |
221 | }, | |
222 | cache: { | |
223 | previews: { | |
224 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
225 | }, | |
226 | captions: { | |
227 | size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE | |
228 | } | |
229 | }, | |
230 | signup: { | |
231 | enabled: CONFIG.SIGNUP.ENABLED, | |
232 | limit: CONFIG.SIGNUP.LIMIT, | |
233 | requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION | |
234 | }, | |
235 | admin: { | |
236 | email: CONFIG.ADMIN.EMAIL | |
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 | threads: CONFIG.TRANSCODING.THREADS, | |
245 | resolutions: { | |
246 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
247 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
248 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
249 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
250 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
251 | } | |
252 | }, | |
253 | import: { | |
254 | videos: { | |
255 | http: { | |
256 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
257 | }, | |
258 | torrent: { | |
259 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
260 | } | |
261 | } | |
262 | } | |
263 | } | |
264 | } |