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