diff options
Diffstat (limited to 'controllers')
-rw-r--r-- | controllers/api/v1/index.js | 12 | ||||
-rw-r--r-- | controllers/api/v1/pods.js | 69 | ||||
-rw-r--r-- | controllers/api/v1/remoteVideos.js | 33 | ||||
-rw-r--r-- | controllers/api/v1/videos.js | 88 | ||||
-rw-r--r-- | controllers/index.js | 12 | ||||
-rw-r--r-- | controllers/views.js | 24 |
6 files changed, 238 insertions, 0 deletions
diff --git a/controllers/api/v1/index.js b/controllers/api/v1/index.js new file mode 100644 index 000000000..f5504ad85 --- /dev/null +++ b/controllers/api/v1/index.js | |||
@@ -0,0 +1,12 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var express = require('express') | ||
5 | var router = express.Router() | ||
6 | |||
7 | router.use('/videos', require('./videos')) | ||
8 | router.use('/remotevideos', require('./remoteVideos')) | ||
9 | router.use('/pods', require('./pods')) | ||
10 | |||
11 | module.exports = router | ||
12 | })() | ||
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js new file mode 100644 index 000000000..30385bd5a --- /dev/null +++ b/controllers/api/v1/pods.js | |||
@@ -0,0 +1,69 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var express = require('express') | ||
5 | var router = express.Router() | ||
6 | var middleware = require('../../../middlewares') | ||
7 | var miscMiddleware = middleware.misc | ||
8 | var reqValidator = middleware.reqValidators.pods | ||
9 | var secureRequest = middleware.reqValidators.remote.secureRequest | ||
10 | var pods = require('../../../models/pods') | ||
11 | |||
12 | function listPods (req, res, next) { | ||
13 | pods.list(function (err, pods_list) { | ||
14 | if (err) return next(err) | ||
15 | |||
16 | res.json(pods_list) | ||
17 | }) | ||
18 | } | ||
19 | |||
20 | function addPods (req, res, next) { | ||
21 | pods.add(req.body.data, function (err, json) { | ||
22 | if (err) return next(err) | ||
23 | |||
24 | res.json(json) | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | function removePods (req, res, next) { | ||
29 | pods.remove(req.body.signature.url, function (err) { | ||
30 | if (err) return next(err) | ||
31 | |||
32 | res.sendStatus(204) | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | function makeFriends (req, res, next) { | ||
37 | pods.hasFriends(function (err, has_friends) { | ||
38 | if (err) return next(err) | ||
39 | |||
40 | if (has_friends === true) { | ||
41 | // We need to quit our friends before make new ones | ||
42 | res.sendStatus(409) | ||
43 | } else { | ||
44 | pods.makeFriends(function (err) { | ||
45 | if (err) return next(err) | ||
46 | |||
47 | res.sendStatus(204) | ||
48 | }) | ||
49 | } | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | function quitFriends (req, res, next) { | ||
54 | pods.quitFriends(function (err) { | ||
55 | if (err) return next(err) | ||
56 | |||
57 | res.sendStatus(204) | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | router.get('/', miscMiddleware.cache(false), listPods) | ||
62 | router.get('/makefriends', miscMiddleware.cache(false), makeFriends) | ||
63 | router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) | ||
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 | |||
68 | module.exports = router | ||
69 | })() | ||
diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js new file mode 100644 index 000000000..d534d6792 --- /dev/null +++ b/controllers/api/v1/remoteVideos.js | |||
@@ -0,0 +1,33 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var express = require('express') | ||
5 | var router = express.Router() | ||
6 | var pluck = require('lodash-node/compat/collection/pluck') | ||
7 | |||
8 | var middleware = require('../../../middlewares') | ||
9 | var miscMiddleware = middleware.misc | ||
10 | var reqValidator = middleware.reqValidators.remote | ||
11 | var videos = require('../../../models/videos') | ||
12 | |||
13 | function addRemoteVideos (req, res, next) { | ||
14 | videos.addRemotes(req.body.data, function (err, videos) { | ||
15 | if (err) return next(err) | ||
16 | |||
17 | res.json(videos) | ||
18 | }) | ||
19 | } | ||
20 | |||
21 | function removeRemoteVideo (req, res, next) { | ||
22 | videos.removeRemotes(req.body.signature.url, pluck(req.body.data, 'magnetUri'), function (err) { | ||
23 | if (err) return next(err) | ||
24 | |||
25 | res.sendStatus(204) | ||
26 | }) | ||
27 | } | ||
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 | })() | ||
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js new file mode 100644 index 000000000..aa8cb466b --- /dev/null +++ b/controllers/api/v1/videos.js | |||
@@ -0,0 +1,88 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var express = require('express') | ||
5 | var config = require('config') | ||
6 | var crypto = require('crypto') | ||
7 | var multer = require('multer') | ||
8 | var router = express.Router() | ||
9 | |||
10 | var middleware = require('../../../middlewares') | ||
11 | var miscMiddleware = middleware.misc | ||
12 | var reqValidator = middleware.reqValidators.videos | ||
13 | var videos = require('../../../models/videos') | ||
14 | |||
15 | var uploads = config.get('storage.uploads') | ||
16 | |||
17 | function listVideos (req, res, next) { | ||
18 | videos.list(function (err, videos_list) { | ||
19 | if (err) return next(err) | ||
20 | |||
21 | res.json(videos_list) | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | function searchVideos (req, res, next) { | ||
26 | videos.search(req.params.name, function (err, videos_list) { | ||
27 | if (err) return next(err) | ||
28 | |||
29 | res.json(videos_list) | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | function addVideos (req, res, next) { | ||
34 | videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { | ||
35 | if (err) return next(err) | ||
36 | |||
37 | // TODO : include Location of the new video | ||
38 | res.sendStatus(201) | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | function getVideos (req, res, next) { | ||
43 | videos.get(req.params.id, function (err, video) { | ||
44 | if (err) return next(err) | ||
45 | |||
46 | if (video === null) { | ||
47 | return res.sendStatus(404) | ||
48 | } | ||
49 | |||
50 | res.json(video) | ||
51 | }) | ||
52 | } | ||
53 | |||
54 | function removeVideo (req, res, next) { | ||
55 | videos.remove(req.params.id, function (err) { | ||
56 | if (err) return next(err) | ||
57 | |||
58 | res.sendStatus(204) | ||
59 | }) | ||
60 | } | ||
61 | |||
62 | // multer configuration | ||
63 | var storage = multer.diskStorage({ | ||
64 | destination: function (req, file, cb) { | ||
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 | |||
87 | module.exports = router | ||
88 | })() | ||
diff --git a/controllers/index.js b/controllers/index.js new file mode 100644 index 000000000..7dca002ff --- /dev/null +++ b/controllers/index.js | |||
@@ -0,0 +1,12 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var constants = require('../initializers/constants') | ||
5 | |||
6 | var routes = { | ||
7 | api: require('./api/' + constants.API_VERSION), | ||
8 | views: require('./views') | ||
9 | } | ||
10 | |||
11 | module.exports = routes | ||
12 | })() | ||
diff --git a/controllers/views.js b/controllers/views.js new file mode 100644 index 000000000..ebd97380e --- /dev/null +++ b/controllers/views.js | |||
@@ -0,0 +1,24 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
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') | ||
16 | var middleware = require('../middlewares').misc | ||
17 | |||
18 | var router = express.Router() | ||
19 | |||
20 | router.get('/partials/:directory/:name', middleware.cache(), getPartial) | ||
21 | router.get(/^\/(index)?$/, middleware.cache(), getIndex) | ||
22 | |||
23 | module.exports = router | ||
24 | })() | ||