diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/config.ts | 91 |
1 files changed, 87 insertions, 4 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 35c89835b..f0b2c3d79 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts | |||
@@ -1,15 +1,34 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { ServerConfig, UserRight } from '../../../shared' | ||
3 | import { CustomConfig } from '../../../shared/models/config/custom-config.model' | ||
4 | import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils' | ||
2 | import { isSignupAllowed } from '../../helpers/utils' | 5 | import { isSignupAllowed } from '../../helpers/utils' |
3 | 6 | import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' | |
4 | import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' | 7 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' |
5 | import { asyncMiddleware } from '../../middlewares' | 8 | import { customConfigUpdateValidator } from '../../middlewares/validators/config' |
6 | import { ServerConfig } from '../../../shared' | 9 | import { omit } from 'lodash' |
7 | 10 | ||
8 | const configRouter = express.Router() | 11 | const configRouter = express.Router() |
9 | 12 | ||
10 | configRouter.get('/', | 13 | configRouter.get('/', |
11 | asyncMiddleware(getConfig) | 14 | asyncMiddleware(getConfig) |
12 | ) | 15 | ) |
16 | configRouter.get('/custom', | ||
17 | authenticate, | ||
18 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | ||
19 | asyncMiddleware(getCustomConfig) | ||
20 | ) | ||
21 | configRouter.put('/custom', | ||
22 | authenticate, | ||
23 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | ||
24 | asyncMiddleware(customConfigUpdateValidator), | ||
25 | asyncMiddleware(updateCustomConfig) | ||
26 | ) | ||
27 | configRouter.delete('/custom', | ||
28 | authenticate, | ||
29 | ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), | ||
30 | asyncMiddleware(deleteCustomConfig) | ||
31 | ) | ||
13 | 32 | ||
14 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | 33 | async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
15 | const allowed = await isSignupAllowed() | 34 | const allowed = await isSignupAllowed() |
@@ -43,8 +62,72 @@ async function getConfig (req: express.Request, res: express.Response, next: exp | |||
43 | return res.json(json) | 62 | return res.json(json) |
44 | } | 63 | } |
45 | 64 | ||
65 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
66 | const data = customConfig() | ||
67 | |||
68 | return res.json(data).end() | ||
69 | } | ||
70 | |||
71 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
72 | await unlinkPromise(CONFIG.CUSTOM_FILE) | ||
73 | |||
74 | reloadConfig() | ||
75 | |||
76 | const data = customConfig() | ||
77 | |||
78 | return res.json(data).end() | ||
79 | } | ||
80 | |||
81 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
82 | const toUpdate: CustomConfig = req.body | ||
83 | |||
84 | // Need to change the videoQuota key a little bit | ||
85 | const toUpdateJSON = omit(toUpdate, 'videoQuota') | ||
86 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | ||
87 | |||
88 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON)) | ||
89 | |||
90 | reloadConfig() | ||
91 | |||
92 | const data = customConfig() | ||
93 | return res.json(data).end() | ||
94 | } | ||
95 | |||
46 | // --------------------------------------------------------------------------- | 96 | // --------------------------------------------------------------------------- |
47 | 97 | ||
48 | export { | 98 | export { |
49 | configRouter | 99 | configRouter |
50 | } | 100 | } |
101 | |||
102 | // --------------------------------------------------------------------------- | ||
103 | |||
104 | function customConfig (): CustomConfig { | ||
105 | return { | ||
106 | cache: { | ||
107 | previews: { | ||
108 | size: CONFIG.CACHE.PREVIEWS.SIZE | ||
109 | } | ||
110 | }, | ||
111 | signup: { | ||
112 | enabled: CONFIG.SIGNUP.ENABLED, | ||
113 | limit: CONFIG.SIGNUP.LIMIT | ||
114 | }, | ||
115 | admin: { | ||
116 | email: CONFIG.ADMIN.EMAIL | ||
117 | }, | ||
118 | user: { | ||
119 | videoQuota: CONFIG.USER.VIDEO_QUOTA | ||
120 | }, | ||
121 | transcoding: { | ||
122 | enabled: CONFIG.TRANSCODING.ENABLED, | ||
123 | threads: CONFIG.TRANSCODING.THREADS, | ||
124 | resolutions: { | ||
125 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | ||
126 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | ||
127 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | ||
128 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | ||
129 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | } | ||