aboutsummaryrefslogtreecommitdiffhomepage
path: root/controllers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-01-31 11:23:52 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-01-31 11:23:52 +0100
commitc45f7f84001c2731909db04dd82e1c1f290386eb (patch)
treeb7e57420a1f65dfbbacc1a532bf489c9bea6125d /controllers
parentcda021079ff455cc0fd0eb95a5395fa808ab63d1 (diff)
downloadPeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.gz
PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.zst
PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.zip
Infile code reorganization
Diffstat (limited to 'controllers')
-rw-r--r--controllers/api/v1/index.js7
-rw-r--r--controllers/api/v1/pods.js46
-rw-r--r--controllers/api/v1/remoteVideos.js30
-rw-r--r--controllers/api/v1/videos.js83
-rw-r--r--controllers/index.js4
-rw-r--r--controllers/views.js29
6 files changed, 117 insertions, 82 deletions
diff --git a/controllers/api/v1/index.js b/controllers/api/v1/index.js
index f5504ad85..b16eeb0f6 100644
--- a/controllers/api/v1/index.js
+++ b/controllers/api/v1/index.js
@@ -2,11 +2,14 @@
2 'use strict' 2 'use strict'
3 3
4 var express = require('express') 4 var express = require('express')
5
5 var router = express.Router() 6 var router = express.Router()
6 7
7 router.use('/videos', require('./videos'))
8 router.use('/remotevideos', require('./remoteVideos'))
9 router.use('/pods', require('./pods')) 8 router.use('/pods', require('./pods'))
9 router.use('/remotevideos', require('./remoteVideos'))
10 router.use('/videos', require('./videos'))
11
12 // ---------------------------------------------------------------------------
10 13
11 module.exports = router 14 module.exports = router
12})() 15})()
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js
index 30385bd5a..b073e85af 100644
--- a/controllers/api/v1/pods.js
+++ b/controllers/api/v1/pods.js
@@ -2,20 +2,27 @@
2 'use strict' 2 'use strict'
3 3
4 var express = require('express') 4 var express = require('express')
5 var router = express.Router() 5
6 var middleware = require('../../../middlewares') 6 var middleware = require('../../../middlewares')
7 var miscMiddleware = middleware.misc 7 var miscMiddleware = middleware.misc
8 var pods = require('../../../models/pods')
8 var reqValidator = middleware.reqValidators.pods 9 var reqValidator = middleware.reqValidators.pods
9 var secureRequest = middleware.reqValidators.remote.secureRequest 10 var secureRequest = middleware.reqValidators.remote.secureRequest
10 var pods = require('../../../models/pods')
11 11
12 function listPods (req, res, next) { 12 var router = express.Router()
13 pods.list(function (err, pods_list) {
14 if (err) return next(err)
15 13
16 res.json(pods_list) 14 router.get('/', miscMiddleware.cache(false), listPods)
17 }) 15 router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods)
18 } 16 router.get('/makefriends', miscMiddleware.cache(false), makeFriends)
17 router.get('/quitfriends', miscMiddleware.cache(false), quitFriends)
18 // Post because this is a secured request
19 router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods)
20
21 // ---------------------------------------------------------------------------
22
23 module.exports = router
24
25 // ---------------------------------------------------------------------------
19 26
20 function addPods (req, res, next) { 27 function addPods (req, res, next) {
21 pods.add(req.body.data, function (err, json) { 28 pods.add(req.body.data, function (err, json) {
@@ -25,11 +32,11 @@
25 }) 32 })
26 } 33 }
27 34
28 function removePods (req, res, next) { 35 function listPods (req, res, next) {
29 pods.remove(req.body.signature.url, function (err) { 36 pods.list(function (err, pods_list) {
30 if (err) return next(err) 37 if (err) return next(err)
31 38
32 res.sendStatus(204) 39 res.json(pods_list)
33 }) 40 })
34 } 41 }
35 42
@@ -50,20 +57,19 @@
50 }) 57 })
51 } 58 }
52 59
53 function quitFriends (req, res, next) { 60 function removePods (req, res, next) {
54 pods.quitFriends(function (err) { 61 pods.remove(req.body.signature.url, function (err) {
55 if (err) return next(err) 62 if (err) return next(err)
56 63
57 res.sendStatus(204) 64 res.sendStatus(204)
58 }) 65 })
59 } 66 }
60 67
61 router.get('/', miscMiddleware.cache(false), listPods) 68 function quitFriends (req, res, next) {
62 router.get('/makefriends', miscMiddleware.cache(false), makeFriends) 69 pods.quitFriends(function (err) {
63 router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) 70 if (err) return next(err)
64 router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods)
65 // Post because this is a secured request
66 router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods)
67 71
68 module.exports = router 72 res.sendStatus(204)
73 })
74 }
69})() 75})()
diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js
index d534d6792..2be2fc87e 100644
--- a/controllers/api/v1/remoteVideos.js
+++ b/controllers/api/v1/remoteVideos.js
@@ -2,7 +2,6 @@
2 'use strict' 2 'use strict'
3 3
4 var express = require('express') 4 var express = require('express')
5 var router = express.Router()
6 var pluck = require('lodash-node/compat/collection/pluck') 5 var pluck = require('lodash-node/compat/collection/pluck')
7 6
8 var middleware = require('../../../middlewares') 7 var middleware = require('../../../middlewares')
@@ -10,6 +9,30 @@
10 var reqValidator = middleware.reqValidators.remote 9 var reqValidator = middleware.reqValidators.remote
11 var videos = require('../../../models/videos') 10 var videos = require('../../../models/videos')
12 11
12 var router = express.Router()
13
14 router.post('/add',
15 reqValidator.secureRequest,
16 miscMiddleware.decryptBody,
17 reqValidator.remoteVideosAdd,
18 miscMiddleware.cache(false),
19 addRemoteVideos
20 )
21
22 router.post('/remove',
23 reqValidator.secureRequest,
24 miscMiddleware.decryptBody,
25 reqValidator.remoteVideosRemove,
26 miscMiddleware.cache(false),
27 removeRemoteVideo
28 )
29
30 // ---------------------------------------------------------------------------
31
32 module.exports = router
33
34 // ---------------------------------------------------------------------------
35
13 function addRemoteVideos (req, res, next) { 36 function addRemoteVideos (req, res, next) {
14 videos.addRemotes(req.body.data, function (err, videos) { 37 videos.addRemotes(req.body.data, function (err, videos) {
15 if (err) return next(err) 38 if (err) return next(err)
@@ -25,9 +48,4 @@
25 res.sendStatus(204) 48 res.sendStatus(204)
26 }) 49 })
27 } 50 }
28
29 router.post('/add', reqValidator.secureRequest, miscMiddleware.decryptBody, reqValidator.remoteVideosAdd, miscMiddleware.cache(false), addRemoteVideos)
30 router.post('/remove', reqValidator.secureRequest, miscMiddleware.decryptBody, reqValidator.remoteVideosRemove, miscMiddleware.cache(false), removeRemoteVideo)
31
32 module.exports = router
33})() 51})()
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js
index aa8cb466b..64b05e32b 100644
--- a/controllers/api/v1/videos.js
+++ b/controllers/api/v1/videos.js
@@ -1,34 +1,50 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 var express = require('express')
5 var config = require('config') 4 var config = require('config')
6 var crypto = require('crypto') 5 var crypto = require('crypto')
6 var express = require('express')
7 var multer = require('multer') 7 var multer = require('multer')
8 var router = express.Router()
9 8
10 var middleware = require('../../../middlewares') 9 var middleware = require('../../../middlewares')
11 var miscMiddleware = middleware.misc 10 var miscMiddleware = middleware.misc
12 var reqValidator = middleware.reqValidators.videos 11 var reqValidator = middleware.reqValidators.videos
13 var videos = require('../../../models/videos') 12 var videos = require('../../../models/videos')
14 13
14 var router = express.Router()
15 var uploads = config.get('storage.uploads') 15 var uploads = config.get('storage.uploads')
16 16
17 function listVideos (req, res, next) { 17 // multer configuration
18 videos.list(function (err, videos_list) { 18 var storage = multer.diskStorage({
19 if (err) return next(err) 19 destination: function (req, file, cb) {
20 cb(null, uploads)
21 },
20 22
21 res.json(videos_list) 23 filename: function (req, file, cb) {
22 }) 24 var extension = ''
23 } 25 if (file.mimetype === 'video/webm') extension = 'webm'
26 else if (file.mimetype === 'video/mp4') extension = 'mp4'
27 else if (file.mimetype === 'video/ogg') extension = 'ogv'
28 crypto.pseudoRandomBytes(16, function (err, raw) {
29 var fieldname = err ? undefined : raw.toString('hex')
30 cb(null, fieldname + '.' + extension)
31 })
32 }
33 })
24 34
25 function searchVideos (req, res, next) { 35 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
26 videos.search(req.params.name, function (err, videos_list) {
27 if (err) return next(err)
28 36
29 res.json(videos_list) 37 router.get('/', miscMiddleware.cache(false), listVideos)
30 }) 38 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos)
31 } 39 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
40 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
41 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
42
43 // ---------------------------------------------------------------------------
44
45 module.exports = router
46
47 // ---------------------------------------------------------------------------
32 48
33 function addVideos (req, res, next) { 49 function addVideos (req, res, next) {
34 videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { 50 videos.add({ video: req.files.input_video[0], data: req.body }, function (err) {
@@ -51,6 +67,14 @@
51 }) 67 })
52 } 68 }
53 69
70 function listVideos (req, res, next) {
71 videos.list(function (err, videos_list) {
72 if (err) return next(err)
73
74 res.json(videos_list)
75 })
76 }
77
54 function removeVideo (req, res, next) { 78 function removeVideo (req, res, next) {
55 videos.remove(req.params.id, function (err) { 79 videos.remove(req.params.id, function (err) {
56 if (err) return next(err) 80 if (err) return next(err)
@@ -59,30 +83,11 @@
59 }) 83 })
60 } 84 }
61 85
62 // multer configuration 86 function searchVideos (req, res, next) {
63 var storage = multer.diskStorage({ 87 videos.search(req.params.name, function (err, videos_list) {
64 destination: function (req, file, cb) { 88 if (err) return next(err)
65 cb(null, uploads)
66 },
67
68 filename: function (req, file, cb) {
69 var extension = ''
70 if (file.mimetype === 'video/webm') extension = 'webm'
71 else if (file.mimetype === 'video/mp4') extension = 'mp4'
72 else if (file.mimetype === 'video/ogg') extension = 'ogv'
73 crypto.pseudoRandomBytes(16, function (err, raw) {
74 var fieldname = err ? undefined : raw.toString('hex')
75 cb(null, fieldname + '.' + extension)
76 })
77 }
78 })
79 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
80
81 router.get('/', miscMiddleware.cache(false), listVideos)
82 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos)
83 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
84 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
85 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
86 89
87 module.exports = router 90 res.json(videos_list)
91 })
92 }
88})() 93})()
diff --git a/controllers/index.js b/controllers/index.js
index 7dca002ff..770d08248 100644
--- a/controllers/index.js
+++ b/controllers/index.js
@@ -3,10 +3,8 @@
3 3
4 var constants = require('../initializers/constants') 4 var constants = require('../initializers/constants')
5 5
6 var routes = { 6 module.exports = {
7 api: require('./api/' + constants.API_VERSION), 7 api: require('./api/' + constants.API_VERSION),
8 views: require('./views') 8 views: require('./views')
9 } 9 }
10
11 module.exports = routes
12})() 10})()
diff --git a/controllers/views.js b/controllers/views.js
index ebd97380e..1bb4ffe70 100644
--- a/controllers/views.js
+++ b/controllers/views.js
@@ -1,24 +1,29 @@
1;(function () { 1;(function () {
2 'use strict' 2 'use strict'
3 3
4 function getPartial (req, res) {
5 var directory = req.params.directory
6 var name = req.params.name
7
8 res.render('partials/' + directory + '/' + name)
9 }
10
11 function getIndex (req, res) {
12 res.render('index')
13 }
14
15 var express = require('express') 4 var express = require('express')
5
16 var middleware = require('../middlewares').misc 6 var middleware = require('../middlewares').misc
17 7
18 var router = express.Router() 8 var router = express.Router()
19 9
20 router.get('/partials/:directory/:name', middleware.cache(), getPartial)
21 router.get(/^\/(index)?$/, middleware.cache(), getIndex) 10 router.get(/^\/(index)?$/, middleware.cache(), getIndex)
11 router.get('/partials/:directory/:name', middleware.cache(), getPartial)
12
13 // ---------------------------------------------------------------------------
22 14
23 module.exports = router 15 module.exports = router
16
17 // ---------------------------------------------------------------------------
18
19 function getIndex (req, res) {
20 res.render('index')
21 }
22
23 function getPartial (req, res) {
24 var directory = req.params.directory
25 var name = req.params.name
26
27 res.render('partials/' + directory + '/' + name)
28 }
24})() 29})()