aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-16 21:37:17 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-16 21:37:17 +0100
commit5101105ef91bfe478f97546b78b321882da2079c (patch)
treea3deec88917fecb8f4299c710bab9ba7679b4803 /server
parent86e054b20fabb5934e09d0bcb096840dd28d69fd (diff)
downloadPeerTube-5101105ef91bfe478f97546b78b321882da2079c.tar.gz
PeerTube-5101105ef91bfe478f97546b78b321882da2079c.tar.zst
PeerTube-5101105ef91bfe478f97546b78b321882da2079c.zip
Video lib/model/reqvalidator refractoring
Diffstat (limited to 'server')
-rw-r--r--server/lib/videos.js11
-rw-r--r--server/middlewares/reqValidators/videos.js19
-rw-r--r--server/models/videos.js34
3 files changed, 23 insertions, 41 deletions
diff --git a/server/lib/videos.js b/server/lib/videos.js
index 3c6ee4a65..a5fe7b0c4 100644
--- a/server/lib/videos.js
+++ b/server/lib/videos.js
@@ -12,10 +12,21 @@ var Videos = require('../models/videos')
12var uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads')) 12var uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
13 13
14var videos = { 14var videos = {
15 getVideoState: getVideoState,
15 seed: seed, 16 seed: seed,
16 seedAllExisting: seedAllExisting 17 seedAllExisting: seedAllExisting
17} 18}
18 19
20function getVideoState (video, callback) {
21 var exist = (video !== null)
22 var owned = false
23 if (exist === true) {
24 owned = (video.namePath !== null)
25 }
26
27 return callback({ exist: exist, owned: owned })
28}
29
19function seed (path, callback) { 30function seed (path, callback) {
20 logger.info('Seeding %s...', path) 31 logger.info('Seeding %s...', path)
21 32
diff --git a/server/middlewares/reqValidators/videos.js b/server/middlewares/reqValidators/videos.js
index 4e5f4391f..b0a6d0360 100644
--- a/server/middlewares/reqValidators/videos.js
+++ b/server/middlewares/reqValidators/videos.js
@@ -2,6 +2,7 @@
2 2
3var checkErrors = require('./utils').checkErrors 3var checkErrors = require('./utils').checkErrors
4var logger = require('../../helpers/logger') 4var logger = require('../../helpers/logger')
5var videos = require('../../lib/videos')
5var Videos = require('../../models/videos') 6var Videos = require('../../models/videos')
6 7
7var reqValidatorsVideos = { 8var reqValidatorsVideos = {
@@ -28,15 +29,17 @@ function videosGet (req, res, next) {
28 logger.debug('Checking videosGet parameters', { parameters: req.params }) 29 logger.debug('Checking videosGet parameters', { parameters: req.params })
29 30
30 checkErrors(req, res, function () { 31 checkErrors(req, res, function () {
31 Videos.getVideoState(req.params.id, function (err, state) { 32 Videos.get(req.params.id, function (err, video) {
32 if (err) { 33 if (err) {
33 logger.error('Error in videosGet request validator.', { error: err }) 34 logger.error('Error in videosGet request validator.', { error: err })
34 res.sendStatus(500) 35 res.sendStatus(500)
35 } 36 }
36 37
37 if (state.exist === false) return res.status(404).send('Video not found') 38 videos.getVideoState(video, function (state) {
39 if (state.exist === false) return res.status(404).send('Video not found')
38 40
39 next() 41 next()
42 })
40 }) 43 })
41 }) 44 })
42} 45}
@@ -47,16 +50,18 @@ function videosRemove (req, res, next) {
47 logger.debug('Checking videosRemove parameters', { parameters: req.params }) 50 logger.debug('Checking videosRemove parameters', { parameters: req.params })
48 51
49 checkErrors(req, res, function () { 52 checkErrors(req, res, function () {
50 Videos.getVideoState(req.params.id, function (err, state) { 53 Videos.get(req.params.id, function (err, video) {
51 if (err) { 54 if (err) {
52 logger.error('Error in videosRemove request validator.', { error: err }) 55 logger.error('Error in videosRemove request validator.', { error: err })
53 res.sendStatus(500) 56 res.sendStatus(500)
54 } 57 }
55 58
56 if (state.exist === false) return res.status(404).send('Video not found') 59 videos.getVideoState(video, function (state) {
57 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') 60 if (state.exist === false) return res.status(404).send('Video not found')
61 else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
58 62
59 next() 63 next()
64 })
60 }) 65 })
61 }) 66 })
62} 67}
diff --git a/server/models/videos.js b/server/models/videos.js
index 436c08bfd..fd02ec9e1 100644
--- a/server/models/videos.js
+++ b/server/models/videos.js
@@ -31,8 +31,6 @@ var Videos = {
31 add: add, 31 add: add,
32 addRemotes: addRemotes, 32 addRemotes: addRemotes,
33 get: get, 33 get: get,
34 getVideoState: getVideoState,
35 isOwned: isOwned,
36 list: list, 34 list: list,
37 listOwned: listOwned, 35 listOwned: listOwned,
38 removeOwned: removeOwned, 36 removeOwned: removeOwned,
@@ -102,38 +100,6 @@ function get (id, callback) {
102 }) 100 })
103} 101}
104 102
105function getVideoState (id, callback) {
106 get(id, function (err, video) {
107 if (err) return callback(err)
108
109 var exist = (video !== null)
110 var owned = false
111 if (exist === true) {
112 owned = (video.namePath !== null)
113 }
114
115 return callback(null, { exist: exist, owned: owned })
116 })
117}
118
119function isOwned (id, callback) {
120 VideosDB.findById(id, function (err, video) {
121 if (err || !video) {
122 if (!err) err = new Error('Cannot find this video.')
123 logger.error('Cannot find this video.')
124 return callback(err)
125 }
126
127 if (video.namePath === null) {
128 var error_string = 'Cannot remove the video of another pod.'
129 logger.error(error_string)
130 return callback(new Error(error_string), false, video)
131 }
132
133 callback(null, true, video)
134 })
135}
136
137function list (callback) { 103function list (callback) {
138 VideosDB.find(function (err, videos_list) { 104 VideosDB.find(function (err, videos_list) {
139 if (err) { 105 if (err) {