diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-01-31 11:23:52 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-01-31 11:23:52 +0100 |
commit | c45f7f84001c2731909db04dd82e1c1f290386eb (patch) | |
tree | b7e57420a1f65dfbbacc1a532bf489c9bea6125d /controllers/api/v1/videos.js | |
parent | cda021079ff455cc0fd0eb95a5395fa808ab63d1 (diff) | |
download | PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.gz PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.tar.zst PeerTube-c45f7f84001c2731909db04dd82e1c1f290386eb.zip |
Infile code reorganization
Diffstat (limited to 'controllers/api/v1/videos.js')
-rw-r--r-- | controllers/api/v1/videos.js | 83 |
1 files changed, 44 insertions, 39 deletions
diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js index aa8cb466b..64b05e32b 100644 --- a/controllers/api/v1/videos.js +++ b/controllers/api/v1/videos.js | |||
@@ -1,34 +1,50 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var express = require('express') | ||
5 | var config = require('config') | 4 | var config = require('config') |
6 | var crypto = require('crypto') | 5 | var crypto = require('crypto') |
6 | var express = require('express') | ||
7 | var multer = require('multer') | 7 | var multer = require('multer') |
8 | var router = express.Router() | ||
9 | 8 | ||
10 | var middleware = require('../../../middlewares') | 9 | var middleware = require('../../../middlewares') |
11 | var miscMiddleware = middleware.misc | 10 | var miscMiddleware = middleware.misc |
12 | var reqValidator = middleware.reqValidators.videos | 11 | var reqValidator = middleware.reqValidators.videos |
13 | var videos = require('../../../models/videos') | 12 | var videos = require('../../../models/videos') |
14 | 13 | ||
14 | var router = express.Router() | ||
15 | var uploads = config.get('storage.uploads') | 15 | var uploads = config.get('storage.uploads') |
16 | 16 | ||
17 | function listVideos (req, res, next) { | 17 | // multer configuration |
18 | videos.list(function (err, videos_list) { | 18 | var storage = multer.diskStorage({ |
19 | if (err) return next(err) | 19 | destination: function (req, file, cb) { |
20 | cb(null, uploads) | ||
21 | }, | ||
20 | 22 | ||
21 | res.json(videos_list) | 23 | filename: function (req, file, cb) { |
22 | }) | 24 | var extension = '' |
23 | } | 25 | if (file.mimetype === 'video/webm') extension = 'webm' |
26 | else if (file.mimetype === 'video/mp4') extension = 'mp4' | ||
27 | else if (file.mimetype === 'video/ogg') extension = 'ogv' | ||
28 | crypto.pseudoRandomBytes(16, function (err, raw) { | ||
29 | var fieldname = err ? undefined : raw.toString('hex') | ||
30 | cb(null, fieldname + '.' + extension) | ||
31 | }) | ||
32 | } | ||
33 | }) | ||
24 | 34 | ||
25 | function searchVideos (req, res, next) { | 35 | var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) |
26 | videos.search(req.params.name, function (err, videos_list) { | ||
27 | if (err) return next(err) | ||
28 | 36 | ||
29 | res.json(videos_list) | 37 | router.get('/', miscMiddleware.cache(false), listVideos) |
30 | }) | 38 | router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos) |
31 | } | 39 | router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos) |
40 | router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo) | ||
41 | router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos) | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | module.exports = router | ||
46 | |||
47 | // --------------------------------------------------------------------------- | ||
32 | 48 | ||
33 | function addVideos (req, res, next) { | 49 | function addVideos (req, res, next) { |
34 | videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { | 50 | videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { |
@@ -51,6 +67,14 @@ | |||
51 | }) | 67 | }) |
52 | } | 68 | } |
53 | 69 | ||
70 | function listVideos (req, res, next) { | ||
71 | videos.list(function (err, videos_list) { | ||
72 | if (err) return next(err) | ||
73 | |||
74 | res.json(videos_list) | ||
75 | }) | ||
76 | } | ||
77 | |||
54 | function removeVideo (req, res, next) { | 78 | function removeVideo (req, res, next) { |
55 | videos.remove(req.params.id, function (err) { | 79 | videos.remove(req.params.id, function (err) { |
56 | if (err) return next(err) | 80 | if (err) return next(err) |
@@ -59,30 +83,11 @@ | |||
59 | }) | 83 | }) |
60 | } | 84 | } |
61 | 85 | ||
62 | // multer configuration | 86 | function searchVideos (req, res, next) { |
63 | var storage = multer.diskStorage({ | 87 | videos.search(req.params.name, function (err, videos_list) { |
64 | destination: function (req, file, cb) { | 88 | if (err) return next(err) |
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 | 89 | ||
87 | module.exports = router | 90 | res.json(videos_list) |
91 | }) | ||
92 | } | ||
88 | })() | 93 | })() |