]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
fd206f0b C |
2 | import { ServerConfig, UserRight } from '../../../shared' |
3 | import { CustomConfig } from '../../../shared/models/config/custom-config.model' | |
4 | import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils' | |
da854ddd | 5 | import { isSignupAllowed } from '../../helpers/utils' |
fd206f0b C |
6 | import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' |
7 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' | |
8 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' | |
9 | import { omit } from 'lodash' | |
65fcc311 | 10 | |
915c5bbe | 11 | const packageJSON = require('../../../../package.json') |
65fcc311 C |
12 | const configRouter = express.Router() |
13 | ||
eb080476 C |
14 | configRouter.get('/', |
15 | asyncMiddleware(getConfig) | |
16 | ) | |
fd206f0b C |
17 | configRouter.get('/custom', |
18 | authenticate, | |
19 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
20 | asyncMiddleware(getCustomConfig) | |
21 | ) | |
22 | configRouter.put('/custom', | |
23 | authenticate, | |
24 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
25 | asyncMiddleware(customConfigUpdateValidator), | |
26 | asyncMiddleware(updateCustomConfig) | |
27 | ) | |
28 | configRouter.delete('/custom', | |
29 | authenticate, | |
30 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | |
31 | asyncMiddleware(deleteCustomConfig) | |
32 | ) | |
65fcc311 | 33 | |
eb080476 C |
34 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
35 | const allowed = await isSignupAllowed() | |
291e8d3e | 36 | |
eb080476 C |
37 | const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) |
38 | .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true) | |
39 | .map(r => parseInt(r, 10)) | |
6a84aafd | 40 | |
eb080476 | 41 | const json: ServerConfig = { |
915c5bbe | 42 | serverVersion: packageJSON.version, |
eb080476 C |
43 | signup: { |
44 | allowed | |
45 | }, | |
46 | transcoding: { | |
47 | enabledResolutions | |
01de67b9 C |
48 | }, |
49 | avatar: { | |
50 | file: { | |
51 | size: { | |
52 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
53 | }, | |
54 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
55 | } | |
56 | }, | |
57 | video: { | |
58 | file: { | |
59 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
60 | } | |
65fcc311 | 61 | } |
eb080476 | 62 | } |
6a84aafd | 63 | |
eb080476 | 64 | return res.json(json) |
65fcc311 C |
65 | } |
66 | ||
fd206f0b C |
67 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
68 | const data = customConfig() | |
69 | ||
70 | return res.json(data).end() | |
71 | } | |
72 | ||
73 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
74 | await unlinkPromise(CONFIG.CUSTOM_FILE) | |
75 | ||
76 | reloadConfig() | |
77 | ||
78 | const data = customConfig() | |
79 | ||
80 | return res.json(data).end() | |
81 | } | |
82 | ||
83 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
84 | const toUpdate: CustomConfig = req.body | |
85 | ||
86 | // Need to change the videoQuota key a little bit | |
87 | const toUpdateJSON = omit(toUpdate, 'videoQuota') | |
88 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | |
89 | ||
90 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON)) | |
91 | ||
92 | reloadConfig() | |
93 | ||
94 | const data = customConfig() | |
95 | return res.json(data).end() | |
96 | } | |
97 | ||
65fcc311 C |
98 | // --------------------------------------------------------------------------- |
99 | ||
100 | export { | |
101 | configRouter | |
102 | } | |
fd206f0b C |
103 | |
104 | // --------------------------------------------------------------------------- | |
105 | ||
106 | function customConfig (): CustomConfig { | |
107 | return { | |
108 | cache: { | |
109 | previews: { | |
110 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
111 | } | |
112 | }, | |
113 | signup: { | |
114 | enabled: CONFIG.SIGNUP.ENABLED, | |
115 | limit: CONFIG.SIGNUP.LIMIT | |
116 | }, | |
117 | admin: { | |
118 | email: CONFIG.ADMIN.EMAIL | |
119 | }, | |
120 | user: { | |
121 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
122 | }, | |
123 | transcoding: { | |
124 | enabled: CONFIG.TRANSCODING.ENABLED, | |
125 | threads: CONFIG.TRANSCODING.THREADS, | |
126 | resolutions: { | |
127 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
128 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
129 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
130 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
131 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
132 | } | |
133 | } | |
134 | } | |
135 | } |