diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /server/controllers/api | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/v1/clients.js | 41 | ||||
-rw-r--r-- | server/controllers/api/v1/index.js | 4 | ||||
-rw-r--r-- | server/controllers/api/v1/pods.js | 62 | ||||
-rw-r--r-- | server/controllers/api/v1/remote.js | 1 | ||||
-rw-r--r-- | server/controllers/api/v1/requests.js | 38 | ||||
-rw-r--r-- | server/controllers/api/v1/users.js | 165 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 7 |
7 files changed, 281 insertions, 37 deletions
diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js new file mode 100644 index 000000000..5b460db2e --- /dev/null +++ b/server/controllers/api/v1/clients.js | |||
@@ -0,0 +1,41 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | const mongoose = require('mongoose') | ||
5 | |||
6 | const constants = require('../../../initializers/constants') | ||
7 | |||
8 | const Client = mongoose.model('OAuthClient') | ||
9 | |||
10 | const router = express.Router() | ||
11 | |||
12 | router.get('/local', getLocalClient) | ||
13 | |||
14 | // Get the client credentials for the PeerTube front end | ||
15 | function getLocalClient (req, res, next) { | ||
16 | const serverHost = constants.CONFIG.WEBSERVER.HOST | ||
17 | const serverPort = constants.CONFIG.WEBSERVER.PORT | ||
18 | let headerHostShouldBe = serverHost | ||
19 | if (serverPort !== 80 && serverPort !== 443) { | ||
20 | headerHostShouldBe += ':' + serverPort | ||
21 | } | ||
22 | |||
23 | // Don't make this check if this is a test instance | ||
24 | if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { | ||
25 | return res.type('json').status(403).end() | ||
26 | } | ||
27 | |||
28 | Client.loadFirstClient(function (err, client) { | ||
29 | if (err) return next(err) | ||
30 | if (!client) return next(new Error('No client available.')) | ||
31 | |||
32 | res.json({ | ||
33 | client_id: client._id, | ||
34 | client_secret: client.clientSecret | ||
35 | }) | ||
36 | }) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | module.exports = router | ||
diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js index e0c29a8a2..2e4fb2dab 100644 --- a/server/controllers/api/v1/index.js +++ b/server/controllers/api/v1/index.js | |||
@@ -4,13 +4,17 @@ const express = require('express') | |||
4 | 4 | ||
5 | const router = express.Router() | 5 | const router = express.Router() |
6 | 6 | ||
7 | const clientsController = require('./clients') | ||
7 | const podsController = require('./pods') | 8 | const podsController = require('./pods') |
8 | const remoteController = require('./remote') | 9 | const remoteController = require('./remote') |
10 | const requestsController = require('./requests') | ||
9 | const usersController = require('./users') | 11 | const usersController = require('./users') |
10 | const videosController = require('./videos') | 12 | const videosController = require('./videos') |
11 | 13 | ||
14 | router.use('/clients', clientsController) | ||
12 | router.use('/pods', podsController) | 15 | router.use('/pods', podsController) |
13 | router.use('/remote', remoteController) | 16 | router.use('/remote', remoteController) |
17 | router.use('/requests', requestsController) | ||
14 | router.use('/users', usersController) | 18 | router.use('/users', usersController) |
15 | router.use('/videos', videosController) | 19 | router.use('/videos', videosController) |
16 | router.use('/*', badRequest) | 20 | router.use('/*', badRequest) |
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 2bc761fef..8ffade578 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -8,7 +8,10 @@ const waterfall = require('async/waterfall') | |||
8 | const logger = require('../../../helpers/logger') | 8 | const logger = require('../../../helpers/logger') |
9 | const friends = require('../../../lib/friends') | 9 | const friends = require('../../../lib/friends') |
10 | const middlewares = require('../../../middlewares') | 10 | const middlewares = require('../../../middlewares') |
11 | const admin = middlewares.admin | ||
11 | const oAuth = middlewares.oauth | 12 | const oAuth = middlewares.oauth |
13 | const podsMiddleware = middlewares.pods | ||
14 | const checkSignature = middlewares.secure.checkSignature | ||
12 | const validators = middlewares.validators.pods | 15 | const validators = middlewares.validators.pods |
13 | const signatureValidator = middlewares.validators.remote.signature | 16 | const signatureValidator = middlewares.validators.remote.signature |
14 | 17 | ||
@@ -16,12 +19,30 @@ const router = express.Router() | |||
16 | const Pod = mongoose.model('Pod') | 19 | const Pod = mongoose.model('Pod') |
17 | const Video = mongoose.model('Video') | 20 | const Video = mongoose.model('Video') |
18 | 21 | ||
19 | router.get('/', listPodsUrl) | 22 | router.get('/', listPods) |
20 | router.post('/', validators.podsAdd, addPods) | 23 | router.post('/', |
21 | router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) | 24 | validators.podsAdd, |
22 | router.get('/quitfriends', oAuth.authenticate, quitFriends) | 25 | podsMiddleware.setBodyUrlPort, |
26 | addPods | ||
27 | ) | ||
28 | router.post('/makefriends', | ||
29 | oAuth.authenticate, | ||
30 | admin.ensureIsAdmin, | ||
31 | validators.makeFriends, | ||
32 | podsMiddleware.setBodyUrlsPort, | ||
33 | makeFriends | ||
34 | ) | ||
35 | router.get('/quitfriends', | ||
36 | oAuth.authenticate, | ||
37 | admin.ensureIsAdmin, | ||
38 | quitFriends | ||
39 | ) | ||
23 | // Post because this is a secured request | 40 | // Post because this is a secured request |
24 | router.post('/remove', signatureValidator, removePods) | 41 | router.post('/remove', |
42 | signatureValidator, | ||
43 | checkSignature, | ||
44 | removePods | ||
45 | ) | ||
25 | 46 | ||
26 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
27 | 48 | ||
@@ -64,20 +85,27 @@ function addPods (req, res, next) { | |||
64 | }) | 85 | }) |
65 | } | 86 | } |
66 | 87 | ||
67 | function listPodsUrl (req, res, next) { | 88 | function listPods (req, res, next) { |
68 | Pod.listOnlyUrls(function (err, podsUrlList) { | 89 | Pod.list(function (err, podsUrlList) { |
69 | if (err) return next(err) | 90 | if (err) return next(err) |
70 | 91 | ||
71 | res.json(podsUrlList) | 92 | res.json(getFormatedPods(podsUrlList)) |
72 | }) | 93 | }) |
73 | } | 94 | } |
74 | 95 | ||
75 | function makeFriends (req, res, next) { | 96 | function makeFriends (req, res, next) { |
76 | friends.makeFriends(function (err) { | 97 | const urls = req.body.urls |
77 | if (err) return next(err) | ||
78 | 98 | ||
79 | res.type('json').status(204).end() | 99 | friends.makeFriends(urls, function (err) { |
100 | if (err) { | ||
101 | logger.error('Could not make friends.', { error: err }) | ||
102 | return | ||
103 | } | ||
104 | |||
105 | logger.info('Made friends!') | ||
80 | }) | 106 | }) |
107 | |||
108 | res.type('json').status(204).end() | ||
81 | } | 109 | } |
82 | 110 | ||
83 | function removePods (req, res, next) { | 111 | function removePods (req, res, next) { |
@@ -125,3 +153,15 @@ function quitFriends (req, res, next) { | |||
125 | res.type('json').status(204).end() | 153 | res.type('json').status(204).end() |
126 | }) | 154 | }) |
127 | } | 155 | } |
156 | |||
157 | // --------------------------------------------------------------------------- | ||
158 | |||
159 | function getFormatedPods (pods) { | ||
160 | const formatedPods = [] | ||
161 | |||
162 | pods.forEach(function (pod) { | ||
163 | formatedPods.push(pod.toFormatedJSON()) | ||
164 | }) | ||
165 | |||
166 | return formatedPods | ||
167 | } | ||
diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js index f452986b8..a22c5d151 100644 --- a/server/controllers/api/v1/remote.js +++ b/server/controllers/api/v1/remote.js | |||
@@ -16,6 +16,7 @@ const Video = mongoose.model('Video') | |||
16 | router.post('/videos', | 16 | router.post('/videos', |
17 | validators.signature, | 17 | validators.signature, |
18 | validators.dataToDecrypt, | 18 | validators.dataToDecrypt, |
19 | secureMiddleware.checkSignature, | ||
19 | secureMiddleware.decryptBody, | 20 | secureMiddleware.decryptBody, |
20 | validators.remoteVideos, | 21 | validators.remoteVideos, |
21 | remoteVideos | 22 | remoteVideos |
diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js new file mode 100644 index 000000000..97616424d --- /dev/null +++ b/server/controllers/api/v1/requests.js | |||
@@ -0,0 +1,38 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | const mongoose = require('mongoose') | ||
5 | |||
6 | const constants = require('../../../initializers/constants') | ||
7 | const middlewares = require('../../../middlewares') | ||
8 | const admin = middlewares.admin | ||
9 | const oAuth = middlewares.oauth | ||
10 | |||
11 | const Request = mongoose.model('Request') | ||
12 | |||
13 | const router = express.Router() | ||
14 | |||
15 | router.get('/stats', | ||
16 | oAuth.authenticate, | ||
17 | admin.ensureIsAdmin, | ||
18 | getStatsRequests | ||
19 | ) | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | module.exports = router | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | function getStatsRequests (req, res, next) { | ||
28 | Request.list(function (err, requests) { | ||
29 | if (err) return next(err) | ||
30 | |||
31 | return res.json({ | ||
32 | requests: requests, | ||
33 | maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL, | ||
34 | remainingMilliSeconds: Request.remainingMilliSeconds(), | ||
35 | milliSecondsInterval: constants.REQUESTS_INTERVAL | ||
36 | }) | ||
37 | }) | ||
38 | } | ||
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index fbbe6e472..975e25e68 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js | |||
@@ -1,18 +1,59 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const each = require('async/each') |
4 | const mongoose = require('mongoose') | ||
5 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
6 | const waterfall = require('async/waterfall') | ||
6 | 7 | ||
7 | const oAuth = require('../../../middlewares').oauth | 8 | const constants = require('../../../initializers/constants') |
9 | const friends = require('../../../lib/friends') | ||
10 | const logger = require('../../../helpers/logger') | ||
11 | const middlewares = require('../../../middlewares') | ||
12 | const admin = middlewares.admin | ||
13 | const oAuth = middlewares.oauth | ||
14 | const pagination = middlewares.pagination | ||
15 | const sort = middlewares.sort | ||
16 | const validatorsPagination = middlewares.validators.pagination | ||
17 | const validatorsSort = middlewares.validators.sort | ||
18 | const validatorsUsers = middlewares.validators.users | ||
8 | 19 | ||
9 | const Client = mongoose.model('OAuthClient') | 20 | const User = mongoose.model('User') |
21 | const Video = mongoose.model('Video') | ||
10 | 22 | ||
11 | const router = express.Router() | 23 | const router = express.Router() |
12 | 24 | ||
13 | router.get('/client', getAngularClient) | 25 | router.get('/me', oAuth.authenticate, getUserInformation) |
26 | |||
27 | router.get('/', | ||
28 | validatorsPagination.pagination, | ||
29 | validatorsSort.usersSort, | ||
30 | sort.setUsersSort, | ||
31 | pagination.setPagination, | ||
32 | listUsers | ||
33 | ) | ||
34 | |||
35 | router.post('/', | ||
36 | oAuth.authenticate, | ||
37 | admin.ensureIsAdmin, | ||
38 | validatorsUsers.usersAdd, | ||
39 | createUser | ||
40 | ) | ||
41 | |||
42 | router.put('/:id', | ||
43 | oAuth.authenticate, | ||
44 | validatorsUsers.usersUpdate, | ||
45 | updateUser | ||
46 | ) | ||
47 | |||
48 | router.delete('/:id', | ||
49 | oAuth.authenticate, | ||
50 | admin.ensureIsAdmin, | ||
51 | validatorsUsers.usersRemove, | ||
52 | removeUser | ||
53 | ) | ||
54 | |||
14 | router.post('/token', oAuth.token, success) | 55 | router.post('/token', oAuth.token, success) |
15 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged,, implement revoke token route | 56 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
16 | 57 | ||
17 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
18 | 59 | ||
@@ -20,26 +61,91 @@ module.exports = router | |||
20 | 61 | ||
21 | // --------------------------------------------------------------------------- | 62 | // --------------------------------------------------------------------------- |
22 | 63 | ||
23 | function getAngularClient (req, res, next) { | 64 | function createUser (req, res, next) { |
24 | const serverHost = config.get('webserver.host') | 65 | const user = new User({ |
25 | const serverPort = config.get('webserver.port') | 66 | username: req.body.username, |
26 | let headerHostShouldBe = serverHost | 67 | password: req.body.password, |
27 | if (serverPort !== 80 && serverPort !== 443) { | 68 | role: constants.USER_ROLES.USER |
28 | headerHostShouldBe += ':' + serverPort | 69 | }) |
29 | } | ||
30 | 70 | ||
31 | // Don't make this check if this is a test instance | 71 | user.save(function (err, createdUser) { |
32 | if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { | 72 | if (err) return next(err) |
33 | return res.type('json').status(403).end() | ||
34 | } | ||
35 | 73 | ||
36 | Client.loadFirstClient(function (err, client) { | 74 | return res.type('json').status(204).end() |
75 | }) | ||
76 | } | ||
77 | |||
78 | function getUserInformation (req, res, next) { | ||
79 | User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | ||
37 | if (err) return next(err) | 80 | if (err) return next(err) |
38 | if (!client) return next(new Error('No client available.')) | ||
39 | 81 | ||
40 | res.json({ | 82 | return res.json(user.toFormatedJSON()) |
41 | client_id: client._id, | 83 | }) |
42 | client_secret: client.clientSecret | 84 | } |
85 | |||
86 | function listUsers (req, res, next) { | ||
87 | User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { | ||
88 | if (err) return next(err) | ||
89 | |||
90 | res.json(getFormatedUsers(usersList, usersTotal)) | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | function removeUser (req, res, next) { | ||
95 | waterfall([ | ||
96 | function getUser (callback) { | ||
97 | User.loadById(req.params.id, callback) | ||
98 | }, | ||
99 | |||
100 | function getVideos (user, callback) { | ||
101 | Video.listOwnedByAuthor(user.username, function (err, videos) { | ||
102 | return callback(err, user, videos) | ||
103 | }) | ||
104 | }, | ||
105 | |||
106 | function removeVideosFromDB (user, videos, callback) { | ||
107 | each(videos, function (video, callbackEach) { | ||
108 | video.remove(callbackEach) | ||
109 | }, function (err) { | ||
110 | return callback(err, user, videos) | ||
111 | }) | ||
112 | }, | ||
113 | |||
114 | function sendInformationToFriends (user, videos, callback) { | ||
115 | videos.forEach(function (video) { | ||
116 | const params = { | ||
117 | name: video.name, | ||
118 | magnetUri: video.magnetUri | ||
119 | } | ||
120 | |||
121 | friends.removeVideoToFriends(params) | ||
122 | }) | ||
123 | |||
124 | return callback(null, user) | ||
125 | }, | ||
126 | |||
127 | function removeUserFromDB (user, callback) { | ||
128 | user.remove(callback) | ||
129 | } | ||
130 | ], function andFinally (err) { | ||
131 | if (err) { | ||
132 | logger.error('Errors when removed the user.', { error: err }) | ||
133 | return next(err) | ||
134 | } | ||
135 | |||
136 | return res.sendStatus(204) | ||
137 | }) | ||
138 | } | ||
139 | |||
140 | function updateUser (req, res, next) { | ||
141 | User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | ||
142 | if (err) return next(err) | ||
143 | |||
144 | user.password = req.body.password | ||
145 | user.save(function (err) { | ||
146 | if (err) return next(err) | ||
147 | |||
148 | return res.sendStatus(204) | ||
43 | }) | 149 | }) |
44 | }) | 150 | }) |
45 | } | 151 | } |
@@ -47,3 +153,18 @@ function getAngularClient (req, res, next) { | |||
47 | function success (req, res, next) { | 153 | function success (req, res, next) { |
48 | res.end() | 154 | res.end() |
49 | } | 155 | } |
156 | |||
157 | // --------------------------------------------------------------------------- | ||
158 | |||
159 | function getFormatedUsers (users, usersTotal) { | ||
160 | const formatedUsers = [] | ||
161 | |||
162 | users.forEach(function (user) { | ||
163 | formatedUsers.push(user.toFormatedJSON()) | ||
164 | }) | ||
165 | |||
166 | return { | ||
167 | total: usersTotal, | ||
168 | data: formatedUsers | ||
169 | } | ||
170 | } | ||
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 1f939b077..70d22f139 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -1,11 +1,11 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
6 | const multer = require('multer') | 5 | const multer = require('multer') |
7 | const waterfall = require('async/waterfall') | 6 | const waterfall = require('async/waterfall') |
8 | 7 | ||
8 | const constants = require('../../../initializers/constants') | ||
9 | const logger = require('../../../helpers/logger') | 9 | const logger = require('../../../helpers/logger') |
10 | const friends = require('../../../lib/friends') | 10 | const friends = require('../../../lib/friends') |
11 | const middlewares = require('../../../middlewares') | 11 | const middlewares = require('../../../middlewares') |
@@ -20,13 +20,12 @@ const sort = middlewares.sort | |||
20 | const utils = require('../../../helpers/utils') | 20 | const utils = require('../../../helpers/utils') |
21 | 21 | ||
22 | const router = express.Router() | 22 | const router = express.Router() |
23 | const uploads = config.get('storage.uploads') | ||
24 | const Video = mongoose.model('Video') | 23 | const Video = mongoose.model('Video') |
25 | 24 | ||
26 | // multer configuration | 25 | // multer configuration |
27 | const storage = multer.diskStorage({ | 26 | const storage = multer.diskStorage({ |
28 | destination: function (req, file, cb) { | 27 | destination: function (req, file, cb) { |
29 | cb(null, uploads) | 28 | cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR) |
30 | }, | 29 | }, |
31 | 30 | ||
32 | filename: function (req, file, cb) { | 31 | filename: function (req, file, cb) { |
@@ -142,7 +141,7 @@ function getVideo (req, res, next) { | |||
142 | } | 141 | } |
143 | 142 | ||
144 | function listVideos (req, res, next) { | 143 | function listVideos (req, res, next) { |
145 | Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { | 144 | Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { |
146 | if (err) return next(err) | 145 | if (err) return next(err) |
147 | 146 | ||
148 | res.json(getFormatedVideos(videosList, videosTotal)) | 147 | res.json(getFormatedVideos(videosList, videosTotal)) |