]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const express = require('express') | |
4 | const fs = require('fs') | |
5 | const multer = require('multer') | |
6 | const path = require('path') | |
7 | const waterfall = require('async/waterfall') | |
8 | ||
9 | const constants = require('../../initializers/constants') | |
10 | const db = require('../../initializers/database') | |
11 | const logger = require('../../helpers/logger') | |
12 | const friends = require('../../lib/friends') | |
13 | const middlewares = require('../../middlewares') | |
14 | const oAuth = middlewares.oauth | |
15 | const pagination = middlewares.pagination | |
16 | const validators = middlewares.validators | |
17 | const validatorsPagination = validators.pagination | |
18 | const validatorsSort = validators.sort | |
19 | const validatorsVideos = validators.videos | |
20 | const search = middlewares.search | |
21 | const sort = middlewares.sort | |
22 | const utils = require('../../helpers/utils') | |
23 | ||
24 | const router = express.Router() | |
25 | ||
26 | // multer configuration | |
27 | const storage = multer.diskStorage({ | |
28 | destination: function (req, file, cb) { | |
29 | cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR) | |
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 | ||
46 | router.get('/', | |
47 | validatorsPagination.pagination, | |
48 | validatorsSort.videosSort, | |
49 | sort.setVideosSort, | |
50 | pagination.setPagination, | |
51 | listVideos | |
52 | ) | |
53 | router.put('/:id', | |
54 | oAuth.authenticate, | |
55 | reqFiles, | |
56 | validatorsVideos.videosUpdate, | |
57 | updateVideo | |
58 | ) | |
59 | router.post('/', | |
60 | oAuth.authenticate, | |
61 | reqFiles, | |
62 | validatorsVideos.videosAdd, | |
63 | addVideo | |
64 | ) | |
65 | router.get('/:id', | |
66 | validatorsVideos.videosGet, | |
67 | getVideo | |
68 | ) | |
69 | router.delete('/:id', | |
70 | oAuth.authenticate, | |
71 | validatorsVideos.videosRemove, | |
72 | removeVideo | |
73 | ) | |
74 | router.get('/search/:value', | |
75 | validatorsVideos.videosSearch, | |
76 | validatorsPagination.pagination, | |
77 | validatorsSort.videosSort, | |
78 | sort.setVideosSort, | |
79 | pagination.setPagination, | |
80 | search.setVideosSearch, | |
81 | searchVideos | |
82 | ) | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | module.exports = router | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | function addVideo (req, res, next) { | |
91 | const videoFile = req.files.videofile[0] | |
92 | const videoInfos = req.body | |
93 | ||
94 | waterfall([ | |
95 | ||
96 | function startTransaction (callback) { | |
97 | db.sequelize.transaction().asCallback(function (err, t) { | |
98 | return callback(err, t) | |
99 | }) | |
100 | }, | |
101 | ||
102 | function findOrCreateAuthor (t, callback) { | |
103 | const user = res.locals.oauth.token.User | |
104 | ||
105 | const name = user.username | |
106 | // null because it is OUR pod | |
107 | const podId = null | |
108 | const userId = user.id | |
109 | ||
110 | db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) { | |
111 | return callback(err, t, authorInstance) | |
112 | }) | |
113 | }, | |
114 | ||
115 | function findOrCreateTags (t, author, callback) { | |
116 | const tags = videoInfos.tags | |
117 | ||
118 | db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) { | |
119 | return callback(err, t, author, tagInstances) | |
120 | }) | |
121 | }, | |
122 | ||
123 | function createVideoObject (t, author, tagInstances, callback) { | |
124 | const videoData = { | |
125 | name: videoInfos.name, | |
126 | remoteId: null, | |
127 | extname: path.extname(videoFile.filename), | |
128 | description: videoInfos.description, | |
129 | duration: videoFile.duration, | |
130 | authorId: author.id | |
131 | } | |
132 | ||
133 | const video = db.Video.build(videoData) | |
134 | ||
135 | return callback(null, t, author, tagInstances, video) | |
136 | }, | |
137 | ||
138 | // Set the videoname the same as the id | |
139 | function renameVideoFile (t, author, tagInstances, video, callback) { | |
140 | const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR | |
141 | const source = path.join(videoDir, videoFile.filename) | |
142 | const destination = path.join(videoDir, video.getVideoFilename()) | |
143 | ||
144 | fs.rename(source, destination, function (err) { | |
145 | return callback(err, t, author, tagInstances, video) | |
146 | }) | |
147 | }, | |
148 | ||
149 | function insertVideoIntoDB (t, author, tagInstances, video, callback) { | |
150 | const options = { transaction: t } | |
151 | ||
152 | // Add tags association | |
153 | video.save(options).asCallback(function (err, videoCreated) { | |
154 | if (err) return callback(err) | |
155 | ||
156 | // Do not forget to add Author informations to the created video | |
157 | videoCreated.Author = author | |
158 | ||
159 | return callback(err, t, tagInstances, videoCreated) | |
160 | }) | |
161 | }, | |
162 | ||
163 | function associateTagsToVideo (t, tagInstances, video, callback) { | |
164 | const options = { transaction: t } | |
165 | ||
166 | video.setTags(tagInstances, options).asCallback(function (err) { | |
167 | video.Tags = tagInstances | |
168 | ||
169 | return callback(err, t, video) | |
170 | }) | |
171 | }, | |
172 | ||
173 | function sendToFriends (t, video, callback) { | |
174 | video.toAddRemoteJSON(function (err, remoteVideo) { | |
175 | if (err) return callback(err) | |
176 | ||
177 | // Now we'll add the video's meta data to our friends | |
178 | friends.addVideoToFriends(remoteVideo) | |
179 | ||
180 | return callback(null, t) | |
181 | }) | |
182 | } | |
183 | ||
184 | ], function andFinally (err, t) { | |
185 | if (err) { | |
186 | logger.error('Cannot insert the video.') | |
187 | ||
188 | // Abort transaction? | |
189 | if (t) t.rollback() | |
190 | ||
191 | return next(err) | |
192 | } | |
193 | ||
194 | // Commit transaction | |
195 | t.commit() | |
196 | ||
197 | // TODO : include Location of the new video -> 201 | |
198 | return res.type('json').status(204).end() | |
199 | }) | |
200 | } | |
201 | ||
202 | function updateVideo (req, res, next) { | |
203 | let videoInstance = res.locals.video | |
204 | const videoInfosToUpdate = req.body | |
205 | ||
206 | waterfall([ | |
207 | ||
208 | function startTransaction (callback) { | |
209 | db.sequelize.transaction().asCallback(function (err, t) { | |
210 | return callback(err, t) | |
211 | }) | |
212 | }, | |
213 | ||
214 | function findOrCreateTags (t, callback) { | |
215 | if (videoInfosToUpdate.tags) { | |
216 | db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) { | |
217 | return callback(err, t, tagInstances) | |
218 | }) | |
219 | } else { | |
220 | return callback(null, t, null) | |
221 | } | |
222 | }, | |
223 | ||
224 | function updateVideoIntoDB (t, tagInstances, callback) { | |
225 | const options = { transaction: t } | |
226 | ||
227 | if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name) | |
228 | if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description) | |
229 | ||
230 | // Add tags association | |
231 | videoInstance.save(options).asCallback(function (err) { | |
232 | if (err) return callback(err) | |
233 | ||
234 | return callback(err, t, tagInstances) | |
235 | }) | |
236 | }, | |
237 | ||
238 | function associateTagsToVideo (t, tagInstances, callback) { | |
239 | if (tagInstances) { | |
240 | const options = { transaction: t } | |
241 | ||
242 | videoInstance.setTags(tagInstances, options).asCallback(function (err) { | |
243 | videoInstance.Tags = tagInstances | |
244 | ||
245 | return callback(err, t) | |
246 | }) | |
247 | } else { | |
248 | return callback(null, t) | |
249 | } | |
250 | }, | |
251 | ||
252 | function sendToFriends (t, callback) { | |
253 | const json = videoInstance.toUpdateRemoteJSON() | |
254 | ||
255 | // Now we'll update the video's meta data to our friends | |
256 | friends.updateVideoToFriends(json) | |
257 | ||
258 | return callback(null, t) | |
259 | } | |
260 | ||
261 | ], function andFinally (err, t) { | |
262 | if (err) { | |
263 | logger.error('Cannot insert the video.') | |
264 | ||
265 | // Abort transaction? | |
266 | if (t) t.rollback() | |
267 | ||
268 | return next(err) | |
269 | } | |
270 | ||
271 | // Commit transaction | |
272 | t.commit() | |
273 | ||
274 | // TODO : include Location of the new video -> 201 | |
275 | return res.type('json').status(204).end() | |
276 | }) | |
277 | } | |
278 | ||
279 | function getVideo (req, res, next) { | |
280 | db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) { | |
281 | if (err) return next(err) | |
282 | ||
283 | if (!video) { | |
284 | return res.type('json').status(204).end() | |
285 | } | |
286 | ||
287 | res.json(video.toFormatedJSON()) | |
288 | }) | |
289 | } | |
290 | ||
291 | function listVideos (req, res, next) { | |
292 | db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { | |
293 | if (err) return next(err) | |
294 | ||
295 | res.json(getFormatedVideos(videosList, videosTotal)) | |
296 | }) | |
297 | } | |
298 | ||
299 | function removeVideo (req, res, next) { | |
300 | const videoId = req.params.id | |
301 | ||
302 | waterfall([ | |
303 | function loadVideo (callback) { | |
304 | db.Video.load(videoId, function (err, video) { | |
305 | return callback(err, video) | |
306 | }) | |
307 | }, | |
308 | ||
309 | function deleteVideo (video, callback) { | |
310 | // Informations to other pods will be sent by the afterDestroy video hook | |
311 | video.destroy().asCallback(callback) | |
312 | } | |
313 | ], function andFinally (err) { | |
314 | if (err) { | |
315 | logger.error('Errors when removed the video.', { error: err }) | |
316 | return next(err) | |
317 | } | |
318 | ||
319 | return res.type('json').status(204).end() | |
320 | }) | |
321 | } | |
322 | ||
323 | function searchVideos (req, res, next) { | |
324 | db.Video.searchAndPopulateAuthorAndPodAndTags( | |
325 | req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, | |
326 | function (err, videosList, videosTotal) { | |
327 | if (err) return next(err) | |
328 | ||
329 | res.json(getFormatedVideos(videosList, videosTotal)) | |
330 | } | |
331 | ) | |
332 | } | |
333 | ||
334 | // --------------------------------------------------------------------------- | |
335 | ||
336 | function getFormatedVideos (videos, videosTotal) { | |
337 | const formatedVideos = [] | |
338 | ||
339 | videos.forEach(function (video) { | |
340 | formatedVideos.push(video.toFormatedJSON()) | |
341 | }) | |
342 | ||
343 | return { | |
344 | total: videosTotal, | |
345 | data: formatedVideos | |
346 | } | |
347 | } |