]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
807df9e6 | 3 | const async = require('async') |
f0f5567b | 4 | const config = require('config') |
f0f5567b | 5 | const express = require('express') |
cbe2f7c3 C |
6 | const fs = require('fs') |
7 | const path = require('path') | |
f0f5567b C |
8 | const multer = require('multer') |
9 | ||
cbe2f7c3 | 10 | const constants = require('../../../initializers/constants') |
f0f5567b C |
11 | const logger = require('../../../helpers/logger') |
12 | const friends = require('../../../lib/friends') | |
b3b92647 C |
13 | const middlewares = require('../../../middlewares') |
14 | const oAuth2 = middlewares.oauth2 | |
fbf1134e C |
15 | const pagination = middlewares.pagination |
16 | const reqValidator = middlewares.reqValidators | |
17 | const reqValidatorPagination = reqValidator.pagination | |
18 | const reqValidatorVideos = reqValidator.videos | |
cbe2f7c3 | 19 | const utils = require('../../../helpers/utils') |
f0f5567b C |
20 | const Videos = require('../../../models/videos') // model |
21 | const videos = require('../../../lib/videos') | |
22 | const webtorrent = require('../../../lib/webtorrent') | |
23 | ||
24 | const router = express.Router() | |
25 | const uploads = config.get('storage.uploads') | |
9f10b292 C |
26 | |
27 | // multer configuration | |
f0f5567b | 28 | const storage = multer.diskStorage({ |
9f10b292 C |
29 | destination: function (req, file, cb) { |
30 | cb(null, uploads) | |
31 | }, | |
32 | ||
33 | filename: function (req, file, cb) { | |
f0f5567b | 34 | let extension = '' |
9f10b292 C |
35 | if (file.mimetype === 'video/webm') extension = 'webm' |
36 | else if (file.mimetype === 'video/mp4') extension = 'mp4' | |
37 | else if (file.mimetype === 'video/ogg') extension = 'ogv' | |
bc503c2a C |
38 | utils.generateRandomString(16, function (err, randomString) { |
39 | const fieldname = err ? undefined : randomString | |
9f10b292 C |
40 | cb(null, fieldname + '.' + extension) |
41 | }) | |
42 | } | |
43 | }) | |
44 | ||
8c9c1942 | 45 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
cbe2f7c3 | 46 | const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails')) |
8c308c2b | 47 | |
fbf1134e C |
48 | router.get('/', |
49 | reqValidatorPagination.pagination, | |
50 | pagination.setPagination, | |
51 | listVideos | |
52 | ) | |
53 | router.post('/', | |
54 | oAuth2.authenticate, | |
55 | reqFiles, | |
56 | reqValidatorVideos.videosAdd, | |
57 | addVideo | |
58 | ) | |
59 | router.get('/:id', | |
60 | reqValidatorVideos.videosGet, | |
61 | getVideos | |
62 | ) | |
63 | router.delete('/:id', | |
64 | oAuth2.authenticate, | |
65 | reqValidatorVideos.videosRemove, | |
66 | removeVideo | |
67 | ) | |
68 | router.get('/search/:name', | |
69 | reqValidatorVideos.videosSearch, | |
70 | reqValidatorPagination.pagination, | |
71 | pagination.setPagination, | |
72 | searchVideos | |
73 | ) | |
8c308c2b | 74 | |
9f10b292 | 75 | // --------------------------------------------------------------------------- |
c45f7f84 | 76 | |
9f10b292 | 77 | module.exports = router |
c45f7f84 | 78 | |
9f10b292 | 79 | // --------------------------------------------------------------------------- |
c45f7f84 | 80 | |
9f10b292 | 81 | function addVideo (req, res, next) { |
bc503c2a C |
82 | const videoFile = req.files.videofile[0] |
83 | const videoInfos = req.body | |
9f10b292 | 84 | |
807df9e6 | 85 | async.waterfall([ |
67100f1f | 86 | function seedTheVideo (callback) { |
807df9e6 C |
87 | videos.seed(videoFile.path, callback) |
88 | }, | |
8c308c2b | 89 | |
67100f1f | 90 | function createThumbnail (torrent, callback) { |
57a56079 | 91 | videos.createVideoThumbnail(videoFile.path, function (err, thumbnailName) { |
3a8a8b51 | 92 | if (err) { |
cbe2f7c3 C |
93 | // TODO: unseed the video |
94 | logger.error('Cannot make a thumbnail of the video file.') | |
807df9e6 | 95 | return callback(err) |
3a8a8b51 C |
96 | } |
97 | ||
67100f1f | 98 | callback(null, torrent, thumbnailName) |
807df9e6 C |
99 | }) |
100 | }, | |
101 | ||
67100f1f | 102 | function insertIntoDB (torrent, thumbnailName, callback) { |
807df9e6 C |
103 | const videoData = { |
104 | name: videoInfos.name, | |
105 | namePath: videoFile.filename, | |
106 | description: videoInfos.description, | |
107 | magnetUri: torrent.magnetURI, | |
108 | author: res.locals.oauth.token.user.username, | |
67100f1f | 109 | duration: videoFile.duration, |
807df9e6 C |
110 | thumbnail: thumbnailName |
111 | } | |
112 | ||
113 | Videos.add(videoData, function (err, insertedVideo) { | |
114 | if (err) { | |
115 | // TODO unseed the video | |
116 | // TODO remove thumbnail | |
117 | logger.error('Cannot insert this video in the database.') | |
118 | return callback(err) | |
cbe2f7c3 | 119 | } |
3a8a8b51 | 120 | |
67100f1f | 121 | return callback(null, torrent, thumbnailName, videoData, insertedVideo) |
3a8a8b51 | 122 | }) |
807df9e6 C |
123 | }, |
124 | ||
67100f1f | 125 | function getThumbnailBase64 (torrent, thumbnailName, videoData, insertedVideo, callback) { |
807df9e6 C |
126 | videoData.createdDate = insertedVideo.createdDate |
127 | ||
128 | fs.readFile(thumbnailsDir + thumbnailName, function (err, thumbnailData) { | |
129 | if (err) { | |
130 | // TODO unseed the video | |
131 | // TODO remove thumbnail | |
132 | // TODO: remove video | |
133 | logger.error('Cannot read the thumbnail of the video') | |
134 | return callback(err) | |
135 | } | |
136 | ||
137 | return callback(null, videoData, thumbnailData) | |
138 | }) | |
139 | }, | |
140 | ||
141 | function sendToFriends (videoData, thumbnailData, callback) { | |
142 | // Set the image in base64 | |
143 | videoData.thumbnailBase64 = new Buffer(thumbnailData).toString('base64') | |
144 | ||
145 | // Now we'll add the video's meta data to our friends | |
146 | friends.addVideoToFriends(videoData) | |
147 | ||
148 | return callback(null) | |
149 | } | |
150 | ||
151 | ], function (err) { | |
152 | if (err) { | |
153 | logger.error('Cannot insert the video.') | |
154 | return next(err) | |
155 | } | |
156 | ||
157 | // TODO : include Location of the new video -> 201 | |
158 | return res.type('json').status(204).end() | |
9f10b292 C |
159 | }) |
160 | } | |
8c308c2b | 161 | |
9f10b292 | 162 | function getVideos (req, res, next) { |
bc503c2a | 163 | Videos.get(req.params.id, function (err, videoObj) { |
9f10b292 | 164 | if (err) return next(err) |
8c308c2b | 165 | |
bc503c2a | 166 | const state = videos.getVideoState(videoObj) |
2df82d42 C |
167 | if (state.exist === false) { |
168 | return res.type('json').status(204).end() | |
9f10b292 | 169 | } |
8c308c2b | 170 | |
bc503c2a | 171 | res.json(getFormatedVideo(videoObj)) |
9f10b292 C |
172 | }) |
173 | } | |
8c308c2b | 174 | |
9f10b292 | 175 | function listVideos (req, res, next) { |
fbf1134e | 176 | Videos.list(req.query.start, req.query.count, function (err, videosList) { |
9f10b292 | 177 | if (err) return next(err) |
c45f7f84 | 178 | |
bc503c2a | 179 | res.json(getFormatedVideos(videosList)) |
9f10b292 C |
180 | }) |
181 | } | |
c45f7f84 | 182 | |
9f10b292 | 183 | function removeVideo (req, res, next) { |
bc503c2a | 184 | const videoId = req.params.id |
8c308c2b | 185 | |
807df9e6 C |
186 | async.waterfall([ |
187 | function getVideo (callback) { | |
188 | Videos.get(videoId, callback) | |
189 | }, | |
190 | ||
191 | function removeVideoTorrent (video, callback) { | |
192 | removeTorrent(video.magnetUri, function () { | |
193 | return callback(null, video) | |
194 | }) | |
195 | }, | |
196 | ||
197 | function removeFromDB (video, callback) { | |
9f10b292 | 198 | Videos.removeOwned(req.params.id, function (err) { |
807df9e6 | 199 | if (err) return callback(err) |
c173e565 | 200 | |
807df9e6 C |
201 | return callback(null, video) |
202 | }) | |
203 | }, | |
cbe2f7c3 | 204 | |
807df9e6 C |
205 | function removeVideoData (video, callback) { |
206 | videos.removeVideosDataFromDisk([ video ], function (err) { | |
207 | if (err) logger.error('Cannot remove video data from disk.', { video: video }) | |
c173e565 | 208 | |
807df9e6 | 209 | return callback(null, video) |
c173e565 | 210 | }) |
807df9e6 C |
211 | }, |
212 | ||
213 | function sendInformationToFriends (video, callback) { | |
214 | const params = { | |
215 | name: video.name, | |
216 | magnetUri: video.magnetUri | |
217 | } | |
218 | ||
219 | friends.removeVideoToFriends(params) | |
220 | ||
221 | return callback(null) | |
222 | } | |
223 | ], function (err) { | |
224 | if (err) { | |
225 | logger.error('Errors when removed the video.', { error: err }) | |
226 | return next(err) | |
227 | } | |
228 | ||
229 | return res.type('json').status(204).end() | |
9f10b292 C |
230 | }) |
231 | } | |
8c308c2b | 232 | |
9f10b292 | 233 | function searchVideos (req, res, next) { |
fbf1134e | 234 | Videos.search(req.params.name, req.query.start, req.query.count, function (err, videosList) { |
9f10b292 | 235 | if (err) return next(err) |
8c308c2b | 236 | |
bc503c2a | 237 | res.json(getFormatedVideos(videosList)) |
9f10b292 C |
238 | }) |
239 | } | |
c173e565 | 240 | |
9f10b292 | 241 | // --------------------------------------------------------------------------- |
c173e565 | 242 | |
bc503c2a C |
243 | function getFormatedVideo (videoObj) { |
244 | const formatedVideo = { | |
245 | id: videoObj._id, | |
246 | name: videoObj.name, | |
247 | description: videoObj.description, | |
248 | podUrl: videoObj.podUrl, | |
249 | isLocal: videos.getVideoState(videoObj).owned, | |
250 | magnetUri: videoObj.magnetUri, | |
251 | author: videoObj.author, | |
252 | duration: videoObj.duration, | |
bb10240e C |
253 | thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail, |
254 | createdDate: videoObj.createdDate | |
2df82d42 C |
255 | } |
256 | ||
bc503c2a | 257 | return formatedVideo |
2df82d42 C |
258 | } |
259 | ||
bc503c2a C |
260 | function getFormatedVideos (videosObj) { |
261 | const formatedVideos = [] | |
2df82d42 | 262 | |
bc503c2a C |
263 | videosObj.forEach(function (videoObj) { |
264 | formatedVideos.push(getFormatedVideo(videoObj)) | |
2df82d42 C |
265 | }) |
266 | ||
bc503c2a | 267 | return formatedVideos |
2df82d42 C |
268 | } |
269 | ||
9f10b292 C |
270 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
271 | function removeTorrent (magnetUri, callback) { | |
272 | try { | |
273 | webtorrent.remove(magnetUri, callback) | |
274 | } catch (err) { | |
275 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | |
276 | return callback(null) | |
c173e565 | 277 | } |
9f10b292 | 278 | } |