]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const express = require('express') |
aaf61f38 | 4 | const mongoose = require('mongoose') |
f0f5567b | 5 | const multer = require('multer') |
1a42c9e2 | 6 | const waterfall = require('async/waterfall') |
f0f5567b | 7 | |
e861452f | 8 | const constants = require('../../../initializers/constants') |
f0f5567b C |
9 | const logger = require('../../../helpers/logger') |
10 | const friends = require('../../../lib/friends') | |
b3b92647 | 11 | const middlewares = require('../../../middlewares') |
69b0a27c | 12 | const oAuth = middlewares.oauth |
fbf1134e | 13 | const pagination = middlewares.pagination |
fc51fde0 C |
14 | const validators = middlewares.validators |
15 | const validatorsPagination = validators.pagination | |
16 | const validatorsSort = validators.sort | |
17 | const validatorsVideos = validators.videos | |
46246b5f | 18 | const search = middlewares.search |
a877d5ac | 19 | const sort = middlewares.sort |
cbe2f7c3 | 20 | const utils = require('../../../helpers/utils') |
f0f5567b C |
21 | |
22 | const router = express.Router() | |
aaf61f38 | 23 | const Video = mongoose.model('Video') |
9f10b292 C |
24 | |
25 | // multer configuration | |
f0f5567b | 26 | const storage = multer.diskStorage({ |
9f10b292 | 27 | destination: function (req, file, cb) { |
b3d92510 | 28 | cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR) |
9f10b292 C |
29 | }, |
30 | ||
31 | filename: function (req, file, cb) { | |
f0f5567b | 32 | let extension = '' |
9f10b292 C |
33 | if (file.mimetype === 'video/webm') extension = 'webm' |
34 | else if (file.mimetype === 'video/mp4') extension = 'mp4' | |
35 | else if (file.mimetype === 'video/ogg') extension = 'ogv' | |
bc503c2a C |
36 | utils.generateRandomString(16, function (err, randomString) { |
37 | const fieldname = err ? undefined : randomString | |
9f10b292 C |
38 | cb(null, fieldname + '.' + extension) |
39 | }) | |
40 | } | |
41 | }) | |
42 | ||
8c9c1942 | 43 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
8c308c2b | 44 | |
fbf1134e | 45 | router.get('/', |
fc51fde0 C |
46 | validatorsPagination.pagination, |
47 | validatorsSort.videosSort, | |
a877d5ac | 48 | sort.setVideosSort, |
fbf1134e C |
49 | pagination.setPagination, |
50 | listVideos | |
51 | ) | |
52 | router.post('/', | |
69b0a27c | 53 | oAuth.authenticate, |
fbf1134e | 54 | reqFiles, |
fc51fde0 | 55 | validatorsVideos.videosAdd, |
fbf1134e C |
56 | addVideo |
57 | ) | |
58 | router.get('/:id', | |
fc51fde0 | 59 | validatorsVideos.videosGet, |
68ce3ae0 | 60 | getVideo |
fbf1134e C |
61 | ) |
62 | router.delete('/:id', | |
69b0a27c | 63 | oAuth.authenticate, |
fc51fde0 | 64 | validatorsVideos.videosRemove, |
fbf1134e C |
65 | removeVideo |
66 | ) | |
46246b5f | 67 | router.get('/search/:value', |
fc51fde0 C |
68 | validatorsVideos.videosSearch, |
69 | validatorsPagination.pagination, | |
70 | validatorsSort.videosSort, | |
a877d5ac | 71 | sort.setVideosSort, |
fbf1134e | 72 | pagination.setPagination, |
46246b5f | 73 | search.setVideosSearch, |
fbf1134e C |
74 | searchVideos |
75 | ) | |
8c308c2b | 76 | |
9f10b292 | 77 | // --------------------------------------------------------------------------- |
c45f7f84 | 78 | |
9f10b292 | 79 | module.exports = router |
c45f7f84 | 80 | |
9f10b292 | 81 | // --------------------------------------------------------------------------- |
c45f7f84 | 82 | |
9f10b292 | 83 | function addVideo (req, res, next) { |
bc503c2a C |
84 | const videoFile = req.files.videofile[0] |
85 | const videoInfos = req.body | |
9f10b292 | 86 | |
1a42c9e2 | 87 | waterfall([ |
807df9e6 | 88 | |
aaf61f38 | 89 | function insertIntoDB (callback) { |
807df9e6 C |
90 | const videoData = { |
91 | name: videoInfos.name, | |
5189d08a | 92 | filename: videoFile.filename, |
807df9e6 | 93 | description: videoInfos.description, |
807df9e6 | 94 | author: res.locals.oauth.token.user.username, |
67100f1f | 95 | duration: videoFile.duration, |
be587647 | 96 | tags: videoInfos.tags |
807df9e6 C |
97 | } |
98 | ||
aaf61f38 C |
99 | const video = new Video(videoData) |
100 | video.save(function (err, video) { | |
101 | // Assert there are only one argument sent to the next function (video) | |
102 | return callback(err, video) | |
3a8a8b51 | 103 | }) |
807df9e6 C |
104 | }, |
105 | ||
aaf61f38 C |
106 | function sendToFriends (video, callback) { |
107 | video.toRemoteJSON(function (err, remoteVideo) { | |
108 | if (err) return callback(err) | |
807df9e6 | 109 | |
528a9efa C |
110 | // Now we'll add the video's meta data to our friends |
111 | friends.addVideoToFriends(remoteVideo) | |
807df9e6 | 112 | |
528a9efa C |
113 | return callback(null) |
114 | }) | |
807df9e6 C |
115 | } |
116 | ||
be587647 | 117 | ], function andFinally (err) { |
807df9e6 C |
118 | if (err) { |
119 | logger.error('Cannot insert the video.') | |
120 | return next(err) | |
121 | } | |
122 | ||
123 | // TODO : include Location of the new video -> 201 | |
124 | return res.type('json').status(204).end() | |
9f10b292 C |
125 | }) |
126 | } | |
8c308c2b | 127 | |
68ce3ae0 | 128 | function getVideo (req, res, next) { |
aaf61f38 | 129 | Video.load(req.params.id, function (err, video) { |
9f10b292 | 130 | if (err) return next(err) |
8c308c2b | 131 | |
aaf61f38 | 132 | if (!video) { |
2df82d42 | 133 | return res.type('json').status(204).end() |
9f10b292 | 134 | } |
8c308c2b | 135 | |
aaf61f38 | 136 | res.json(video.toFormatedJSON()) |
9f10b292 C |
137 | }) |
138 | } | |
8c308c2b | 139 | |
9f10b292 | 140 | function listVideos (req, res, next) { |
0ff21c1c | 141 | Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { |
9f10b292 | 142 | if (err) return next(err) |
c45f7f84 | 143 | |
aaf61f38 | 144 | res.json(getFormatedVideos(videosList, videosTotal)) |
9f10b292 C |
145 | }) |
146 | } | |
c45f7f84 | 147 | |
9f10b292 | 148 | function removeVideo (req, res, next) { |
bc503c2a | 149 | const videoId = req.params.id |
8c308c2b | 150 | |
1a42c9e2 | 151 | waterfall([ |
807df9e6 | 152 | function getVideo (callback) { |
aaf61f38 | 153 | Video.load(videoId, callback) |
807df9e6 C |
154 | }, |
155 | ||
156 | function removeFromDB (video, callback) { | |
aaf61f38 | 157 | video.remove(function (err) { |
807df9e6 | 158 | if (err) return callback(err) |
c173e565 | 159 | |
807df9e6 C |
160 | return callback(null, video) |
161 | }) | |
162 | }, | |
cbe2f7c3 | 163 | |
807df9e6 C |
164 | function sendInformationToFriends (video, callback) { |
165 | const params = { | |
166 | name: video.name, | |
167 | magnetUri: video.magnetUri | |
168 | } | |
169 | ||
170 | friends.removeVideoToFriends(params) | |
171 | ||
172 | return callback(null) | |
173 | } | |
be587647 | 174 | ], function andFinally (err) { |
807df9e6 C |
175 | if (err) { |
176 | logger.error('Errors when removed the video.', { error: err }) | |
177 | return next(err) | |
178 | } | |
179 | ||
180 | return res.type('json').status(204).end() | |
9f10b292 C |
181 | }) |
182 | } | |
8c308c2b | 183 | |
9f10b292 | 184 | function searchVideos (req, res, next) { |
aaf61f38 C |
185 | Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, |
186 | function (err, videosList, videosTotal) { | |
9f10b292 | 187 | if (err) return next(err) |
8c308c2b | 188 | |
aaf61f38 | 189 | res.json(getFormatedVideos(videosList, videosTotal)) |
9f10b292 C |
190 | }) |
191 | } | |
c173e565 | 192 | |
9f10b292 | 193 | // --------------------------------------------------------------------------- |
c173e565 | 194 | |
aaf61f38 | 195 | function getFormatedVideos (videos, videosTotal) { |
bc503c2a | 196 | const formatedVideos = [] |
2df82d42 | 197 | |
aaf61f38 C |
198 | videos.forEach(function (video) { |
199 | formatedVideos.push(video.toFormatedJSON()) | |
2df82d42 C |
200 | }) |
201 | ||
68ce3ae0 | 202 | return { |
aaf61f38 | 203 | total: videosTotal, |
68ce3ae0 C |
204 | data: formatedVideos |
205 | } | |
2df82d42 | 206 | } |