]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const async = require('async') | |
4 | const config = require('config') | |
5 | const express = require('express') | |
6 | const mongoose = require('mongoose') | |
7 | const multer = require('multer') | |
8 | ||
9 | const logger = require('../../../helpers/logger') | |
10 | const friends = require('../../../lib/friends') | |
11 | const middlewares = require('../../../middlewares') | |
12 | const oAuth = middlewares.oauth | |
13 | const pagination = middlewares.pagination | |
14 | const validators = middlewares.validators | |
15 | const validatorsPagination = validators.pagination | |
16 | const validatorsSort = validators.sort | |
17 | const validatorsVideos = validators.videos | |
18 | const search = middlewares.search | |
19 | const sort = middlewares.sort | |
20 | const utils = require('../../../helpers/utils') | |
21 | ||
22 | const router = express.Router() | |
23 | const uploads = config.get('storage.uploads') | |
24 | const Video = mongoose.model('Video') | |
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 | ||
46 | router.get('/', | |
47 | validatorsPagination.pagination, | |
48 | validatorsSort.videosSort, | |
49 | sort.setVideosSort, | |
50 | pagination.setPagination, | |
51 | listVideos | |
52 | ) | |
53 | router.post('/', | |
54 | oAuth.authenticate, | |
55 | reqFiles, | |
56 | validatorsVideos.videosAdd, | |
57 | addVideo | |
58 | ) | |
59 | router.get('/:id', | |
60 | validatorsVideos.videosGet, | |
61 | getVideo | |
62 | ) | |
63 | router.delete('/:id', | |
64 | oAuth.authenticate, | |
65 | validatorsVideos.videosRemove, | |
66 | removeVideo | |
67 | ) | |
68 | router.get('/search/:value', | |
69 | validatorsVideos.videosSearch, | |
70 | validatorsPagination.pagination, | |
71 | validatorsSort.videosSort, | |
72 | sort.setVideosSort, | |
73 | pagination.setPagination, | |
74 | search.setVideosSearch, | |
75 | searchVideos | |
76 | ) | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | ||
80 | module.exports = router | |
81 | ||
82 | // --------------------------------------------------------------------------- | |
83 | ||
84 | function addVideo (req, res, next) { | |
85 | const videoFile = req.files.videofile[0] | |
86 | const videoInfos = req.body | |
87 | ||
88 | async.waterfall([ | |
89 | ||
90 | function insertIntoDB (callback) { | |
91 | const videoData = { | |
92 | name: videoInfos.name, | |
93 | filename: videoFile.filename, | |
94 | description: videoInfos.description, | |
95 | author: res.locals.oauth.token.user.username, | |
96 | duration: videoFile.duration, | |
97 | tags: videoInfos.tags | |
98 | } | |
99 | ||
100 | const video = new Video(videoData) | |
101 | video.save(function (err, video) { | |
102 | // Assert there are only one argument sent to the next function (video) | |
103 | return callback(err, video) | |
104 | }) | |
105 | }, | |
106 | ||
107 | function sendToFriends (video, callback) { | |
108 | video.toRemoteJSON(function (err, remoteVideo) { | |
109 | if (err) return callback(err) | |
110 | ||
111 | // Now we'll add the video's meta data to our friends | |
112 | friends.addVideoToFriends(remoteVideo) | |
113 | ||
114 | return callback(null) | |
115 | }) | |
116 | } | |
117 | ||
118 | ], function andFinally (err) { | |
119 | if (err) { | |
120 | // TODO unseed the video | |
121 | // TODO remove thumbnail | |
122 | // TODO delete from DB | |
123 | logger.error('Cannot insert the video.') | |
124 | return next(err) | |
125 | } | |
126 | ||
127 | // TODO : include Location of the new video -> 201 | |
128 | return res.type('json').status(204).end() | |
129 | }) | |
130 | } | |
131 | ||
132 | function getVideo (req, res, next) { | |
133 | Video.load(req.params.id, function (err, video) { | |
134 | if (err) return next(err) | |
135 | ||
136 | if (!video) { | |
137 | return res.type('json').status(204).end() | |
138 | } | |
139 | ||
140 | res.json(video.toFormatedJSON()) | |
141 | }) | |
142 | } | |
143 | ||
144 | function listVideos (req, res, next) { | |
145 | Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { | |
146 | if (err) return next(err) | |
147 | ||
148 | res.json(getFormatedVideos(videosList, videosTotal)) | |
149 | }) | |
150 | } | |
151 | ||
152 | function removeVideo (req, res, next) { | |
153 | const videoId = req.params.id | |
154 | ||
155 | async.waterfall([ | |
156 | function getVideo (callback) { | |
157 | Video.load(videoId, callback) | |
158 | }, | |
159 | ||
160 | function removeFromDB (video, callback) { | |
161 | video.remove(function (err) { | |
162 | if (err) return callback(err) | |
163 | ||
164 | return callback(null, video) | |
165 | }) | |
166 | }, | |
167 | ||
168 | function sendInformationToFriends (video, callback) { | |
169 | const params = { | |
170 | name: video.name, | |
171 | magnetUri: video.magnetUri | |
172 | } | |
173 | ||
174 | friends.removeVideoToFriends(params) | |
175 | ||
176 | return callback(null) | |
177 | } | |
178 | ], function andFinally (err) { | |
179 | if (err) { | |
180 | logger.error('Errors when removed the video.', { error: err }) | |
181 | return next(err) | |
182 | } | |
183 | ||
184 | return res.type('json').status(204).end() | |
185 | }) | |
186 | } | |
187 | ||
188 | function searchVideos (req, res, next) { | |
189 | Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, | |
190 | function (err, videosList, videosTotal) { | |
191 | if (err) return next(err) | |
192 | ||
193 | res.json(getFormatedVideos(videosList, videosTotal)) | |
194 | }) | |
195 | } | |
196 | ||
197 | // --------------------------------------------------------------------------- | |
198 | ||
199 | function getFormatedVideos (videos, videosTotal) { | |
200 | const formatedVideos = [] | |
201 | ||
202 | videos.forEach(function (video) { | |
203 | formatedVideos.push(video.toFormatedJSON()) | |
204 | }) | |
205 | ||
206 | return { | |
207 | total: videosTotal, | |
208 | data: formatedVideos | |
209 | } | |
210 | } |