]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Fix oauth server module
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
36f9424f 2import { omit } from 'lodash'
fd206f0b 3import { ServerConfig, UserRight } from '../../../shared'
09cababd
C
4import { About } from '../../../shared/models/server/about.model'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
06215f15 6import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
fd206f0b
C
7import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
8import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9import { customConfigUpdateValidator } from '../../middlewares/validators/config'
e032aec9 10import { ClientHtml } from '../../lib/client-html'
a84b8fa5 11import { auditLoggerFactory, CustomConfigAuditView } from '../../helpers/audit-logger'
62689b94 12import { remove, writeJSON } from 'fs-extra'
65fcc311 13
915c5bbe 14const packageJSON = require('../../../../package.json')
65fcc311
C
15const configRouter = express.Router()
16
80e36cd9
AB
17const auditLogger = auditLoggerFactory('config')
18
36f9424f 19configRouter.get('/about', getAbout)
eb080476
C
20configRouter.get('/',
21 asyncMiddleware(getConfig)
22)
36f9424f 23
fd206f0b
C
24configRouter.get('/custom',
25 authenticate,
26 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
27 asyncMiddleware(getCustomConfig)
28)
29configRouter.put('/custom',
30 authenticate,
31 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
32 asyncMiddleware(customConfigUpdateValidator),
33 asyncMiddleware(updateCustomConfig)
34)
35configRouter.delete('/custom',
36 authenticate,
37 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38 asyncMiddleware(deleteCustomConfig)
39)
65fcc311 40
eb080476
C
41async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
42 const allowed = await isSignupAllowed()
ff2c1fe8 43 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
291e8d3e 44
eb080476
C
45 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
46 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
47 .map(r => parseInt(r, 10))
6a84aafd 48
eb080476 49 const json: ServerConfig = {
36f9424f 50 instance: {
00b5556c 51 name: CONFIG.INSTANCE.NAME,
2e3a0215 52 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
901637bb 53 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 54 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
55 customizations: {
56 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
57 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
58 }
36f9424f 59 },
915c5bbe 60 serverVersion: packageJSON.version,
eb080476 61 signup: {
ff2c1fe8
RK
62 allowed,
63 allowedForCurrentIP
eb080476
C
64 },
65 transcoding: {
66 enabledResolutions
01de67b9 67 },
5d08a6a7 68 import: {
b2977eec 69 videos: {
5d08a6a7
C
70 http: {
71 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
a84b8fa5
C
72 },
73 torrent: {
74 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
5d08a6a7
C
75 }
76 }
77 },
01de67b9
C
78 avatar: {
79 file: {
80 size: {
81 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
82 },
83 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
84 }
85 },
86 video: {
6de36768
C
87 image: {
88 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
89 size: {
90 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
91 }
92 },
01de67b9
C
93 file: {
94 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
95 }
1869c875 96 },
40e87e9e
C
97 videoCaption: {
98 file: {
99 size: {
100 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
101 },
102 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
103 }
104 },
1869c875 105 user: {
bee0abff
FA
106 videoQuota: CONFIG.USER.VIDEO_QUOTA,
107 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
65fcc311 108 }
eb080476 109 }
6a84aafd 110
eb080476 111 return res.json(json)
65fcc311
C
112}
113
36f9424f
C
114function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
115 const about: About = {
116 instance: {
117 name: CONFIG.INSTANCE.NAME,
2e3a0215 118 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
36f9424f
C
119 description: CONFIG.INSTANCE.DESCRIPTION,
120 terms: CONFIG.INSTANCE.TERMS
121 }
122 }
123
124 return res.json(about).end()
125}
126
fd206f0b
C
127async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
128 const data = customConfig()
129
130 return res.json(data).end()
131}
132
133async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
62689b94 134 await remove(CONFIG.CUSTOM_FILE)
fd206f0b 135
80e36cd9
AB
136 auditLogger.delete(
137 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
138 new CustomConfigAuditView(customConfig())
139 )
140
fd206f0b 141 reloadConfig()
e032aec9 142 ClientHtml.invalidCache()
fd206f0b
C
143
144 const data = customConfig()
145
146 return res.json(data).end()
147}
148
149async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
150 const toUpdate: CustomConfig = req.body
80e36cd9 151 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
fd206f0b 152
be1fc4bc
C
153 // Force number conversion
154 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
40e87e9e 155 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
be1fc4bc
C
156 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
157 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
bee0abff 158 toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
be1fc4bc
C
159 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
160
161 // camelCase to snake_case key
40e87e9e 162 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
fd206f0b 163 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
901637bb 164 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
2e3a0215 165 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
0883b324 166 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
fd206f0b 167
62689b94 168 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
fd206f0b
C
169
170 reloadConfig()
e032aec9 171 ClientHtml.invalidCache()
fd206f0b
C
172
173 const data = customConfig()
80e36cd9
AB
174
175 auditLogger.update(
176 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
177 new CustomConfigAuditView(data),
178 oldCustomConfigAuditKeys
179 )
180
fd206f0b
C
181 return res.json(data).end()
182}
183
65fcc311
C
184// ---------------------------------------------------------------------------
185
186export {
187 configRouter
188}
fd206f0b
C
189
190// ---------------------------------------------------------------------------
191
192function customConfig (): CustomConfig {
193 return {
66b16caf
C
194 instance: {
195 name: CONFIG.INSTANCE.NAME,
2e3a0215 196 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
66b16caf 197 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c 198 terms: CONFIG.INSTANCE.TERMS,
901637bb 199 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 200 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
201 customizations: {
202 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
203 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
204 }
66b16caf 205 },
8be1afa1
C
206 services: {
207 twitter: {
208 username: CONFIG.SERVICES.TWITTER.USERNAME,
209 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
210 }
211 },
fd206f0b
C
212 cache: {
213 previews: {
214 size: CONFIG.CACHE.PREVIEWS.SIZE
40e87e9e
C
215 },
216 captions: {
217 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
fd206f0b
C
218 }
219 },
220 signup: {
221 enabled: CONFIG.SIGNUP.ENABLED,
222 limit: CONFIG.SIGNUP.LIMIT
223 },
224 admin: {
225 email: CONFIG.ADMIN.EMAIL
226 },
227 user: {
bee0abff
FA
228 videoQuota: CONFIG.USER.VIDEO_QUOTA,
229 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
fd206f0b
C
230 },
231 transcoding: {
232 enabled: CONFIG.TRANSCODING.ENABLED,
233 threads: CONFIG.TRANSCODING.THREADS,
234 resolutions: {
235 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
236 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
237 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
238 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
239 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
240 }
5d08a6a7
C
241 },
242 import: {
243 videos: {
244 http: {
245 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
a84b8fa5
C
246 },
247 torrent: {
248 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
5d08a6a7
C
249 }
250 }
fd206f0b
C
251 }
252 }
253}