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