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