]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Cleanup utils helper
[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'
fd206f0b 6import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
06215f15 7import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
fd206f0b
C
8import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10import { customConfigUpdateValidator } from '../../middlewares/validators/config'
e032aec9 11import { ClientHtml } from '../../lib/client-html'
a84b8fa5 12import { auditLoggerFactory, CustomConfigAuditView } from '../../helpers/audit-logger'
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
RK
105 user: {
106 videoQuota: CONFIG.USER.VIDEO_QUOTA
65fcc311 107 }
eb080476 108 }
6a84aafd 109
eb080476 110 return res.json(json)
65fcc311
C
111}
112
36f9424f
C
113function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
114 const about: About = {
115 instance: {
116 name: CONFIG.INSTANCE.NAME,
2e3a0215 117 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
36f9424f
C
118 description: CONFIG.INSTANCE.DESCRIPTION,
119 terms: CONFIG.INSTANCE.TERMS
120 }
121 }
122
123 return res.json(about).end()
124}
125
fd206f0b
C
126async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
127 const data = customConfig()
128
129 return res.json(data).end()
130}
131
132async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
133 await unlinkPromise(CONFIG.CUSTOM_FILE)
134
80e36cd9
AB
135 auditLogger.delete(
136 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
137 new CustomConfigAuditView(customConfig())
138 )
139
fd206f0b 140 reloadConfig()
e032aec9 141 ClientHtml.invalidCache()
fd206f0b
C
142
143 const data = customConfig()
144
145 return res.json(data).end()
146}
147
148async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
149 const toUpdate: CustomConfig = req.body
80e36cd9 150 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
fd206f0b 151
be1fc4bc
C
152 // Force number conversion
153 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
40e87e9e 154 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
be1fc4bc
C
155 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
156 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
157 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
158
159 // camelCase to snake_case key
40e87e9e 160 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
fd206f0b 161 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
901637bb 162 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
2e3a0215 163 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
0883b324 164 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
fd206f0b 165
2ad42952 166 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
fd206f0b
C
167
168 reloadConfig()
e032aec9 169 ClientHtml.invalidCache()
fd206f0b
C
170
171 const data = customConfig()
80e36cd9
AB
172
173 auditLogger.update(
174 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
175 new CustomConfigAuditView(data),
176 oldCustomConfigAuditKeys
177 )
178
fd206f0b
C
179 return res.json(data).end()
180}
181
65fcc311
C
182// ---------------------------------------------------------------------------
183
184export {
185 configRouter
186}
fd206f0b
C
187
188// ---------------------------------------------------------------------------
189
190function customConfig (): CustomConfig {
191 return {
66b16caf
C
192 instance: {
193 name: CONFIG.INSTANCE.NAME,
2e3a0215 194 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
66b16caf 195 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c 196 terms: CONFIG.INSTANCE.TERMS,
901637bb 197 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 198 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
199 customizations: {
200 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
201 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
202 }
66b16caf 203 },
8be1afa1
C
204 services: {
205 twitter: {
206 username: CONFIG.SERVICES.TWITTER.USERNAME,
207 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
208 }
209 },
fd206f0b
C
210 cache: {
211 previews: {
212 size: CONFIG.CACHE.PREVIEWS.SIZE
40e87e9e
C
213 },
214 captions: {
215 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
fd206f0b
C
216 }
217 },
218 signup: {
219 enabled: CONFIG.SIGNUP.ENABLED,
220 limit: CONFIG.SIGNUP.LIMIT
221 },
222 admin: {
223 email: CONFIG.ADMIN.EMAIL
224 },
225 user: {
226 videoQuota: CONFIG.USER.VIDEO_QUOTA
227 },
228 transcoding: {
229 enabled: CONFIG.TRANSCODING.ENABLED,
230 threads: CONFIG.TRANSCODING.THREADS,
231 resolutions: {
232 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
233 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
234 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
235 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
236 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
237 }
5d08a6a7
C
238 },
239 import: {
240 videos: {
241 http: {
242 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
a84b8fa5
C
243 },
244 torrent: {
245 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
5d08a6a7
C
246 }
247 }
fd206f0b
C
248 }
249 }
250}