]>
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, |
00b5556c C |
49 | customizations: { |
50 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT, | |
51 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS | |
52 | } | |
36f9424f | 53 | }, |
915c5bbe | 54 | serverVersion: packageJSON.version, |
eb080476 C |
55 | signup: { |
56 | allowed | |
57 | }, | |
58 | transcoding: { | |
59 | enabledResolutions | |
01de67b9 C |
60 | }, |
61 | avatar: { | |
62 | file: { | |
63 | size: { | |
64 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
65 | }, | |
66 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
67 | } | |
68 | }, | |
69 | video: { | |
6de36768 C |
70 | image: { |
71 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
72 | size: { | |
73 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
74 | } | |
75 | }, | |
01de67b9 C |
76 | file: { |
77 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
78 | } | |
1869c875 RK |
79 | }, |
80 | user: { | |
81 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
65fcc311 | 82 | } |
eb080476 | 83 | } |
6a84aafd | 84 | |
eb080476 | 85 | return res.json(json) |
65fcc311 C |
86 | } |
87 | ||
36f9424f C |
88 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { |
89 | const about: About = { | |
90 | instance: { | |
91 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 92 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
36f9424f C |
93 | description: CONFIG.INSTANCE.DESCRIPTION, |
94 | terms: CONFIG.INSTANCE.TERMS | |
95 | } | |
96 | } | |
97 | ||
98 | return res.json(about).end() | |
99 | } | |
100 | ||
fd206f0b C |
101 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
102 | const data = customConfig() | |
103 | ||
104 | return res.json(data).end() | |
105 | } | |
106 | ||
107 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
108 | await unlinkPromise(CONFIG.CUSTOM_FILE) | |
109 | ||
110 | reloadConfig() | |
111 | ||
112 | const data = customConfig() | |
113 | ||
114 | return res.json(data).end() | |
115 | } | |
116 | ||
117 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
118 | const toUpdate: CustomConfig = req.body | |
119 | ||
120 | // Need to change the videoQuota key a little bit | |
121 | const toUpdateJSON = omit(toUpdate, 'videoQuota') | |
122 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | |
901637bb | 123 | toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute |
2e3a0215 | 124 | toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription |
509cd56a | 125 | delete toUpdate.user.videoQuota |
901637bb | 126 | delete toUpdate.instance.defaultClientRoute |
2e3a0215 | 127 | delete toUpdate.instance.shortDescription |
fd206f0b | 128 | |
2ad42952 | 129 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2)) |
fd206f0b C |
130 | |
131 | reloadConfig() | |
132 | ||
133 | const data = customConfig() | |
134 | return res.json(data).end() | |
135 | } | |
136 | ||
65fcc311 C |
137 | // --------------------------------------------------------------------------- |
138 | ||
139 | export { | |
140 | configRouter | |
141 | } | |
fd206f0b C |
142 | |
143 | // --------------------------------------------------------------------------- | |
144 | ||
145 | function customConfig (): CustomConfig { | |
146 | return { | |
66b16caf C |
147 | instance: { |
148 | name: CONFIG.INSTANCE.NAME, | |
2e3a0215 | 149 | shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
66b16caf | 150 | description: CONFIG.INSTANCE.DESCRIPTION, |
00b5556c | 151 | terms: CONFIG.INSTANCE.TERMS, |
901637bb | 152 | defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, |
00b5556c C |
153 | customizations: { |
154 | css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS, | |
155 | javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT | |
156 | } | |
66b16caf | 157 | }, |
fd206f0b C |
158 | cache: { |
159 | previews: { | |
160 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
161 | } | |
162 | }, | |
163 | signup: { | |
164 | enabled: CONFIG.SIGNUP.ENABLED, | |
165 | limit: CONFIG.SIGNUP.LIMIT | |
166 | }, | |
167 | admin: { | |
168 | email: CONFIG.ADMIN.EMAIL | |
169 | }, | |
170 | user: { | |
171 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
172 | }, | |
173 | transcoding: { | |
174 | enabled: CONFIG.TRANSCODING.ENABLED, | |
175 | threads: CONFIG.TRANSCODING.THREADS, | |
176 | resolutions: { | |
177 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
178 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
179 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
180 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
181 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
182 | } | |
183 | } | |
184 | } | |
185 | } |