]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/pods.ts
Remove ng2 file upload module
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
e02643f3 4import { database as db } from '../../initializers/database'
65fcc311
C
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib'
9import { isTestInstance } from '../../helpers'
9f10b292 10
69818c93 11function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
441b66f8 12 // Force https if the administrator wants to make friends
65fcc311 13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
bfb3a98f
C
14 return res.status(400)
15 .json({
16 error: 'Cannot make friends with a non HTTPS web server.'
17 })
18 .end()
441b66f8
C
19 }
20
49abbbbe 21 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
1e2564d3
C
22
23 logger.debug('Checking makeFriends parameters', { parameters: req.body })
24
075f16ca 25 checkErrors(req, res, () => {
6fcd19ba
C
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 => {
ad0997ad 36 logger.error('Cannot know if we have friends.', err)
d57d6f26 37 res.sendStatus(500)
6fcd19ba 38 })
9f10b292
C
39 })
40}
41
69818c93 42function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
4793c343
C
43 req.checkBody('host', 'Should have a host').isHostValid()
44 req.checkBody('email', 'Should have an email').isEmail()
528a9efa 45 req.checkBody('publicKey', 'Should have a public key').notEmpty()
b09ce645 46 logger.debug('Checking podsAdd parameters', { parameters: req.body })
528a9efa 47
075f16ca 48 checkErrors(req, res, () => {
6fcd19ba
C
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 => {
ad0997ad 59 logger.error('Cannot load pod by host.', err)
b09ce645 60 res.sendStatus(500)
6fcd19ba 61 })
b09ce645 62 })
9f10b292
C
63}
64
d5f5a670
GS
65function podRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
66 req.checkParams('id', 'Should have a valid id').notEmpty().isNumeric()
67
68 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
69
70 checkErrors(req, res, function () {
71 db.Pod.load(req.params.id)
72 .then(pod => {
73 if (!pod) {
74 logger.error('Cannot find pod %d.', req.params.id)
75 return res.sendStatus(404)
76 }
77
78 res.locals.pod = pod
79 return next()
80 })
81 .catch(err => {
82 logger.error('Cannot load pod %d.', req.params.id, err)
83 res.sendStatus(500)
84 })
85 })
86}
87
9f10b292
C
88// ---------------------------------------------------------------------------
89
65fcc311
C
90export {
91 makeFriendsValidator,
d5f5a670
GS
92 podsAddValidator,
93 podRemoveValidator
65fcc311 94}