diff options
Diffstat (limited to 'server/controllers/api/v1/videos.js')
-rw-r--r-- | server/controllers/api/v1/videos.js | 206 |
1 files changed, 0 insertions, 206 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js deleted file mode 100644 index ee47ce7ac..000000000 --- a/server/controllers/api/v1/videos.js +++ /dev/null | |||
@@ -1,206 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | const mongoose = require('mongoose') | ||
5 | const multer = require('multer') | ||
6 | const waterfall = require('async/waterfall') | ||
7 | |||
8 | const constants = require('../../../initializers/constants') | ||
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 Video = mongoose.model('Video') | ||
24 | |||
25 | // multer configuration | ||
26 | const storage = multer.diskStorage({ | ||
27 | destination: function (req, file, cb) { | ||
28 | cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR) | ||
29 | }, | ||
30 | |||
31 | filename: function (req, file, cb) { | ||
32 | let extension = '' | ||
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' | ||
36 | utils.generateRandomString(16, function (err, randomString) { | ||
37 | const fieldname = err ? undefined : randomString | ||
38 | cb(null, fieldname + '.' + extension) | ||
39 | }) | ||
40 | } | ||
41 | }) | ||
42 | |||
43 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | ||
44 | |||
45 | router.get('/', | ||
46 | validatorsPagination.pagination, | ||
47 | validatorsSort.videosSort, | ||
48 | sort.setVideosSort, | ||
49 | pagination.setPagination, | ||
50 | listVideos | ||
51 | ) | ||
52 | router.post('/', | ||
53 | oAuth.authenticate, | ||
54 | reqFiles, | ||
55 | validatorsVideos.videosAdd, | ||
56 | addVideo | ||
57 | ) | ||
58 | router.get('/:id', | ||
59 | validatorsVideos.videosGet, | ||
60 | getVideo | ||
61 | ) | ||
62 | router.delete('/:id', | ||
63 | oAuth.authenticate, | ||
64 | validatorsVideos.videosRemove, | ||
65 | removeVideo | ||
66 | ) | ||
67 | router.get('/search/:value', | ||
68 | validatorsVideos.videosSearch, | ||
69 | validatorsPagination.pagination, | ||
70 | validatorsSort.videosSort, | ||
71 | sort.setVideosSort, | ||
72 | pagination.setPagination, | ||
73 | search.setVideosSearch, | ||
74 | searchVideos | ||
75 | ) | ||
76 | |||
77 | // --------------------------------------------------------------------------- | ||
78 | |||
79 | module.exports = router | ||
80 | |||
81 | // --------------------------------------------------------------------------- | ||
82 | |||
83 | function addVideo (req, res, next) { | ||
84 | const videoFile = req.files.videofile[0] | ||
85 | const videoInfos = req.body | ||
86 | |||
87 | waterfall([ | ||
88 | |||
89 | function insertIntoDB (callback) { | ||
90 | const videoData = { | ||
91 | name: videoInfos.name, | ||
92 | filename: videoFile.filename, | ||
93 | description: videoInfos.description, | ||
94 | author: res.locals.oauth.token.user.username, | ||
95 | duration: videoFile.duration, | ||
96 | tags: videoInfos.tags | ||
97 | } | ||
98 | |||
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) | ||
103 | }) | ||
104 | }, | ||
105 | |||
106 | function sendToFriends (video, callback) { | ||
107 | video.toRemoteJSON(function (err, remoteVideo) { | ||
108 | if (err) return callback(err) | ||
109 | |||
110 | // Now we'll add the video's meta data to our friends | ||
111 | friends.addVideoToFriends(remoteVideo) | ||
112 | |||
113 | return callback(null) | ||
114 | }) | ||
115 | } | ||
116 | |||
117 | ], function andFinally (err) { | ||
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() | ||
125 | }) | ||
126 | } | ||
127 | |||
128 | function getVideo (req, res, next) { | ||
129 | Video.load(req.params.id, function (err, video) { | ||
130 | if (err) return next(err) | ||
131 | |||
132 | if (!video) { | ||
133 | return res.type('json').status(204).end() | ||
134 | } | ||
135 | |||
136 | res.json(video.toFormatedJSON()) | ||
137 | }) | ||
138 | } | ||
139 | |||
140 | function listVideos (req, res, next) { | ||
141 | Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { | ||
142 | if (err) return next(err) | ||
143 | |||
144 | res.json(getFormatedVideos(videosList, videosTotal)) | ||
145 | }) | ||
146 | } | ||
147 | |||
148 | function removeVideo (req, res, next) { | ||
149 | const videoId = req.params.id | ||
150 | |||
151 | waterfall([ | ||
152 | function getVideo (callback) { | ||
153 | Video.load(videoId, callback) | ||
154 | }, | ||
155 | |||
156 | function removeFromDB (video, callback) { | ||
157 | video.remove(function (err) { | ||
158 | if (err) return callback(err) | ||
159 | |||
160 | return callback(null, video) | ||
161 | }) | ||
162 | }, | ||
163 | |||
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 | } | ||
174 | ], function andFinally (err) { | ||
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() | ||
181 | }) | ||
182 | } | ||
183 | |||
184 | function searchVideos (req, res, next) { | ||
185 | Video.search(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort, | ||
186 | function (err, videosList, videosTotal) { | ||
187 | if (err) return next(err) | ||
188 | |||
189 | res.json(getFormatedVideos(videosList, videosTotal)) | ||
190 | }) | ||
191 | } | ||
192 | |||
193 | // --------------------------------------------------------------------------- | ||
194 | |||
195 | function getFormatedVideos (videos, videosTotal) { | ||
196 | const formatedVideos = [] | ||
197 | |||
198 | videos.forEach(function (video) { | ||
199 | formatedVideos.push(video.toFormatedJSON()) | ||
200 | }) | ||
201 | |||
202 | return { | ||
203 | total: videosTotal, | ||
204 | data: formatedVideos | ||
205 | } | ||
206 | } | ||