aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/pods.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-09-15 12:17:08 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-09-15 12:17:08 +0200
commitb60e5f38daf77e720a27aa86d3b482c58906a03a (patch)
treea70909860ab9705d348b3c082f8af440e0a5e4d2 /server/middlewares/validators/pods.ts
parent315cc0cc1871ab2a6d6c1bb61cf7b9f10511c3a9 (diff)
downloadPeerTube-b60e5f38daf77e720a27aa86d3b482c58906a03a.tar.gz
PeerTube-b60e5f38daf77e720a27aa86d3b482c58906a03a.tar.zst
PeerTube-b60e5f38daf77e720a27aa86d3b482c58906a03a.zip
Upgrade express validator to v4
Diffstat (limited to 'server/middlewares/validators/pods.ts')
-rw-r--r--server/middlewares/validators/pods.ts155
1 files changed, 81 insertions, 74 deletions
diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts
index 3a0f56f6a..ab7702e78 100644
--- a/server/middlewares/validators/pods.ts
+++ b/server/middlewares/validators/pods.ts
@@ -1,89 +1,96 @@
1import 'express-validator' 1import { body, param } from 'express-validator/check'
2import * as express from 'express' 2import * as express from 'express'
3 3
4import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils' 5import { checkErrors } from './utils'
6import { logger } from '../../helpers' 6import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers'
7import { CONFIG } from '../../initializers' 7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib' 8import { hasFriends } from '../../lib'
9import { isTestInstance } from '../../helpers' 9import { isTestInstance } from '../../helpers'
10 10
11function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) { 11const makeFriendsValidator = [
12 // Force https if the administrator wants to make friends 12 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { 13
14 return res.status(400) 14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 .json({ 15 // Force https if the administrator wants to make friends
16 error: 'Cannot make friends with a non HTTPS web server.' 16 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
17 }) 17 return res.status(400)
18 .end() 18 .json({
19 error: 'Cannot make friends with a non HTTPS web server.'
20 })
21 .end()
22 }
23
24 logger.debug('Checking makeFriends parameters', { parameters: req.body })
25
26 checkErrors(req, res, () => {
27 hasFriends()
28 .then(heHasFriends => {
29 if (heHasFriends === true) {
30 // We need to quit our friends before make new ones
31 return res.sendStatus(409)
32 }
33
34 return next()
35 })
36 .catch(err => {
37 logger.error('Cannot know if we have friends.', err)
38 res.sendStatus(500)
39 })
40 })
19 } 41 }
42]
20 43
21 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid() 44const podsAddValidator = [
22 45 body('host').custom(isHostValid).withMessage('Should have a host'),
23 logger.debug('Checking makeFriends parameters', { parameters: req.body }) 46 body('email').isEmail().withMessage('Should have an email'),
24 47 body('publicKey').not().isEmpty().withMessage('Should have a public key'),
25 checkErrors(req, res, () => {
26 hasFriends()
27 .then(heHasFriends => {
28 if (heHasFriends === true) {
29 // We need to quit our friends before make new ones
30 return res.sendStatus(409)
31 }
32
33 return next()
34 })
35 .catch(err => {
36 logger.error('Cannot know if we have friends.', err)
37 res.sendStatus(500)
38 })
39 })
40}
41 48
42function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { 49 (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 req.checkBody('host', 'Should have a host').isHostValid() 50 logger.debug('Checking podsAdd parameters', { parameters: req.body })
44 req.checkBody('email', 'Should have an email').isEmail()
45 req.checkBody('publicKey', 'Should have a public key').notEmpty()
46 logger.debug('Checking podsAdd parameters', { parameters: req.body })
47
48 checkErrors(req, res, () => {
49 db.Pod.loadByHost(req.body.host)
50 .then(pod => {
51 // Pod with this host already exists
52 if (pod) {
53 return res.sendStatus(409)
54 }
55
56 return next()
57 })
58 .catch(err => {
59 logger.error('Cannot load pod by host.', err)
60 res.sendStatus(500)
61 })
62 })
63}
64 51
65function podRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { 52 checkErrors(req, res, () => {
66 req.checkParams('id', 'Should have a valid id').notEmpty().isNumeric() 53 db.Pod.loadByHost(req.body.host)
67 54 .then(pod => {
68 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params }) 55 // Pod with this host already exists
69 56 if (pod) {
70 checkErrors(req, res, function () { 57 return res.sendStatus(409)
71 db.Pod.load(req.params.id) 58 }
72 .then(pod => { 59
73 if (!pod) { 60 return next()
74 logger.error('Cannot find pod %d.', req.params.id) 61 })
75 return res.sendStatus(404) 62 .catch(err => {
76 } 63 logger.error('Cannot load pod by host.', err)
77 64 res.sendStatus(500)
78 res.locals.pod = pod 65 })
79 return next() 66 })
80 }) 67 }
81 .catch(err => { 68]
82 logger.error('Cannot load pod %d.', req.params.id, err) 69
83 res.sendStatus(500) 70const podRemoveValidator = [
84 }) 71 param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
85 }) 72
86} 73 (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
75
76 checkErrors(req, res, () => {
77 db.Pod.load(req.params.id)
78 .then(pod => {
79 if (!pod) {
80 logger.error('Cannot find pod %d.', req.params.id)
81 return res.sendStatus(404)
82 }
83
84 res.locals.pod = pod
85 return next()
86 })
87 .catch(err => {
88 logger.error('Cannot load pod %d.', req.params.id, err)
89 res.sendStatus(500)
90 })
91 })
92 }
93]
87 94
88// --------------------------------------------------------------------------- 95// ---------------------------------------------------------------------------
89 96