]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/pods.ts
Remove ng2 file upload module
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
... / ...
CommitLineData
1import 'express-validator'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib'
9import { isTestInstance } from '../../helpers'
10
11function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
12 // Force https if the administrator wants to make friends
13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
14 return res.status(400)
15 .json({
16 error: 'Cannot make friends with a non HTTPS web server.'
17 })
18 .end()
19 }
20
21 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
22
23 logger.debug('Checking makeFriends parameters', { parameters: req.body })
24
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
42function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
43 req.checkBody('host', 'Should have a host').isHostValid()
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
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
88// ---------------------------------------------------------------------------
89
90export {
91 makeFriendsValidator,
92 podsAddValidator,
93 podRemoveValidator
94}