]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/config.ts
Add ability to disable webtorrent
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / config.ts
CommitLineData
fd206f0b 1import * as express from 'express'
c8861d5d 2import { body } from 'express-validator'
7cd4d2ba 3import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users'
fd206f0b 4import { logger } from '../../helpers/logger'
576ad67a
JM
5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6import { Emailer } from '../../lib/emailer'
fd206f0b 7import { areValidationErrors } from './utils'
503c6f44
C
8import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
9import { isThemeRegistered } from '../../lib/plugins/theme-utils'
fd206f0b
C
10
11const customConfigUpdateValidator = [
901637bb 12 body('instance.name').exists().withMessage('Should have a valid instance name'),
a4101923 13 body('instance.shortDescription').exists().withMessage('Should have a valid instance short description'),
901637bb
C
14 body('instance.description').exists().withMessage('Should have a valid instance description'),
15 body('instance.terms').exists().withMessage('Should have a valid instance terms'),
16 body('instance.defaultClientRoute').exists().withMessage('Should have a valid instance default client route'),
0883b324 17 body('instance.defaultNSFWPolicy').custom(isUserNSFWPolicyValid).withMessage('Should have a valid NSFW policy'),
901637bb
C
18 body('instance.customizations.css').exists().withMessage('Should have a valid instance CSS customization'),
19 body('instance.customizations.javascript').exists().withMessage('Should have a valid instance JavaScript customization'),
a4101923
C
20
21 body('services.twitter.username').exists().withMessage('Should have a valid twitter username'),
22 body('services.twitter.whitelisted').isBoolean().withMessage('Should have a valid twitter whitelisted boolean'),
23
24 body('cache.previews.size').isInt().withMessage('Should have a valid previews cache size'),
25 body('cache.captions.size').isInt().withMessage('Should have a valid captions cache size'),
26
fd206f0b
C
27 body('signup.enabled').isBoolean().withMessage('Should have a valid signup enabled boolean'),
28 body('signup.limit').isInt().withMessage('Should have a valid signup limit'),
a4101923
C
29 body('signup.requiresEmailVerification').isBoolean().withMessage('Should have a valid requiresEmailVerification boolean'),
30
fd206f0b 31 body('admin.email').isEmail().withMessage('Should have a valid administrator email'),
a4101923
C
32 body('contactForm.enabled').isBoolean().withMessage('Should have a valid contact form enabled boolean'),
33
fd206f0b 34 body('user.videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid video quota'),
a4101923
C
35 body('user.videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily video quota'),
36
fd206f0b 37 body('transcoding.enabled').isBoolean().withMessage('Should have a valid transcoding enabled boolean'),
a4101923 38 body('transcoding.allowAdditionalExtensions').isBoolean().withMessage('Should have a valid additional extensions boolean'),
fd206f0b
C
39 body('transcoding.threads').isInt().withMessage('Should have a valid transcoding threads number'),
40 body('transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
41 body('transcoding.resolutions.360p').isBoolean().withMessage('Should have a valid transcoding 360p resolution enabled boolean'),
42 body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'),
43 body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'),
44 body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'),
a4101923 45
d7a25329
C
46 body('transcoding.webtorrent.enabled').isBoolean().withMessage('Should have a valid webtorrent transcoding enabled boolean'),
47 body('transcoding.hls.enabled').isBoolean().withMessage('Should have a valid webtorrent transcoding enabled boolean'),
48
5d08a6a7 49 body('import.videos.http.enabled').isBoolean().withMessage('Should have a valid import video http enabled boolean'),
a84b8fa5 50 body('import.videos.torrent.enabled').isBoolean().withMessage('Should have a valid import video torrent enabled boolean'),
fd206f0b 51
14893eb7
C
52 body('followers.instance.enabled').isBoolean().withMessage('Should have a valid followers of instance boolean'),
53 body('followers.instance.manualApproval').isBoolean().withMessage('Should have a valid manual approval boolean'),
54
503c6f44 55 body('theme.default').custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
7cd4d2ba 56
fd206f0b
C
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body })
59
60 if (areValidationErrors(req, res)) return
576ad67a 61 if (!checkInvalidConfigIfEmailDisabled(req.body as CustomConfig, res)) return
d7a25329 62 if (!checkInvalidTranscodingConfig(req.body as CustomConfig, res)) return
fd206f0b
C
63
64 return next()
65 }
66]
67
576ad67a
JM
68// ---------------------------------------------------------------------------
69
fd206f0b
C
70export {
71 customConfigUpdateValidator
72}
576ad67a
JM
73
74function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) {
75 if (Emailer.isEnabled()) return true
76
77 if (customConfig.signup.requiresEmailVerification === true) {
78 res.status(400)
79 .send({ error: 'Emailer is disabled but you require signup email verification.' })
80 .end()
81 return false
82 }
83
84 return true
85}
d7a25329
C
86
87function checkInvalidTranscodingConfig (customConfig: CustomConfig, res: express.Response) {
88 if (customConfig.transcoding.enabled === false) return true
89
90 if (customConfig.transcoding.webtorrent.enabled === false && customConfig.transcoding.hls.enabled === false) {
91 res.status(400)
92 .send({ error: 'You need to enable at least webtorrent transcoding or hls transcoding' })
93 .end()
94 return false
95 }
96
97 return true
98}