aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-04 21:16:27 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-04 21:23:06 +0100
commita5fa04b0cce851f680a45055f57ed2c3d22f1c82 (patch)
tree0749c4d6889d7cf250110f9ff59a8cd22ee8a480
parentc173e56520b0fe4206b9ea8049b6add40bfeabcd (diff)
downloadPeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.tar.gz
PeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.tar.zst
PeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.zip
Split misc middleware
-rw-r--r--controllers/api/v1/pods.js13
-rw-r--r--controllers/api/v1/remoteVideos.js11
-rw-r--r--controllers/api/v1/videos.js12
-rw-r--r--controllers/views.js6
-rw-r--r--middlewares/cache.js25
-rw-r--r--middlewares/index.js5
-rw-r--r--middlewares/secure.js (renamed from middlewares/misc.js)19
7 files changed, 52 insertions, 39 deletions
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js
index 82d8d7f08..3e457ec57 100644
--- a/controllers/api/v1/pods.js
+++ b/controllers/api/v1/pods.js
@@ -7,21 +7,22 @@
7 var logger = require('../../../helpers/logger') 7 var logger = require('../../../helpers/logger')
8 var friends = require('../../../lib/friends') 8 var friends = require('../../../lib/friends')
9 var middleware = require('../../../middlewares') 9 var middleware = require('../../../middlewares')
10 var miscMiddleware = middleware.misc 10 var cacheMiddleware = middleware.cache
11 var Pods = require('../../../models/pods') 11 var Pods = require('../../../models/pods')
12 var reqValidator = middleware.reqValidators.pods 12 var reqValidator = middleware.reqValidators.pods
13 var secureMiddleware = middleware.secure
13 var secureRequest = middleware.reqValidators.remote.secureRequest 14 var secureRequest = middleware.reqValidators.remote.secureRequest
14 var utils = require('../../../helpers/utils') 15 var utils = require('../../../helpers/utils')
15 var Videos = require('../../../models/videos') 16 var Videos = require('../../../models/videos')
16 17
17 var router = express.Router() 18 var router = express.Router()
18 19
19 router.get('/', miscMiddleware.cache(false), listPods) 20 router.get('/', cacheMiddleware.cache(false), listPods)
20 router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods) 21 router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
21 router.get('/makefriends', reqValidator.makeFriends, miscMiddleware.cache(false), makeFriends) 22 router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
22 router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) 23 router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
23 // Post because this is a secured request 24 // Post because this is a secured request
24 router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods) 25 router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
25 26
26 // --------------------------------------------------------------------------- 27 // ---------------------------------------------------------------------------
27 28
diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js
index d72db9836..58bb5f3cb 100644
--- a/controllers/api/v1/remoteVideos.js
+++ b/controllers/api/v1/remoteVideos.js
@@ -5,7 +5,8 @@
5 var pluck = require('lodash-node/compat/collection/pluck') 5 var pluck = require('lodash-node/compat/collection/pluck')
6 6
7 var middleware = require('../../../middlewares') 7 var middleware = require('../../../middlewares')
8 var miscMiddleware = middleware.misc 8 var secureMiddleware = middleware.secure
9 var cacheMiddleware = middleware.cache
9 var reqValidator = middleware.reqValidators.remote 10 var reqValidator = middleware.reqValidators.remote
10 var videos = require('../../../models/videos') 11 var videos = require('../../../models/videos')
11 12
@@ -13,17 +14,17 @@
13 14
14 router.post('/add', 15 router.post('/add',
15 reqValidator.secureRequest, 16 reqValidator.secureRequest,
16 miscMiddleware.decryptBody, 17 secureMiddleware.decryptBody,
17 reqValidator.remoteVideosAdd, 18 reqValidator.remoteVideosAdd,
18 miscMiddleware.cache(false), 19 cacheMiddleware.cache(false),
19 addRemoteVideos 20 addRemoteVideos
20 ) 21 )
21 22
22 router.post('/remove', 23 router.post('/remove',
23 reqValidator.secureRequest, 24 reqValidator.secureRequest,
24 miscMiddleware.decryptBody, 25 secureMiddleware.decryptBody,
25 reqValidator.remoteVideosRemove, 26 reqValidator.remoteVideosRemove,
26 miscMiddleware.cache(false), 27 cacheMiddleware.cache(false),
27 removeRemoteVideo 28 removeRemoteVideo
28 ) 29 )
29 30
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js
index d2e7e8825..7792059ca 100644
--- a/controllers/api/v1/videos.js
+++ b/controllers/api/v1/videos.js
@@ -9,7 +9,7 @@
9 var logger = require('../../../helpers/logger') 9 var logger = require('../../../helpers/logger')
10 var friends = require('../../../lib/friends') 10 var friends = require('../../../lib/friends')
11 var middleware = require('../../../middlewares') 11 var middleware = require('../../../middlewares')
12 var miscMiddleware = middleware.misc 12 var cacheMiddleware = middleware.cache
13 var reqValidator = middleware.reqValidators.videos 13 var reqValidator = middleware.reqValidators.videos
14 var Videos = require('../../../models/videos') // model 14 var Videos = require('../../../models/videos') // model
15 var videos = require('../../../lib/videos') 15 var videos = require('../../../lib/videos')
@@ -38,11 +38,11 @@
38 38
39 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) 39 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
40 40
41 router.get('/', miscMiddleware.cache(false), listVideos) 41 router.get('/', cacheMiddleware.cache(false), listVideos)
42 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideo) 42 router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo)
43 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos) 43 router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos)
44 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo) 44 router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo)
45 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos) 45 router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos)
46 46
47 // --------------------------------------------------------------------------- 47 // ---------------------------------------------------------------------------
48 48
diff --git a/controllers/views.js b/controllers/views.js
index 1bb4ffe70..82d3d00ab 100644
--- a/controllers/views.js
+++ b/controllers/views.js
@@ -3,12 +3,12 @@
3 3
4 var express = require('express') 4 var express = require('express')
5 5
6 var middleware = require('../middlewares').misc 6 var cacheMiddleware = require('../middlewares').cache
7 7
8 var router = express.Router() 8 var router = express.Router()
9 9
10 router.get(/^\/(index)?$/, middleware.cache(), getIndex) 10 router.get(/^\/(index)?$/, cacheMiddleware.cache(), getIndex)
11 router.get('/partials/:directory/:name', middleware.cache(), getPartial) 11 router.get('/partials/:directory/:name', cacheMiddleware.cache(), getPartial)
12 12
13 // --------------------------------------------------------------------------- 13 // ---------------------------------------------------------------------------
14 14
diff --git a/middlewares/cache.js b/middlewares/cache.js
new file mode 100644
index 000000000..782165155
--- /dev/null
+++ b/middlewares/cache.js
@@ -0,0 +1,25 @@
1;(function () {
2 'use strict'
3
4 var cacheMiddleware = {
5 cache: cache
6 }
7
8 function cache (cache) {
9 return function (req, res, next) {
10 // If we want explicitly a cache
11 // Or if we don't specify if we want a cache or no and we are in production
12 if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) {
13 res.setHeader('Cache-Control', 'public')
14 } else {
15 res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
16 }
17
18 next()
19 }
20 }
21
22 // ---------------------------------------------------------------------------
23
24 module.exports = cacheMiddleware
25})()
diff --git a/middlewares/index.js b/middlewares/index.js
index 311dfb6d2..bfe325b1e 100644
--- a/middlewares/index.js
+++ b/middlewares/index.js
@@ -2,8 +2,9 @@
2 'use strict' 2 'use strict'
3 3
4 var middlewares = { 4 var middlewares = {
5 misc: require('./misc'), 5 cache: require('./cache'),
6 reqValidators: require('./reqValidators') 6 reqValidators: require('./reqValidators'),
7 secure: require('./secure')
7 } 8 }
8 9
9 // --------------------------------------------------------------------------- 10 // ---------------------------------------------------------------------------
diff --git a/middlewares/misc.js b/middlewares/secure.js
index cc4e2e8a4..99ac9cdae 100644
--- a/middlewares/misc.js
+++ b/middlewares/secure.js
@@ -8,25 +8,10 @@
8 var Pods = require('../models/pods') 8 var Pods = require('../models/pods')
9 var utils = require('../helpers/utils') 9 var utils = require('../helpers/utils')
10 10
11 var miscMiddleware = { 11 var secureMiddleware = {
12 cache: cache,
13 decryptBody: decryptBody 12 decryptBody: decryptBody
14 } 13 }
15 14
16 function cache (cache) {
17 return function (req, res, next) {
18 // If we want explicitly a cache
19 // Or if we don't specify if we want a cache or no and we are in production
20 if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) {
21 res.setHeader('Cache-Control', 'public')
22 } else {
23 res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
24 }
25
26 next()
27 }
28 }
29
30 function decryptBody (req, res, next) { 15 function decryptBody (req, res, next) {
31 var url = req.body.signature.url 16 var url = req.body.signature.url
32 Pods.findByUrl(url, function (err, pod) { 17 Pods.findByUrl(url, function (err, pod) {
@@ -61,5 +46,5 @@
61 46
62 // --------------------------------------------------------------------------- 47 // ---------------------------------------------------------------------------
63 48
64 module.exports = miscMiddleware 49 module.exports = secureMiddleware
65})() 50})()