aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos/index.js')
-rw-r--r--server/controllers/api/videos/index.js404
1 files changed, 404 insertions, 0 deletions
diff --git a/server/controllers/api/videos/index.js b/server/controllers/api/videos/index.js
new file mode 100644
index 000000000..8de44d5ac
--- /dev/null
+++ b/server/controllers/api/videos/index.js
@@ -0,0 +1,404 @@
1'use strict'
2
3const express = require('express')
4const fs = require('fs')
5const multer = require('multer')
6const path = require('path')
7const waterfall = require('async/waterfall')
8
9const constants = require('../../../initializers/constants')
10const db = require('../../../initializers/database')
11const logger = require('../../../helpers/logger')
12const friends = require('../../../lib/friends')
13const middlewares = require('../../../middlewares')
14const oAuth = middlewares.oauth
15const pagination = middlewares.pagination
16const validators = middlewares.validators
17const validatorsPagination = validators.pagination
18const validatorsSort = validators.sort
19const validatorsVideos = validators.videos
20const search = middlewares.search
21const sort = middlewares.sort
22const databaseUtils = require('../../../helpers/database-utils')
23const utils = require('../../../helpers/utils')
24
25const abuseController = require('./abuse')
26const blacklistController = require('./blacklist')
27const rateController = require('./rate')
28
29const router = express.Router()
30
31// multer configuration
32const storage = multer.diskStorage({
33 destination: function (req, file, cb) {
34 cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR)
35 },
36
37 filename: function (req, file, cb) {
38 let extension = ''
39 if (file.mimetype === 'video/webm') extension = 'webm'
40 else if (file.mimetype === 'video/mp4') extension = 'mp4'
41 else if (file.mimetype === 'video/ogg') extension = 'ogv'
42 utils.generateRandomString(16, function (err, randomString) {
43 const fieldname = err ? undefined : randomString
44 cb(null, fieldname + '.' + extension)
45 })
46 }
47})
48
49const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
50
51router.use('/', abuseController)
52router.use('/', blacklistController)
53router.use('/', rateController)
54
55router.get('/categories', listVideoCategories)
56router.get('/licences', listVideoLicences)
57router.get('/languages', listVideoLanguages)
58
59router.get('/',
60 validatorsPagination.pagination,
61 validatorsSort.videosSort,
62 sort.setVideosSort,
63 pagination.setPagination,
64 listVideos
65)
66router.put('/:id',
67 oAuth.authenticate,
68 reqFiles,
69 validatorsVideos.videosUpdate,
70 updateVideoRetryWrapper
71)
72router.post('/',
73 oAuth.authenticate,
74 reqFiles,
75 validatorsVideos.videosAdd,
76 addVideoRetryWrapper
77)
78router.get('/:id',
79 validatorsVideos.videosGet,
80 getVideo
81)
82
83router.delete('/:id',
84 oAuth.authenticate,
85 validatorsVideos.videosRemove,
86 removeVideo
87)
88
89router.get('/search/:value',
90 validatorsVideos.videosSearch,
91 validatorsPagination.pagination,
92 validatorsSort.videosSort,
93 sort.setVideosSort,
94 pagination.setPagination,
95 search.setVideosSearch,
96 searchVideos
97)
98
99// ---------------------------------------------------------------------------
100
101module.exports = router
102
103// ---------------------------------------------------------------------------
104
105function listVideoCategories (req, res, next) {
106 res.json(constants.VIDEO_CATEGORIES)
107}
108
109function listVideoLicences (req, res, next) {
110 res.json(constants.VIDEO_LICENCES)
111}
112
113function listVideoLanguages (req, res, next) {
114 res.json(constants.VIDEO_LANGUAGES)
115}
116
117// Wrapper to video add that retry the function if there is a database error
118// We need this because we run the transaction in SERIALIZABLE isolation that can fail
119function addVideoRetryWrapper (req, res, next) {
120 const options = {
121 arguments: [ req, res, req.files.videofile[0] ],
122 errorMessage: 'Cannot insert the video with many retries.'
123 }
124
125 databaseUtils.retryTransactionWrapper(addVideo, options, function (err) {
126 if (err) return next(err)
127
128 // TODO : include Location of the new video -> 201
129 return res.type('json').status(204).end()
130 })
131}
132
133function addVideo (req, res, videoFile, finalCallback) {
134 const videoInfos = req.body
135
136 waterfall([
137
138 databaseUtils.startSerializableTransaction,
139
140 function findOrCreateAuthor (t, callback) {
141 const user = res.locals.oauth.token.User
142
143 const name = user.username
144 // null because it is OUR pod
145 const podId = null
146 const userId = user.id
147
148 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
149 return callback(err, t, authorInstance)
150 })
151 },
152
153 function findOrCreateTags (t, author, callback) {
154 const tags = videoInfos.tags
155
156 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
157 return callback(err, t, author, tagInstances)
158 })
159 },
160
161 function createVideoObject (t, author, tagInstances, callback) {
162 const videoData = {
163 name: videoInfos.name,
164 remoteId: null,
165 extname: path.extname(videoFile.filename),
166 category: videoInfos.category,
167 licence: videoInfos.licence,
168 language: videoInfos.language,
169 nsfw: videoInfos.nsfw,
170 description: videoInfos.description,
171 duration: videoFile.duration,
172 authorId: author.id
173 }
174
175 const video = db.Video.build(videoData)
176
177 return callback(null, t, author, tagInstances, video)
178 },
179
180 // Set the videoname the same as the id
181 function renameVideoFile (t, author, tagInstances, video, callback) {
182 const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR
183 const source = path.join(videoDir, videoFile.filename)
184 const destination = path.join(videoDir, video.getVideoFilename())
185
186 fs.rename(source, destination, function (err) {
187 if (err) return callback(err)
188
189 // This is important in case if there is another attempt
190 videoFile.filename = video.getVideoFilename()
191 return callback(null, t, author, tagInstances, video)
192 })
193 },
194
195 function insertVideoIntoDB (t, author, tagInstances, video, callback) {
196 const options = { transaction: t }
197
198 // Add tags association
199 video.save(options).asCallback(function (err, videoCreated) {
200 if (err) return callback(err)
201
202 // Do not forget to add Author informations to the created video
203 videoCreated.Author = author
204
205 return callback(err, t, tagInstances, videoCreated)
206 })
207 },
208
209 function associateTagsToVideo (t, tagInstances, video, callback) {
210 const options = { transaction: t }
211
212 video.setTags(tagInstances, options).asCallback(function (err) {
213 video.Tags = tagInstances
214
215 return callback(err, t, video)
216 })
217 },
218
219 function sendToFriends (t, video, callback) {
220 // Let transcoding job send the video to friends because the videofile extension might change
221 if (constants.CONFIG.TRANSCODING.ENABLED === true) return callback(null, t)
222
223 video.toAddRemoteJSON(function (err, remoteVideo) {
224 if (err) return callback(err)
225
226 // Now we'll add the video's meta data to our friends
227 friends.addVideoToFriends(remoteVideo, t, function (err) {
228 return callback(err, t)
229 })
230 })
231 },
232
233 databaseUtils.commitTransaction
234
235 ], function andFinally (err, t) {
236 if (err) {
237 // This is just a debug because we will retry the insert
238 logger.debug('Cannot insert the video.', { error: err })
239 return databaseUtils.rollbackTransaction(err, t, finalCallback)
240 }
241
242 logger.info('Video with name %s created.', videoInfos.name)
243 return finalCallback(null)
244 })
245}
246
247function updateVideoRetryWrapper (req, res, next) {
248 const options = {
249 arguments: [ req, res ],
250 errorMessage: 'Cannot update the video with many retries.'
251 }
252
253 databaseUtils.retryTransactionWrapper(updateVideo, options, function (err) {
254 if (err) return next(err)
255
256 // TODO : include Location of the new video -> 201
257 return res.type('json').status(204).end()
258 })
259}
260
261function updateVideo (req, res, finalCallback) {
262 const videoInstance = res.locals.video
263 const videoFieldsSave = videoInstance.toJSON()
264 const videoInfosToUpdate = req.body
265
266 waterfall([
267
268 databaseUtils.startSerializableTransaction,
269
270 function findOrCreateTags (t, callback) {
271 if (videoInfosToUpdate.tags) {
272 db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
273 return callback(err, t, tagInstances)
274 })
275 } else {
276 return callback(null, t, null)
277 }
278 },
279
280 function updateVideoIntoDB (t, tagInstances, callback) {
281 const options = {
282 transaction: t
283 }
284
285 if (videoInfosToUpdate.name !== undefined) videoInstance.set('name', videoInfosToUpdate.name)
286 if (videoInfosToUpdate.category !== undefined) videoInstance.set('category', videoInfosToUpdate.category)
287 if (videoInfosToUpdate.licence !== undefined) videoInstance.set('licence', videoInfosToUpdate.licence)
288 if (videoInfosToUpdate.language !== undefined) videoInstance.set('language', videoInfosToUpdate.language)
289 if (videoInfosToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfosToUpdate.nsfw)
290 if (videoInfosToUpdate.description !== undefined) videoInstance.set('description', videoInfosToUpdate.description)
291
292 videoInstance.save(options).asCallback(function (err) {
293 return callback(err, t, tagInstances)
294 })
295 },
296
297 function associateTagsToVideo (t, tagInstances, callback) {
298 if (tagInstances) {
299 const options = { transaction: t }
300
301 videoInstance.setTags(tagInstances, options).asCallback(function (err) {
302 videoInstance.Tags = tagInstances
303
304 return callback(err, t)
305 })
306 } else {
307 return callback(null, t)
308 }
309 },
310
311 function sendToFriends (t, callback) {
312 const json = videoInstance.toUpdateRemoteJSON()
313
314 // Now we'll update the video's meta data to our friends
315 friends.updateVideoToFriends(json, t, function (err) {
316 return callback(err, t)
317 })
318 },
319
320 databaseUtils.commitTransaction
321
322 ], function andFinally (err, t) {
323 if (err) {
324 logger.debug('Cannot update the video.', { error: err })
325
326 // Force fields we want to update
327 // If the transaction is retried, sequelize will think the object has not changed
328 // So it will skip the SQL request, even if the last one was ROLLBACKed!
329 Object.keys(videoFieldsSave).forEach(function (key) {
330 const value = videoFieldsSave[key]
331 videoInstance.set(key, value)
332 })
333
334 return databaseUtils.rollbackTransaction(err, t, finalCallback)
335 }
336
337 logger.info('Video with name %s updated.', videoInfosToUpdate.name)
338 return finalCallback(null)
339 })
340}
341
342function getVideo (req, res, next) {
343 const videoInstance = res.locals.video
344
345 if (videoInstance.isOwned()) {
346 // The increment is done directly in the database, not using the instance value
347 videoInstance.increment('views').asCallback(function (err) {
348 if (err) {
349 logger.error('Cannot add view to video %d.', videoInstance.id)
350 return
351 }
352
353 // FIXME: make a real view system
354 // For example, only add a view when a user watch a video during 30s etc
355 const qaduParams = {
356 videoId: videoInstance.id,
357 type: constants.REQUEST_VIDEO_QADU_TYPES.VIEWS
358 }
359 friends.quickAndDirtyUpdateVideoToFriends(qaduParams)
360 })
361 } else {
362 // Just send the event to our friends
363 const eventParams = {
364 videoId: videoInstance.id,
365 type: constants.REQUEST_VIDEO_EVENT_TYPES.VIEWS
366 }
367 friends.addEventToRemoteVideo(eventParams)
368 }
369
370 // Do not wait the view system
371 res.json(videoInstance.toFormatedJSON())
372}
373
374function listVideos (req, res, next) {
375 db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
376 if (err) return next(err)
377
378 res.json(utils.getFormatedObjects(videosList, videosTotal))
379 })
380}
381
382function removeVideo (req, res, next) {
383 const videoInstance = res.locals.video
384
385 videoInstance.destroy().asCallback(function (err) {
386 if (err) {
387 logger.error('Errors when removed the video.', { error: err })
388 return next(err)
389 }
390
391 return res.type('json').status(204).end()
392 })
393}
394
395function searchVideos (req, res, next) {
396 db.Video.searchAndPopulateAuthorAndPodAndTags(
397 req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort,
398 function (err, videosList, videosTotal) {
399 if (err) return next(err)
400
401 res.json(utils.getFormatedObjects(videosList, videosTotal))
402 }
403 )
404}