]>
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' | |
fd206f0b | 6 | import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils' |
da854ddd | 7 | import { isSignupAllowed } from '../../helpers/utils' |
fd206f0b C |
8 | import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' |
9 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' | |
10 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' | |
65fcc311 | 11 | |
915c5bbe | 12 | const packageJSON = require('../../../../package.json') |
65fcc311 C |
13 | const configRouter = express.Router() |
14 | ||
36f9424f | 15 | configRouter.get('/about', getAbout) |
eb080476 C |
16 | configRouter.get('/', |
17 | asyncMiddleware(getConfig) | |
18 | ) | |
36f9424f | 19 | |
fd206f0b C |
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 | ) | |
65fcc311 | 36 | |
eb080476 C |
37 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
38 | const allowed = await isSignupAllowed() | |
291e8d3e | 39 | |
eb080476 C |
40 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) |
41 | .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true) | |
42 | .map(r => parseInt(r, 10)) | |
6a84aafd | 43 | |
eb080476 | 44 | const json: ServerConfig = { |
36f9424f | 45 | instance: { |
00b5556c | 46 | name: CONFIG.INSTANCE.NAME, |
2e3a0215 | 47 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
901637bb | 48 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
0883b324 | 49 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
50 | customizations: { |
51 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
52 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
53 | } | |
36f9424f | 54 | }, |
915c5bbe | 55 | serverVersion: packageJSON.version, |
eb080476 C |
56 | signup: { |
57 | allowed | |
58 | }, | |
59 | transcoding: { | |
60 | enabledResolutions | |
01de67b9 C |
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: { | |
6de36768 C |
71 | image: { |
72 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
73 | size: { | |
74 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
75 | } | |
76 | }, | |
01de67b9 C |
77 | file: { |
78 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
79 | } | |
1869c875 RK |
80 | }, |
81 | user: { | |
82 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
65fcc311 | 83 | } |
eb080476 | 84 | } |
6a84aafd | 85 | |
eb080476 | 86 | return res.json(json) |
65fcc311 C |
87 | } |
88 | ||
36f9424f C |
89 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { |
90 | const about: About = { | |
91 | instance: { | |
92 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 93 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
36f9424f C |
94 | description: CONFIG.INSTANCE.DESCRIPTION, |
95 | terms: CONFIG.INSTANCE.TERMS | |
96 | } | |
97 | } | |
98 | ||
99 | return res.json(about).end() | |
100 | } | |
101 | ||
fd206f0b C |
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 | ||
be1fc4bc C |
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') | |
fd206f0b | 129 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota |
901637bb | 130 | toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute |
2e3a0215 | 131 | toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription |
0883b324 | 132 | toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy |
fd206f0b | 133 | |
2ad42952 | 134 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2)) |
fd206f0b C |
135 | |
136 | reloadConfig() | |
137 | ||
138 | const data = customConfig() | |
139 | return res.json(data).end() | |
140 | } | |
141 | ||
65fcc311 C |
142 | // --------------------------------------------------------------------------- |
143 | ||
144 | export { | |
145 | configRouter | |
146 | } | |
fd206f0b C |
147 | |
148 | // --------------------------------------------------------------------------- | |
149 | ||
150 | function customConfig (): CustomConfig { | |
151 | return { | |
66b16caf C |
152 | instance: { |
153 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 154 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
66b16caf | 155 | description: CONFIG.INSTANCE.DESCRIPTION, |
00b5556c | 156 | terms: CONFIG.INSTANCE.TERMS, |
901637bb | 157 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
0883b324 | 158 | defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
00b5556c C |
159 | customizations: { |
160 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
161 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
162 | } | |
66b16caf | 163 | }, |
fd206f0b C |
164 | cache: { |
165 | previews: { | |
166 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
167 | } | |
168 | }, | |
169 | signup: { | |
170 | enabled: CONFIG.SIGNUP.ENABLED, | |
171 | limit: CONFIG.SIGNUP.LIMIT | |
172 | }, | |
173 | admin: { | |
174 | email: CONFIG.ADMIN.EMAIL | |
175 | }, | |
176 | user: { | |
177 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
178 | }, | |
179 | transcoding: { | |
180 | enabled: CONFIG.TRANSCODING.ENABLED, | |
181 | threads: CONFIG.TRANSCODING.THREADS, | |
182 | resolutions: { | |
183 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
184 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
185 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
186 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
187 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
188 | } | |
189 | } | |
190 | } | |
191 | } |