diff options
Diffstat (limited to 'controllers/api/v1/videos.js')
-rw-r--r-- | controllers/api/v1/videos.js | 77 |
1 files changed, 65 insertions, 12 deletions
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 | })() |