]>
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 { unlinkPromise, writeFilePromise } from '../../helpers/core-utils' | |
7 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils' | |
8 | import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' | |
9 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' | |
10 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' | |
11 | ||
12 | const packageJSON = require('../../../../package.json') | |
13 | const configRouter = express.Router() | |
14 | ||
15 | configRouter.get('/about', getAbout) | |
16 | configRouter.get('/', | |
17 | asyncMiddleware(getConfig) | |
18 | ) | |
19 | ||
20 | configRouter.get('/custom', | |
21 | authenticate, | |
22 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
23 | asyncMiddleware(getCustomConfig) | |
24 | ) | |
25 | configRouter.put('/custom', | |
26 | authenticate, | |
27 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
28 | asyncMiddleware(customConfigUpdateValidator), | |
29 | asyncMiddleware(updateCustomConfig) | |
30 | ) | |
31 | configRouter.delete('/custom', | |
32 | authenticate, | |
33 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
34 | asyncMiddleware(deleteCustomConfig) | |
35 | ) | |
36 | ||
37 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
38 | const allowed = await isSignupAllowed() | |
39 | const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) | |
40 | ||
41 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) | |
42 | .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true) | |
43 | .map(r => parseInt(r, 10)) | |
44 | ||
45 | const json: ServerConfig = { | |
46 | instance: { | |
47 | name: CONFIG.INSTANCE.NAME, | |
48 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
49 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
50 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
51 | customizations: { | |
52 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
53 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
54 | } | |
55 | }, | |
56 | serverVersion: packageJSON.version, | |
57 | signup: { | |
58 | allowed, | |
59 | allowedForCurrentIP | |
60 | }, | |
61 | transcoding: { | |
62 | enabledResolutions | |
63 | }, | |
64 | avatar: { | |
65 | file: { | |
66 | size: { | |
67 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
68 | }, | |
69 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
70 | } | |
71 | }, | |
72 | video: { | |
73 | image: { | |
74 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
75 | size: { | |
76 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
77 | } | |
78 | }, | |
79 | file: { | |
80 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
81 | } | |
82 | }, | |
83 | user: { | |
84 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
85 | } | |
86 | } | |
87 | ||
88 | return res.json(json) | |
89 | } | |
90 | ||
91 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { | |
92 | const about: About = { | |
93 | instance: { | |
94 | name: CONFIG.INSTANCE.NAME, | |
95 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
96 | description: CONFIG.INSTANCE.DESCRIPTION, | |
97 | terms: CONFIG.INSTANCE.TERMS | |
98 | } | |
99 | } | |
100 | ||
101 | return res.json(about).end() | |
102 | } | |
103 | ||
104 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
105 | const data = customConfig() | |
106 | ||
107 | return res.json(data).end() | |
108 | } | |
109 | ||
110 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
111 | await unlinkPromise(CONFIG.CUSTOM_FILE) | |
112 | ||
113 | reloadConfig() | |
114 | ||
115 | const data = customConfig() | |
116 | ||
117 | return res.json(data).end() | |
118 | } | |
119 | ||
120 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
121 | const toUpdate: CustomConfig = req.body | |
122 | ||
123 | // Force number conversion | |
124 | toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10) | |
125 | toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10) | |
126 | toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10) | |
127 | toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10) | |
128 | ||
129 | // camelCase to snake_case key | |
130 | const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription') | |
131 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | |
132 | toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute | |
133 | toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription | |
134 | toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy | |
135 | ||
136 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2)) | |
137 | ||
138 | reloadConfig() | |
139 | ||
140 | const data = customConfig() | |
141 | return res.json(data).end() | |
142 | } | |
143 | ||
144 | // --------------------------------------------------------------------------- | |
145 | ||
146 | export { | |
147 | configRouter | |
148 | } | |
149 | ||
150 | // --------------------------------------------------------------------------- | |
151 | ||
152 | function customConfig (): CustomConfig { | |
153 | return { | |
154 | instance: { | |
155 | name: CONFIG.INSTANCE.NAME, | |
156 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
157 | description: CONFIG.INSTANCE.DESCRIPTION, | |
158 | terms: CONFIG.INSTANCE.TERMS, | |
159 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, | |
160 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | |
161 | customizations: { | |
162 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
163 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
164 | } | |
165 | }, | |
166 | services: { | |
167 | twitter: { | |
168 | username: CONFIG.SERVICES.TWITTER.USERNAME, | |
169 | whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED | |
170 | } | |
171 | }, | |
172 | cache: { | |
173 | previews: { | |
174 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
175 | } | |
176 | }, | |
177 | signup: { | |
178 | enabled: CONFIG.SIGNUP.ENABLED, | |
179 | limit: CONFIG.SIGNUP.LIMIT | |
180 | }, | |
181 | admin: { | |
182 | email: CONFIG.ADMIN.EMAIL | |
183 | }, | |
184 | user: { | |
185 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
186 | }, | |
187 | transcoding: { | |
188 | enabled: CONFIG.TRANSCODING.ENABLED, | |
189 | threads: CONFIG.TRANSCODING.THREADS, | |
190 | resolutions: { | |
191 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
192 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
193 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
194 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
195 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
196 | } | |
197 | } | |
198 | } | |
199 | } |