aboutsummaryrefslogtreecommitdiffhomepage
path: root/controllers/api/v1
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/api/v1')
-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
4 files changed, 99 insertions, 67 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})()