diff options
Diffstat (limited to 'server/controllers/api/v1')
-rw-r--r-- | server/controllers/api/v1/pods.js | 17 | ||||
-rw-r--r-- | server/controllers/api/v1/remote.js | 45 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 124 |
3 files changed, 51 insertions, 135 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 881b2090d..9dd9197b3 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
5 | 6 | ||
6 | const logger = require('../../../helpers/logger') | 7 | const logger = require('../../../helpers/logger') |
7 | const friends = require('../../../lib/friends') | 8 | const friends = require('../../../lib/friends') |
@@ -10,10 +11,9 @@ const Pods = require('../../../models/pods') | |||
10 | const oAuth2 = middlewares.oauth2 | 11 | const oAuth2 = middlewares.oauth2 |
11 | const reqValidator = middlewares.reqValidators.pods | 12 | const reqValidator = middlewares.reqValidators.pods |
12 | const signatureValidator = middlewares.reqValidators.remote.signature | 13 | const signatureValidator = middlewares.reqValidators.remote.signature |
13 | const videos = require('../../../lib/videos') | ||
14 | const Videos = require('../../../models/videos') | ||
15 | 14 | ||
16 | const router = express.Router() | 15 | const router = express.Router() |
16 | const Video = mongoose.model('Video') | ||
17 | 17 | ||
18 | router.get('/', listPodsUrl) | 18 | router.get('/', listPodsUrl) |
19 | router.post('/', reqValidator.podsAdd, addPods) | 19 | router.post('/', reqValidator.podsAdd, addPods) |
@@ -86,7 +86,7 @@ function removePods (req, res, next) { | |||
86 | }, | 86 | }, |
87 | 87 | ||
88 | function (callback) { | 88 | function (callback) { |
89 | Videos.listFromUrl(url, function (err, videosList) { | 89 | Video.listByUrls([ url ], function (err, videosList) { |
90 | if (err) { | 90 | if (err) { |
91 | logger.error('Cannot list videos from url.', { error: err }) | 91 | logger.error('Cannot list videos from url.', { error: err }) |
92 | return callback(err) | 92 | return callback(err) |
@@ -97,14 +97,9 @@ function removePods (req, res, next) { | |||
97 | }, | 97 | }, |
98 | 98 | ||
99 | function removeTheRemoteVideos (videosList, callback) { | 99 | function removeTheRemoteVideos (videosList, callback) { |
100 | videos.removeRemoteVideos(videosList, function (err) { | 100 | async.each(videosList, function (video, callbackEach) { |
101 | if (err) { | 101 | video.remove(callbackEach) |
102 | logger.error('Cannot remove remote videos.', { error: err }) | 102 | }, callback) |
103 | return callback(err) | ||
104 | } | ||
105 | |||
106 | return callback(null) | ||
107 | }) | ||
108 | } | 103 | } |
109 | ], function (err) { | 104 | ], function (err) { |
110 | if (err) return next(err) | 105 | if (err) return next(err) |
diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js index ced8470d7..2d71c605d 100644 --- a/server/controllers/api/v1/remote.js +++ b/server/controllers/api/v1/remote.js | |||
@@ -2,15 +2,15 @@ | |||
2 | 2 | ||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
5 | 6 | ||
6 | const middlewares = require('../../../middlewares') | 7 | const middlewares = require('../../../middlewares') |
7 | const secureMiddleware = middlewares.secure | 8 | const secureMiddleware = middlewares.secure |
8 | const reqValidator = middlewares.reqValidators.remote | 9 | const reqValidator = middlewares.reqValidators.remote |
9 | const logger = require('../../../helpers/logger') | 10 | const logger = require('../../../helpers/logger') |
10 | const Videos = require('../../../models/videos') | ||
11 | const videos = require('../../../lib/videos') | ||
12 | 11 | ||
13 | const router = express.Router() | 12 | const router = express.Router() |
13 | const Video = mongoose.model('Video') | ||
14 | 14 | ||
15 | router.post('/videos', | 15 | router.post('/videos', |
16 | reqValidator.signature, | 16 | reqValidator.signature, |
@@ -33,48 +33,39 @@ function remoteVideos (req, res, next) { | |||
33 | // We need to process in the same order to keep consistency | 33 | // We need to process in the same order to keep consistency |
34 | // TODO: optimization | 34 | // TODO: optimization |
35 | async.eachSeries(requests, function (request, callbackEach) { | 35 | async.eachSeries(requests, function (request, callbackEach) { |
36 | const video = request.data | 36 | const videoData = request.data |
37 | 37 | ||
38 | if (request.type === 'add') { | 38 | if (request.type === 'add') { |
39 | addRemoteVideo(video, callbackEach) | 39 | addRemoteVideo(videoData, callbackEach) |
40 | } else if (request.type === 'remove') { | 40 | } else if (request.type === 'remove') { |
41 | removeRemoteVideo(video, fromUrl, callbackEach) | 41 | removeRemoteVideo(videoData, fromUrl, callbackEach) |
42 | } | 42 | } |
43 | }, function (err) { | ||
44 | if (err) logger.error('Error managing remote videos.', { error: err }) | ||
43 | }) | 45 | }) |
44 | 46 | ||
45 | // We don't need to keep the other pod waiting | 47 | // We don't need to keep the other pod waiting |
46 | return res.type('json').status(204).end() | 48 | return res.type('json').status(204).end() |
47 | } | 49 | } |
48 | 50 | ||
49 | function addRemoteVideo (videoToCreate, callback) { | 51 | function addRemoteVideo (videoToCreateData, callback) { |
50 | videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) { | 52 | // Mongoose pre hook will automatically create the thumbnail on disk |
51 | if (err) { | 53 | videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64 |
52 | logger.error('Cannot create remote videos.', { error: err }) | ||
53 | // Don't break the process | ||
54 | } | ||
55 | 54 | ||
56 | return callback() | 55 | const video = new Video(videoToCreateData) |
57 | }) | 56 | video.save(callback) |
58 | } | 57 | } |
59 | 58 | ||
60 | function removeRemoteVideo (videoToRemove, fromUrl, callback) { | 59 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { |
61 | const magnetUris = [ videoToRemove.magnetUri ] | ||
62 | |||
63 | // We need the list because we have to remove some other stuffs (thumbnail etc) | 60 | // We need the list because we have to remove some other stuffs (thumbnail etc) |
64 | Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) { | 61 | Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) { |
65 | if (err) { | 62 | if (err) { |
66 | logger.error('Cannot list videos from url and magnets.', { error: err }) | 63 | logger.error('Cannot list videos from url and magnets.', { error: err }) |
67 | // Don't break the process | 64 | return callback(err) |
68 | return callback() | ||
69 | } | 65 | } |
70 | 66 | ||
71 | videos.removeRemoteVideos(videosList, function (err) { | 67 | async.each(videosList, function (video, callbackEach) { |
72 | if (err) { | 68 | video.remove(callbackEach) |
73 | logger.error('Cannot remove remote videos.', { error: err }) | 69 | }, callback) |
74 | // Don't break the process | ||
75 | } | ||
76 | |||
77 | return callback() | ||
78 | }) | ||
79 | }) | 70 | }) |
80 | } | 71 | } |
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 2edb31122..83734b35e 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -3,9 +3,9 @@ | |||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const config = require('config') | 4 | const config = require('config') |
5 | const express = require('express') | 5 | const express = require('express') |
6 | const mongoose = require('mongoose') | ||
6 | const multer = require('multer') | 7 | const multer = require('multer') |
7 | 8 | ||
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') |
@@ -18,12 +18,10 @@ const reqValidatorVideos = reqValidator.videos | |||
18 | const search = middlewares.search | 18 | const search = middlewares.search |
19 | const sort = middlewares.sort | 19 | const sort = middlewares.sort |
20 | const utils = require('../../../helpers/utils') | 20 | const utils = require('../../../helpers/utils') |
21 | const Videos = require('../../../models/videos') // model | ||
22 | const videos = require('../../../lib/videos') | ||
23 | const webtorrent = require('../../../lib/webtorrent') | ||
24 | 21 | ||
25 | const router = express.Router() | 22 | const router = express.Router() |
26 | const uploads = config.get('storage.uploads') | 23 | const uploads = config.get('storage.uploads') |
24 | const Video = mongoose.model('Video') | ||
27 | 25 | ||
28 | // multer configuration | 26 | // multer configuration |
29 | const storage = multer.diskStorage({ | 27 | const storage = multer.diskStorage({ |
@@ -88,55 +86,27 @@ function addVideo (req, res, next) { | |||
88 | const videoInfos = req.body | 86 | const videoInfos = req.body |
89 | 87 | ||
90 | async.waterfall([ | 88 | async.waterfall([ |
91 | function seedTheVideo (callback) { | ||
92 | videos.seed(videoFile.path, callback) | ||
93 | }, | ||
94 | |||
95 | function createThumbnail (torrent, callback) { | ||
96 | videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) { | ||
97 | if (err) { | ||
98 | // TODO: unseed the video | ||
99 | logger.error('Cannot make a thumbnail of the video file.') | ||
100 | return callback(err) | ||
101 | } | ||
102 | |||
103 | callback(null, torrent, thumbnailName) | ||
104 | }) | ||
105 | }, | ||
106 | 89 | ||
107 | function insertIntoDB (torrent, thumbnailName, callback) { | 90 | function insertIntoDB (callback) { |
108 | const videoData = { | 91 | const videoData = { |
109 | name: videoInfos.name, | 92 | name: videoInfos.name, |
110 | namePath: videoFile.filename, | 93 | namePath: videoFile.filename, |
111 | description: videoInfos.description, | 94 | description: videoInfos.description, |
112 | magnetUri: torrent.magnetURI, | ||
113 | author: res.locals.oauth.token.user.username, | 95 | author: res.locals.oauth.token.user.username, |
114 | duration: videoFile.duration, | 96 | duration: videoFile.duration, |
115 | thumbnail: thumbnailName, | ||
116 | tags: videoInfos.tags | 97 | tags: videoInfos.tags |
117 | } | 98 | } |
118 | 99 | ||
119 | Videos.add(videoData, function (err, insertedVideo) { | 100 | const video = new Video(videoData) |
120 | if (err) { | 101 | video.save(function (err, video) { |
121 | // TODO unseed the video | 102 | // Assert there are only one argument sent to the next function (video) |
122 | // TODO remove thumbnail | 103 | return callback(err, video) |
123 | logger.error('Cannot insert this video in the database.') | ||
124 | return callback(err) | ||
125 | } | ||
126 | |||
127 | return callback(null, insertedVideo) | ||
128 | }) | 104 | }) |
129 | }, | 105 | }, |
130 | 106 | ||
131 | function sendToFriends (insertedVideo, callback) { | 107 | function sendToFriends (video, callback) { |
132 | videos.convertVideoToRemote(insertedVideo, function (err, remoteVideo) { | 108 | video.toRemoteJSON(function (err, remoteVideo) { |
133 | if (err) { | 109 | if (err) return callback(err) |
134 | // TODO unseed the video | ||
135 | // TODO remove thumbnail | ||
136 | // TODO delete from DB | ||
137 | logger.error('Cannot convert video to remote.') | ||
138 | return callback(err) | ||
139 | } | ||
140 | 110 | ||
141 | // Now we'll add the video's meta data to our friends | 111 | // Now we'll add the video's meta data to our friends |
142 | friends.addVideoToFriends(remoteVideo) | 112 | friends.addVideoToFriends(remoteVideo) |
@@ -147,6 +117,9 @@ function addVideo (req, res, next) { | |||
147 | 117 | ||
148 | ], function andFinally (err) { | 118 | ], function andFinally (err) { |
149 | if (err) { | 119 | if (err) { |
120 | // TODO unseed the video | ||
121 | // TODO remove thumbnail | ||
122 | // TODO delete from DB | ||
150 | logger.error('Cannot insert the video.') | 123 | logger.error('Cannot insert the video.') |
151 | return next(err) | 124 | return next(err) |
152 | } | 125 | } |
@@ -157,23 +130,22 @@ function addVideo (req, res, next) { | |||
157 | } | 130 | } |
158 | 131 | ||
159 | function getVideo (req, res, next) { | 132 | function getVideo (req, res, next) { |
160 | Videos.get(req.params.id, function (err, videoObj) { | 133 | Video.load(req.params.id, function (err, video) { |
161 | if (err) return next(err) | 134 | if (err) return next(err) |
162 | 135 | ||
163 | const state = videos.getVideoState(videoObj) | 136 | if (!video) { |
164 | if (state.exist === false) { | ||
165 | return res.type('json').status(204).end() | 137 | return res.type('json').status(204).end() |
166 | } | 138 | } |
167 | 139 | ||
168 | res.json(getFormatedVideo(videoObj)) | 140 | res.json(video.toFormatedJSON()) |
169 | }) | 141 | }) |
170 | } | 142 | } |
171 | 143 | ||
172 | function listVideos (req, res, next) { | 144 | function listVideos (req, res, next) { |
173 | Videos.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, totalVideos) { | 145 | Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { |
174 | if (err) return next(err) | 146 | if (err) return next(err) |
175 | 147 | ||
176 | res.json(getFormatedVideos(videosList, totalVideos)) | 148 | res.json(getFormatedVideos(videosList, videosTotal)) |
177 | }) | 149 | }) |
178 | } | 150 | } |
179 | 151 | ||
@@ -182,31 +154,17 @@ function removeVideo (req, res, next) { | |||
182 | 154 | ||
183 | async.waterfall([ | 155 | async.waterfall([ |
184 | function getVideo (callback) { | 156 | function getVideo (callback) { |
185 | Videos.get(videoId, callback) | 157 | Video.load(videoId, callback) |
186 | }, | ||
187 | |||
188 | function removeVideoTorrent (video, callback) { | ||
189 | removeTorrent(video.magnetUri, function () { | ||
190 | return callback(null, video) | ||
191 | }) | ||
192 | }, | 158 | }, |
193 | 159 | ||
194 | function removeFromDB (video, callback) { | 160 | function removeFromDB (video, callback) { |
195 | Videos.removeOwned(req.params.id, function (err) { | 161 | video.remove(function (err) { |
196 | if (err) return callback(err) | 162 | if (err) return callback(err) |
197 | 163 | ||
198 | return callback(null, video) | 164 | return callback(null, video) |
199 | }) | 165 | }) |
200 | }, | 166 | }, |
201 | 167 | ||
202 | function removeVideoData (video, callback) { | ||
203 | videos.removeVideosDataFromDisk([ video ], function (err) { | ||
204 | if (err) logger.error('Cannot remove video data from disk.', { video: video }) | ||
205 | |||
206 | return callback(null, video) | ||
207 | }) | ||
208 | }, | ||
209 | |||
210 | function sendInformationToFriends (video, callback) { | 168 | function sendInformationToFriends (video, callback) { |
211 | const params = { | 169 | const params = { |
212 | name: video.name, | 170 | name: video.name, |
@@ -228,53 +186,25 @@ function removeVideo (req, res, next) { | |||
228 | } | 186 | } |
229 | 187 | ||
230 | function searchVideos (req, res, next) { | 188 | function searchVideos (req, res, next) { |
231 | Videos.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, | 189 | Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, |
232 | function (err, videosList, totalVideos) { | 190 | function (err, videosList, videosTotal) { |
233 | if (err) return next(err) | 191 | if (err) return next(err) |
234 | 192 | ||
235 | res.json(getFormatedVideos(videosList, totalVideos)) | 193 | res.json(getFormatedVideos(videosList, videosTotal)) |
236 | }) | 194 | }) |
237 | } | 195 | } |
238 | 196 | ||
239 | // --------------------------------------------------------------------------- | 197 | // --------------------------------------------------------------------------- |
240 | 198 | ||
241 | function getFormatedVideo (videoObj) { | 199 | function getFormatedVideos (videos, videosTotal) { |
242 | const formatedVideo = { | ||
243 | id: videoObj._id, | ||
244 | name: videoObj.name, | ||
245 | description: videoObj.description, | ||
246 | podUrl: videoObj.podUrl.replace(/^https?:\/\//, ''), | ||
247 | isLocal: videos.getVideoState(videoObj).owned, | ||
248 | magnetUri: videoObj.magnetUri, | ||
249 | author: videoObj.author, | ||
250 | duration: videoObj.duration, | ||
251 | tags: videoObj.tags, | ||
252 | thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail, | ||
253 | createdDate: videoObj.createdDate | ||
254 | } | ||
255 | |||
256 | return formatedVideo | ||
257 | } | ||
258 | |||
259 | function getFormatedVideos (videosObj, totalVideos) { | ||
260 | const formatedVideos = [] | 200 | const formatedVideos = [] |
261 | 201 | ||
262 | videosObj.forEach(function (videoObj) { | 202 | videos.forEach(function (video) { |
263 | formatedVideos.push(getFormatedVideo(videoObj)) | 203 | formatedVideos.push(video.toFormatedJSON()) |
264 | }) | 204 | }) |
265 | 205 | ||
266 | return { | 206 | return { |
267 | total: totalVideos, | 207 | total: videosTotal, |
268 | data: formatedVideos | 208 | data: formatedVideos |
269 | } | 209 | } |
270 | } | 210 | } |
271 | |||
272 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | ||
273 | function removeTorrent (magnetUri, callback) { | ||
274 | try { | ||
275 | webtorrent.remove(magnetUri, callback) | ||
276 | } catch (err) { | ||
277 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | ||
278 | return callback(null) | ||
279 | } | ||
280 | } | ||