Server: optimization for videoGet and videoRemove
[oweals/peertube.git] / server / middlewares / pods.js
1 'use strict'
2
3 const constants = require('../initializers/constants')
4
5 const podsMiddleware = {
6   setBodyHostsPort,
7   setBodyHostPort
8 }
9
10 function setBodyHostsPort (req, res, next) {
11   for (let i = 0; i < req.body.hosts.length; i++) {
12     const hostWithPort = getHostWithPort(req.body.hosts[i])
13
14     // Problem with the url parsing?
15     if (hostWithPort === null) {
16       return res.sendStatus(500)
17     }
18
19     req.body.hosts[i] = hostWithPort
20   }
21
22   return next()
23 }
24
25 function setBodyHostPort (req, res, next) {
26   const hostWithPort = getHostWithPort(req.body.host)
27
28   // Problem with the url parsing?
29   if (hostWithPort === null) {
30     return res.sendStatus(500)
31   }
32
33   req.body.host = hostWithPort
34
35   return next()
36 }
37
38 // ---------------------------------------------------------------------------
39
40 module.exports = podsMiddleware
41
42 // ---------------------------------------------------------------------------
43
44 function getHostWithPort (host) {
45   const splitted = host.split(':')
46
47   // The port was not specified
48   if (splitted.length === 1) {
49     if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
50
51     return host + ':80'
52   }
53
54   return host
55 }