diff options
Diffstat (limited to 'controllers/api/v1')
-rw-r--r-- | controllers/api/v1/index.js | 18 | ||||
-rw-r--r-- | controllers/api/v1/pods.js | 136 | ||||
-rw-r--r-- | controllers/api/v1/remoteVideos.js | 82 | ||||
-rw-r--r-- | controllers/api/v1/videos.js | 232 |
4 files changed, 230 insertions, 238 deletions
diff --git a/controllers/api/v1/index.js b/controllers/api/v1/index.js index b16eeb0f6..fc6bf4946 100644 --- a/controllers/api/v1/index.js +++ b/controllers/api/v1/index.js | |||
@@ -1,15 +1,13 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | ||
3 | 2 | ||
4 | var express = require('express') | 3 | var express = require('express') |
5 | 4 | ||
6 | var router = express.Router() | 5 | var router = express.Router() |
7 | 6 | ||
8 | router.use('/pods', require('./pods')) | 7 | router.use('/pods', require('./pods')) |
9 | router.use('/remotevideos', require('./remoteVideos')) | 8 | router.use('/remotevideos', require('./remoteVideos')) |
10 | router.use('/videos', require('./videos')) | 9 | router.use('/videos', require('./videos')) |
11 | 10 | ||
12 | // --------------------------------------------------------------------------- | 11 | // --------------------------------------------------------------------------- |
13 | 12 | ||
14 | module.exports = router | 13 | module.exports = router |
15 | })() | ||
diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js index 456f53dea..c93a86ee8 100644 --- a/controllers/api/v1/pods.js +++ b/controllers/api/v1/pods.js | |||
@@ -1,95 +1,93 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | ||
3 | 2 | ||
4 | var express = require('express') | 3 | var express = require('express') |
5 | var fs = require('fs') | 4 | var fs = require('fs') |
6 | 5 | ||
7 | var logger = require('../../../helpers/logger') | 6 | var logger = require('../../../helpers/logger') |
8 | var friends = require('../../../lib/friends') | 7 | var friends = require('../../../lib/friends') |
9 | var middleware = require('../../../middlewares') | 8 | var middleware = require('../../../middlewares') |
10 | var cacheMiddleware = middleware.cache | 9 | var cacheMiddleware = middleware.cache |
11 | var peertubeCrypto = require('../../../helpers/peertubeCrypto') | 10 | var peertubeCrypto = require('../../../helpers/peertubeCrypto') |
12 | var Pods = require('../../../models/pods') | 11 | var Pods = require('../../../models/pods') |
13 | var reqValidator = middleware.reqValidators.pods | 12 | var reqValidator = middleware.reqValidators.pods |
14 | var secureMiddleware = middleware.secure | 13 | var secureMiddleware = middleware.secure |
15 | var secureRequest = middleware.reqValidators.remote.secureRequest | 14 | var secureRequest = middleware.reqValidators.remote.secureRequest |
16 | var Videos = require('../../../models/videos') | 15 | var Videos = require('../../../models/videos') |
17 | 16 | ||
18 | var router = express.Router() | 17 | var router = express.Router() |
19 | 18 | ||
20 | router.get('/', cacheMiddleware.cache(false), listPods) | 19 | router.get('/', cacheMiddleware.cache(false), listPods) |
21 | router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) | 20 | router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods) |
22 | router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) | 21 | router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends) |
23 | router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) | 22 | router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends) |
24 | // Post because this is a secured request | 23 | // Post because this is a secured request |
25 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) | 24 | router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) |
26 | 25 | ||
27 | // --------------------------------------------------------------------------- | 26 | // --------------------------------------------------------------------------- |
28 | 27 | ||
29 | module.exports = router | 28 | module.exports = router |
30 | 29 | ||
31 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
32 | 31 | ||
33 | function addPods (req, res, next) { | 32 | function addPods (req, res, next) { |
34 | var informations = req.body.data | 33 | var informations = req.body.data |
35 | Pods.add(informations, function (err) { | 34 | Pods.add(informations, function (err) { |
36 | if (err) return next(err) | 35 | if (err) return next(err) |
37 | 36 | ||
38 | Videos.addRemotes(informations.videos) | 37 | Videos.addRemotes(informations.videos) |
39 | 38 | ||
40 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | 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) { | ||
41 | if (err) { | 46 | if (err) { |
42 | logger.error('Cannot read cert file.') | 47 | logger.error('Cannot get the list of owned videos.') |
43 | return next(err) | 48 | return next(err) |
44 | } | 49 | } |
45 | 50 | ||
46 | Videos.listOwned(function (err, videos_list) { | 51 | res.json({ cert: cert, videos: videos_list }) |
47 | if (err) { | ||
48 | logger.error('Cannot get the list of owned videos.') | ||
49 | return next(err) | ||
50 | } | ||
51 | |||
52 | res.json({ cert: cert, videos: videos_list }) | ||
53 | }) | ||
54 | }) | 52 | }) |
55 | }) | 53 | }) |
56 | } | 54 | }) |
55 | } | ||
57 | 56 | ||
58 | function listPods (req, res, next) { | 57 | function listPods (req, res, next) { |
59 | Pods.list(function (err, pods_list) { | 58 | Pods.list(function (err, pods_list) { |
60 | if (err) return next(err) | 59 | if (err) return next(err) |
61 | 60 | ||
62 | res.json(pods_list) | 61 | res.json(pods_list) |
63 | }) | 62 | }) |
64 | } | 63 | } |
65 | 64 | ||
66 | function makeFriends (req, res, next) { | 65 | function makeFriends (req, res, next) { |
67 | friends.makeFriends(function (err) { | 66 | friends.makeFriends(function (err) { |
68 | if (err) return next(err) | 67 | if (err) return next(err) |
69 | 68 | ||
70 | res.sendStatus(204) | 69 | res.sendStatus(204) |
71 | }) | 70 | }) |
72 | } | 71 | } |
73 | 72 | ||
74 | function removePods (req, res, next) { | 73 | function removePods (req, res, next) { |
75 | var url = req.body.signature.url | 74 | var url = req.body.signature.url |
76 | Pods.remove(url, function (err) { | 75 | Pods.remove(url, function (err) { |
77 | if (err) return next(err) | 76 | if (err) return next(err) |
78 | 77 | ||
79 | Videos.removeAllRemotesOf(url, function (err) { | 78 | Videos.removeAllRemotesOf(url, function (err) { |
80 | if (err) logger.error('Cannot remove all remote videos of %s.', url) | 79 | if (err) logger.error('Cannot remove all remote videos of %s.', url) |
81 | else logger.info('%s pod removed.', url) | 80 | else logger.info('%s pod removed.', url) |
82 | 81 | ||
83 | res.sendStatus(204) | 82 | res.sendStatus(204) |
84 | }) | ||
85 | }) | 83 | }) |
86 | } | 84 | }) |
85 | } | ||
87 | 86 | ||
88 | function quitFriends (req, res, next) { | 87 | function quitFriends (req, res, next) { |
89 | friends.quitFriends(function (err) { | 88 | friends.quitFriends(function (err) { |
90 | if (err) return next(err) | 89 | if (err) return next(err) |
91 | 90 | ||
92 | res.sendStatus(204) | 91 | res.sendStatus(204) |
93 | }) | 92 | }) |
94 | } | 93 | } |
95 | })() | ||
diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js index 58bb5f3cb..475a874cf 100644 --- a/controllers/api/v1/remoteVideos.js +++ b/controllers/api/v1/remoteVideos.js | |||
@@ -1,55 +1,53 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | ||
3 | 2 | ||
4 | var express = require('express') | 3 | var express = require('express') |
5 | var pluck = require('lodash-node/compat/collection/pluck') | 4 | var pluck = require('lodash-node/compat/collection/pluck') |
6 | 5 | ||
7 | var middleware = require('../../../middlewares') | 6 | var middleware = require('../../../middlewares') |
8 | var secureMiddleware = middleware.secure | 7 | var secureMiddleware = middleware.secure |
9 | var cacheMiddleware = middleware.cache | 8 | var cacheMiddleware = middleware.cache |
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 | ||
13 | var router = express.Router() | 12 | var router = express.Router() |
14 | 13 | ||
15 | router.post('/add', | 14 | router.post('/add', |
16 | reqValidator.secureRequest, | 15 | reqValidator.secureRequest, |
17 | secureMiddleware.decryptBody, | 16 | secureMiddleware.decryptBody, |
18 | reqValidator.remoteVideosAdd, | 17 | reqValidator.remoteVideosAdd, |
19 | cacheMiddleware.cache(false), | 18 | cacheMiddleware.cache(false), |
20 | addRemoteVideos | 19 | addRemoteVideos |
21 | ) | 20 | ) |
22 | 21 | ||
23 | router.post('/remove', | 22 | router.post('/remove', |
24 | reqValidator.secureRequest, | 23 | reqValidator.secureRequest, |
25 | secureMiddleware.decryptBody, | 24 | secureMiddleware.decryptBody, |
26 | reqValidator.remoteVideosRemove, | 25 | reqValidator.remoteVideosRemove, |
27 | cacheMiddleware.cache(false), | 26 | cacheMiddleware.cache(false), |
28 | removeRemoteVideo | 27 | removeRemoteVideo |
29 | ) | 28 | ) |
30 | 29 | ||
31 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
32 | 31 | ||
33 | module.exports = router | 32 | module.exports = router |
34 | 33 | ||
35 | // --------------------------------------------------------------------------- | 34 | // --------------------------------------------------------------------------- |
36 | 35 | ||
37 | function addRemoteVideos (req, res, next) { | 36 | function addRemoteVideos (req, res, next) { |
38 | videos.addRemotes(req.body.data, function (err, videos) { | 37 | videos.addRemotes(req.body.data, function (err, videos) { |
39 | if (err) return next(err) | 38 | if (err) return next(err) |
40 | 39 | ||
41 | res.json(videos) | 40 | res.json(videos) |
42 | }) | 41 | }) |
43 | } | 42 | } |
44 | 43 | ||
45 | function removeRemoteVideo (req, res, next) { | 44 | function removeRemoteVideo (req, res, next) { |
46 | var url = req.body.signature.url | 45 | var url = req.body.signature.url |
47 | var magnetUris = pluck(req.body.data, 'magnetUri') | 46 | var magnetUris = pluck(req.body.data, 'magnetUri') |
48 | 47 | ||
49 | videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { | 48 | videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { |
50 | if (err) return next(err) | 49 | if (err) return next(err) |
51 | 50 | ||
52 | res.sendStatus(204) | 51 | res.sendStatus(204) |
53 | }) | 52 | }) |
54 | } | 53 | } |
55 | })() | ||
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js index 7dccfa7c3..620711925 100644 --- a/controllers/api/v1/videos.js +++ b/controllers/api/v1/videos.js | |||
@@ -1,146 +1,144 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | 2 | |
3 | 3 | var config = require('config') | |
4 | var config = require('config') | 4 | var crypto = require('crypto') |
5 | var crypto = require('crypto') | 5 | var express = require('express') |
6 | var express = require('express') | 6 | var multer = require('multer') |
7 | var multer = require('multer') | 7 | |
8 | 8 | var logger = require('../../../helpers/logger') | |
9 | var logger = require('../../../helpers/logger') | 9 | var friends = require('../../../lib/friends') |
10 | var friends = require('../../../lib/friends') | 10 | var middleware = require('../../../middlewares') |
11 | var middleware = require('../../../middlewares') | 11 | var cacheMiddleware = middleware.cache |
12 | var cacheMiddleware = middleware.cache | 12 | var reqValidator = middleware.reqValidators.videos |
13 | var reqValidator = middleware.reqValidators.videos | 13 | var Videos = require('../../../models/videos') // model |
14 | var Videos = require('../../../models/videos') // model | 14 | var videos = require('../../../lib/videos') |
15 | var videos = require('../../../lib/videos') | 15 | var webtorrent = require('../../../lib/webtorrent') |
16 | var webtorrent = require('../../../lib/webtorrent') | 16 | |
17 | 17 | var router = express.Router() | |
18 | var router = express.Router() | 18 | var uploads = config.get('storage.uploads') |
19 | var uploads = config.get('storage.uploads') | 19 | |
20 | 20 | // multer configuration | |
21 | // multer configuration | 21 | var storage = multer.diskStorage({ |
22 | var storage = multer.diskStorage({ | 22 | destination: function (req, file, cb) { |
23 | destination: function (req, file, cb) { | 23 | cb(null, uploads) |
24 | cb(null, uploads) | 24 | }, |
25 | }, | 25 | |
26 | 26 | filename: function (req, file, cb) { | |
27 | filename: function (req, file, cb) { | 27 | var extension = '' |
28 | var extension = '' | 28 | if (file.mimetype === 'video/webm') extension = 'webm' |
29 | if (file.mimetype === 'video/webm') extension = 'webm' | 29 | else if (file.mimetype === 'video/mp4') extension = 'mp4' |
30 | else if (file.mimetype === 'video/mp4') extension = 'mp4' | 30 | else if (file.mimetype === 'video/ogg') extension = 'ogv' |
31 | else if (file.mimetype === 'video/ogg') extension = 'ogv' | 31 | crypto.pseudoRandomBytes(16, function (err, raw) { |
32 | crypto.pseudoRandomBytes(16, function (err, raw) { | 32 | var fieldname = err ? undefined : raw.toString('hex') |
33 | var fieldname = err ? undefined : raw.toString('hex') | 33 | cb(null, fieldname + '.' + extension) |
34 | cb(null, fieldname + '.' + extension) | 34 | }) |
35 | }) | 35 | } |
36 | } | 36 | }) |
37 | }) | 37 | |
38 | var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) | ||
38 | 39 | ||
39 | var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) | 40 | router.get('/', cacheMiddleware.cache(false), listVideos) |
41 | router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) | ||
42 | router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) | ||
43 | router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) | ||
44 | router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) | ||
40 | 45 | ||
41 | router.get('/', cacheMiddleware.cache(false), listVideos) | 46 | // --------------------------------------------------------------------------- |
42 | router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) | ||
43 | router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) | ||
44 | router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) | ||
45 | router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) | ||
46 | 47 | ||
47 | // --------------------------------------------------------------------------- | 48 | module.exports = router |
48 | 49 | ||
49 | module.exports = router | 50 | // --------------------------------------------------------------------------- |
50 | 51 | ||
51 | // --------------------------------------------------------------------------- | 52 | function addVideo (req, res, next) { |
53 | var video_file = req.files.input_video[0] | ||
54 | var video_infos = req.body | ||
55 | |||
56 | videos.seed(video_file.path, function (err, torrent) { | ||
57 | if (err) { | ||
58 | logger.error('Cannot seed this video.') | ||
59 | return next(err) | ||
60 | } | ||
52 | 61 | ||
53 | function addVideo (req, res, next) { | 62 | var video_data = { |
54 | var video_file = req.files.input_video[0] | 63 | name: video_infos.name, |
55 | var video_infos = req.body | 64 | namePath: video_file.filename, |
65 | description: video_infos.description, | ||
66 | magnetUri: torrent.magnetURI | ||
67 | } | ||
56 | 68 | ||
57 | videos.seed(video_file.path, function (err, torrent) { | 69 | Videos.add(video_data, function (err) { |
58 | if (err) { | 70 | if (err) { |
59 | logger.error('Cannot seed this video.') | 71 | // TODO unseed the video |
72 | logger.error('Cannot insert this video in the database.') | ||
60 | return next(err) | 73 | return next(err) |
61 | } | 74 | } |
62 | 75 | ||
63 | var video_data = { | 76 | // Now we'll add the video's meta data to our friends |
64 | name: video_infos.name, | 77 | friends.addVideoToFriends(video_data) |
65 | namePath: video_file.filename, | ||
66 | description: video_infos.description, | ||
67 | magnetUri: torrent.magnetURI | ||
68 | } | ||
69 | |||
70 | Videos.add(video_data, function (err) { | ||
71 | if (err) { | ||
72 | // TODO unseed the video | ||
73 | logger.error('Cannot insert this video in the database.') | ||
74 | return next(err) | ||
75 | } | ||
76 | |||
77 | // Now we'll add the video's meta data to our friends | ||
78 | friends.addVideoToFriends(video_data) | ||
79 | 78 | ||
80 | // TODO : include Location of the new video | 79 | // TODO : include Location of the new video |
81 | res.sendStatus(201) | 80 | res.sendStatus(201) |
82 | }) | ||
83 | }) | 81 | }) |
84 | } | 82 | }) |
83 | } | ||
85 | 84 | ||
86 | function getVideos (req, res, next) { | 85 | function getVideos (req, res, next) { |
87 | Videos.get(req.params.id, function (err, video) { | 86 | Videos.get(req.params.id, function (err, video) { |
88 | if (err) return next(err) | 87 | if (err) return next(err) |
89 | 88 | ||
90 | if (video === null) { | 89 | if (video === null) { |
91 | return res.sendStatus(404) | 90 | return res.sendStatus(404) |
92 | } | 91 | } |
93 | 92 | ||
94 | res.json(video) | 93 | res.json(video) |
95 | }) | 94 | }) |
96 | } | 95 | } |
97 | 96 | ||
98 | function listVideos (req, res, next) { | 97 | function listVideos (req, res, next) { |
99 | Videos.list(function (err, videos_list) { | 98 | Videos.list(function (err, videos_list) { |
100 | if (err) return next(err) | 99 | if (err) return next(err) |
101 | 100 | ||
102 | res.json(videos_list) | 101 | res.json(videos_list) |
103 | }) | 102 | }) |
104 | } | 103 | } |
105 | 104 | ||
106 | function removeVideo (req, res, next) { | 105 | function removeVideo (req, res, next) { |
107 | var video_id = req.params.id | 106 | var video_id = req.params.id |
108 | Videos.get(video_id, function (err, video) { | 107 | Videos.get(video_id, function (err, video) { |
109 | if (err) return next(err) | 108 | if (err) return next(err) |
110 | 109 | ||
111 | removeTorrent(video.magnetUri, function () { | 110 | removeTorrent(video.magnetUri, function () { |
112 | Videos.removeOwned(req.params.id, function (err) { | 111 | Videos.removeOwned(req.params.id, function (err) { |
113 | if (err) return next(err) | 112 | if (err) return next(err) |
114 | 113 | ||
115 | var params = { | 114 | var params = { |
116 | name: video.name, | 115 | name: video.name, |
117 | magnetUri: video.magnetUri | 116 | magnetUri: video.magnetUri |
118 | } | 117 | } |
119 | 118 | ||
120 | friends.removeVideoToFriends(params) | 119 | friends.removeVideoToFriends(params) |
121 | res.sendStatus(204) | 120 | res.sendStatus(204) |
122 | }) | ||
123 | }) | 121 | }) |
124 | }) | 122 | }) |
125 | } | 123 | }) |
124 | } | ||
126 | 125 | ||
127 | function searchVideos (req, res, next) { | 126 | function searchVideos (req, res, next) { |
128 | Videos.search(req.params.name, function (err, videos_list) { | 127 | Videos.search(req.params.name, function (err, videos_list) { |
129 | if (err) return next(err) | 128 | if (err) return next(err) |
130 | 129 | ||
131 | res.json(videos_list) | 130 | res.json(videos_list) |
132 | }) | 131 | }) |
133 | } | 132 | } |
134 | 133 | ||
135 | // --------------------------------------------------------------------------- | 134 | // --------------------------------------------------------------------------- |
136 | 135 | ||
137 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 136 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
138 | function removeTorrent (magnetUri, callback) { | 137 | function removeTorrent (magnetUri, callback) { |
139 | try { | 138 | try { |
140 | webtorrent.remove(magnetUri, callback) | 139 | webtorrent.remove(magnetUri, callback) |
141 | } catch (err) { | 140 | } catch (err) { |
142 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | 141 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) |
143 | return callback(null) | 142 | return callback(null) |
144 | } | ||
145 | } | 143 | } |
146 | })() | 144 | } |