]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
36f9424f | 2 | import { omit } from 'lodash' |
fd206f0b | 3 | import { ServerConfig, UserRight } from '../../../shared' |
36f9424f | 4 | import { About } from '../../../shared/models/config/about.model' |
fd206f0b C |
5 | import { CustomConfig } from '../../../shared/models/config/custom-config.model' |
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 C |
45 | instance: { |
46 | name: CONFIG.INSTANCE.NAME | |
47 | }, | |
915c5bbe | 48 | serverVersion: packageJSON.version, |
eb080476 C |
49 | signup: { |
50 | allowed | |
51 | }, | |
52 | transcoding: { | |
53 | enabledResolutions | |
01de67b9 C |
54 | }, |
55 | avatar: { | |
56 | file: { | |
57 | size: { | |
58 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
59 | }, | |
60 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
61 | } | |
62 | }, | |
63 | video: { | |
64 | file: { | |
65 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
66 | } | |
65fcc311 | 67 | } |
eb080476 | 68 | } |
6a84aafd | 69 | |
eb080476 | 70 | return res.json(json) |
65fcc311 C |
71 | } |
72 | ||
36f9424f C |
73 | function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) { |
74 | const about: About = { | |
75 | instance: { | |
76 | name: CONFIG.INSTANCE.NAME, | |
77 | description: CONFIG.INSTANCE.DESCRIPTION, | |
78 | terms: CONFIG.INSTANCE.TERMS | |
79 | } | |
80 | } | |
81 | ||
82 | return res.json(about).end() | |
83 | } | |
84 | ||
fd206f0b C |
85 | async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { |
86 | const data = customConfig() | |
87 | ||
88 | return res.json(data).end() | |
89 | } | |
90 | ||
91 | async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
92 | await unlinkPromise(CONFIG.CUSTOM_FILE) | |
93 | ||
94 | reloadConfig() | |
95 | ||
96 | const data = customConfig() | |
97 | ||
98 | return res.json(data).end() | |
99 | } | |
100 | ||
101 | async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) { | |
102 | const toUpdate: CustomConfig = req.body | |
103 | ||
104 | // Need to change the videoQuota key a little bit | |
105 | const toUpdateJSON = omit(toUpdate, 'videoQuota') | |
106 | toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota | |
107 | ||
108 | await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON)) | |
109 | ||
110 | reloadConfig() | |
111 | ||
112 | const data = customConfig() | |
113 | return res.json(data).end() | |
114 | } | |
115 | ||
65fcc311 C |
116 | // --------------------------------------------------------------------------- |
117 | ||
118 | export { | |
119 | configRouter | |
120 | } | |
fd206f0b C |
121 | |
122 | // --------------------------------------------------------------------------- | |
123 | ||
124 | function customConfig (): CustomConfig { | |
125 | return { | |
66b16caf C |
126 | instance: { |
127 | name: CONFIG.INSTANCE.NAME, | |
128 | description: CONFIG.INSTANCE.DESCRIPTION, | |
129 | terms: CONFIG.INSTANCE.TERMS | |
130 | }, | |
fd206f0b C |
131 | cache: { |
132 | previews: { | |
133 | size: CONFIG.CACHE.PREVIEWS.SIZE | |
134 | } | |
135 | }, | |
136 | signup: { | |
137 | enabled: CONFIG.SIGNUP.ENABLED, | |
138 | limit: CONFIG.SIGNUP.LIMIT | |
139 | }, | |
140 | admin: { | |
141 | email: CONFIG.ADMIN.EMAIL | |
142 | }, | |
143 | user: { | |
144 | videoQuota: CONFIG.USER.VIDEO_QUOTA | |
145 | }, | |
146 | transcoding: { | |
147 | enabled: CONFIG.TRANSCODING.ENABLED, | |
148 | threads: CONFIG.TRANSCODING.THREADS, | |
149 | resolutions: { | |
150 | '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], | |
151 | '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], | |
152 | '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], | |
153 | '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], | |
154 | '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] | |
155 | } | |
156 | } | |
157 | } | |
158 | } |