const middlewares = require('../../../middlewares')
const admin = middlewares.admin
const oAuth = middlewares.oauth
+const podsMiddleware = middlewares.pods
const checkSignature = middlewares.secure.checkSignature
const validators = middlewares.validators.pods
const signatureValidator = middlewares.validators.remote.signature
const Video = mongoose.model('Video')
router.get('/', listPods)
-router.post('/', validators.podsAdd, addPods)
+router.post('/',
+ validators.podsAdd,
+ podsMiddleware.setBodyUrlPort,
+ addPods
+)
router.post('/makefriends',
oAuth.authenticate,
admin.ensureIsAdmin,
validators.makeFriends,
+ podsMiddleware.setBodyUrlsPort,
makeFriends
)
router.get('/quitfriends',
const adminMiddleware = require('./admin')
const oauthMiddleware = require('./oauth')
const paginationMiddleware = require('./pagination')
+const podsMiddleware = require('./pods')
const validatorsMiddleware = require('./validators')
const searchMiddleware = require('./search')
const sortMiddleware = require('./sort')
admin: adminMiddleware,
oauth: oauthMiddleware,
pagination: paginationMiddleware,
+ pods: podsMiddleware,
search: searchMiddleware,
secure: secureMiddleware,
sort: sortMiddleware,
--- /dev/null
+'use strict'
+
+const urlModule = require('url')
+
+const logger = require('../helpers/logger')
+
+const podsMiddleware = {
+ setBodyUrlsPort: setBodyUrlsPort,
+ setBodyUrlPort: setBodyUrlPort
+}
+
+function setBodyUrlsPort (req, res, next) {
+ for (let i = 0; i < req.body.urls.length; i++) {
+ const urlWithPort = getUrlWithPort(req.body.urls[i])
+
+ // Problem with the url parsing?
+ if (urlWithPort === null) {
+ return res.sendStatus(500)
+ }
+
+ req.body.urls[i] = urlWithPort
+ }
+
+ return next()
+}
+
+function setBodyUrlPort (req, res, next) {
+ const urlWithPort = getUrlWithPort(req.body.url)
+
+ // Problem with the url parsing?
+ if (urlWithPort === null) {
+ return res.sendStatus(500)
+ }
+
+ req.body.url = urlWithPort
+
+ return next()
+}
+
+// ---------------------------------------------------------------------------
+
+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
+ }
+ }
+
+ return url
+}