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