aboutsummaryrefslogtreecommitdiffhomepage
path: root/controllers/api/v1/videos.js
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/api/v1/videos.js')
-rw-r--r--controllers/api/v1/videos.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js
new file mode 100644
index 000000000..aa8cb466b
--- /dev/null
+++ b/controllers/api/v1/videos.js
@@ -0,0 +1,88 @@
1;(function () {
2 'use strict'
3
4 var express = require('express')
5 var config = require('config')
6 var crypto = require('crypto')
7 var multer = require('multer')
8 var router = express.Router()
9
10 var middleware = require('../../../middlewares')
11 var miscMiddleware = middleware.misc
12 var reqValidator = middleware.reqValidators.videos
13 var videos = require('../../../models/videos')
14
15 var uploads = config.get('storage.uploads')
16
17 function listVideos (req, res, next) {
18 videos.list(function (err, videos_list) {
19 if (err) return next(err)
20
21 res.json(videos_list)
22 })
23 }
24
25 function searchVideos (req, res, next) {
26 videos.search(req.params.name, function (err, videos_list) {
27 if (err) return next(err)
28
29 res.json(videos_list)
30 })
31 }
32
33 function addVideos (req, res, next) {
34 videos.add({ video: req.files.input_video[0], data: req.body }, function (err) {
35 if (err) return next(err)
36
37 // TODO : include Location of the new video
38 res.sendStatus(201)
39 })
40 }
41
42 function getVideos (req, res, next) {
43 videos.get(req.params.id, function (err, video) {
44 if (err) return next(err)
45
46 if (video === null) {
47 return res.sendStatus(404)
48 }
49
50 res.json(video)
51 })
52 }
53
54 function removeVideo (req, res, next) {
55 videos.remove(req.params.id, function (err) {
56 if (err) return next(err)
57
58 res.sendStatus(204)
59 })
60 }
61
62 // multer configuration
63 var storage = multer.diskStorage({
64 destination: function (req, file, cb) {
65 cb(null, uploads)
66 },
67
68 filename: function (req, file, cb) {
69 var extension = ''
70 if (file.mimetype === 'video/webm') extension = 'webm'
71 else if (file.mimetype === 'video/mp4') extension = 'mp4'
72 else if (file.mimetype === 'video/ogg') extension = 'ogv'
73 crypto.pseudoRandomBytes(16, function (err, raw) {
74 var fieldname = err ? undefined : raw.toString('hex')
75 cb(null, fieldname + '.' + extension)
76 })
77 }
78 })
79 var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
80
81 router.get('/', miscMiddleware.cache(false), listVideos)
82 router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos)
83 router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
84 router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
85 router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
86
87 module.exports = router
88})()