]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/pods.js
Server: finish old jobs at startup
[github/Chocobozzz/PeerTube.git] / server / middlewares / pods.js
index 6e0874a7699833c0d8ed7804c90926c8c1aa61f9..2647f9ff0b342849007fbca19cfcb552b72ee339 100644 (file)
@@ -1,38 +1,40 @@
 'use strict'
 
-const urlModule = require('url')
-
-const logger = require('../helpers/logger')
+const constants = require('../initializers/constants')
 
 const podsMiddleware = {
-  setBodyUrlsPort,
-  setBodyUrlPort
+  setBodyHostsPort,
+  setBodyHostPort
 }
 
-function setBodyUrlsPort (req, res, next) {
-  for (let i = 0; i < req.body.urls.length; i++) {
-    const urlWithPort = getUrlWithPort(req.body.urls[i])
+function setBodyHostsPort (req, res, next) {
+  if (!req.body.hosts) return next()
+
+  for (let i = 0; i < req.body.hosts.length; i++) {
+    const hostWithPort = getHostWithPort(req.body.hosts[i])
 
     // Problem with the url parsing?
-    if (urlWithPort === null) {
+    if (hostWithPort === null) {
       return res.sendStatus(500)
     }
 
-    req.body.urls[i] = urlWithPort
+    req.body.hosts[i] = hostWithPort
   }
 
   return next()
 }
 
-function setBodyUrlPort (req, res, next) {
-  const urlWithPort = getUrlWithPort(req.body.url)
+function setBodyHostPort (req, res, next) {
+  if (!req.body.host) return next()
+
+  const hostWithPort = getHostWithPort(req.body.host)
 
   // Problem with the url parsing?
-  if (urlWithPort === null) {
+  if (hostWithPort === null) {
     return res.sendStatus(500)
   }
 
-  req.body.url = urlWithPort
+  req.body.host = hostWithPort
 
   return next()
 }
@@ -43,20 +45,15 @@ module.exports = podsMiddleware
 
 // ---------------------------------------------------------------------------
 
-function getUrlWithPort (url) {
-  const urlObj = urlModule.parse(url)
-
-  // Add the port if it is not specified
-  if (urlObj.port === null) {
-    if (urlObj.protocol === 'http:') {
-      return url + ':80'
-    } else if (urlObj.protocol === 'https:') {
-      return url + ':443'
-    } else {
-      logger.error('Unknown url protocol: ' + urlObj.protocol)
-      return null
-    }
+function getHostWithPort (host) {
+  const splitted = host.split(':')
+
+  // The port was not specified
+  if (splitted.length === 1) {
+    if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
+
+    return host + ':80'
   }
 
-  return url
+  return host
 }