2015-06-09 10:41:40 -05:00
|
|
|
;(function () {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var express = require('express')
|
2015-12-04 14:43:26 -06:00
|
|
|
var config = require('config')
|
|
|
|
var multer = require('multer')
|
2015-06-09 10:41:40 -05:00
|
|
|
var router = express.Router()
|
2015-12-04 14:43:26 -06:00
|
|
|
|
2015-11-06 10:34:15 -06:00
|
|
|
var middleware = require('../../../middlewares')
|
2015-11-07 11:40:13 -06:00
|
|
|
var miscMiddleware = middleware.misc
|
|
|
|
var reqValidator = middleware.reqValidators.videos
|
2015-11-06 10:34:15 -06:00
|
|
|
var videos = require('../../../src/videos')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2015-12-04 14:43:26 -06:00
|
|
|
var uploads = config.get('storage.uploads')
|
|
|
|
|
2015-06-09 10:41:40 -05:00
|
|
|
function listVideos (req, res, next) {
|
|
|
|
videos.list(function (err, videos_list) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.json(videos_list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchVideos (req, res, next) {
|
|
|
|
videos.search(req.params.name, function (err, videos_list) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.json(videos_list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVideos (req, res, next) {
|
2015-12-04 14:43:26 -06:00
|
|
|
videos.add({ video: req.files.input_video[0], data: req.body }, function (err) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// TODO : include Location of the new video
|
|
|
|
res.sendStatus(201)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideos (req, res, next) {
|
|
|
|
videos.get(req.params.id, function (err, video) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
if (video === null) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(video)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeVideo (req, res, next) {
|
|
|
|
videos.remove(req.params.id, function (err) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-04 14:43:26 -06:00
|
|
|
var reqFiles = multer({ dest: uploads }).fields([{ name: 'input_video', maxCount: 1 }])
|
|
|
|
|
2015-11-07 11:40:13 -06:00
|
|
|
router.get('/', miscMiddleware.cache(false), listVideos)
|
2015-12-04 14:43:26 -06:00
|
|
|
router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos)
|
2015-11-07 11:40:13 -06:00
|
|
|
router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
|
|
|
|
router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
|
|
|
|
router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
module.exports = router
|
|
|
|
})()
|