diff options
Diffstat (limited to 'server/controllers/api/v1/pods.js')
-rw-r--r-- | server/controllers/api/v1/pods.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js new file mode 100644 index 000000000..c93a86ee8 --- /dev/null +++ b/server/controllers/api/v1/pods.js | |||
@@ -0,0 +1,93 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var express = require('express') | ||
4 | var fs = require('fs') | ||
5 | |||
6 | var logger = require('../../../helpers/logger') | ||
7 | var friends = require('../../../lib/friends') | ||
8 | var middleware = require('../../../middlewares') | ||
9 | var cacheMiddleware = middleware.cache | ||
10 | var peertubeCrypto = require('../../../helpers/peertubeCrypto') | ||
11 | var Pods = require('../../../models/pods') | ||
12 | var reqValidator = middleware.reqValidators.pods | ||
13 | var secureMiddleware = middleware.secure | ||
14 | var secureRequest = middleware.reqValidators.remote.secureRequest | ||
15 | var Videos = require('../../../models/videos') | ||
16 | |||
17 | var router = express.Router() | ||
18 | |||
19 | router.get('/', cacheMiddleware.cache(false), listPods) | ||
20 | router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) | ||
21 | router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) | ||
22 | router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) | ||
23 | // Post because this is a secured request | ||
24 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) | ||
25 | |||
26 | // --------------------------------------------------------------------------- | ||
27 | |||
28 | module.exports = router | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | function addPods (req, res, next) { | ||
33 | var informations = req.body.data | ||
34 | Pods.add(informations, function (err) { | ||
35 | if (err) return next(err) | ||
36 | |||
37 | Videos.addRemotes(informations.videos) | ||
38 | |||
39 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | ||
40 | if (err) { | ||
41 | logger.error('Cannot read cert file.') | ||
42 | return next(err) | ||
43 | } | ||
44 | |||
45 | Videos.listOwned(function (err, videos_list) { | ||
46 | if (err) { | ||
47 | logger.error('Cannot get the list of owned videos.') | ||
48 | return next(err) | ||
49 | } | ||
50 | |||
51 | res.json({ cert: cert, videos: videos_list }) | ||
52 | }) | ||
53 | }) | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | function listPods (req, res, next) { | ||
58 | Pods.list(function (err, pods_list) { | ||
59 | if (err) return next(err) | ||
60 | |||
61 | res.json(pods_list) | ||
62 | }) | ||
63 | } | ||
64 | |||
65 | function makeFriends (req, res, next) { | ||
66 | friends.makeFriends(function (err) { | ||
67 | if (err) return next(err) | ||
68 | |||
69 | res.sendStatus(204) | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | function removePods (req, res, next) { | ||
74 | var url = req.body.signature.url | ||
75 | Pods.remove(url, function (err) { | ||
76 | if (err) return next(err) | ||
77 | |||
78 | Videos.removeAllRemotesOf(url, function (err) { | ||
79 | if (err) logger.error('Cannot remove all remote videos of %s.', url) | ||
80 | else logger.info('%s pod removed.', url) | ||
81 | |||
82 | res.sendStatus(204) | ||
83 | }) | ||
84 | }) | ||
85 | } | ||
86 | |||
87 | function quitFriends (req, res, next) { | ||
88 | friends.quitFriends(function (err) { | ||
89 | if (err) return next(err) | ||
90 | |||
91 | res.sendStatus(204) | ||
92 | }) | ||
93 | } | ||