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