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