Video lib/model/reqvalidator refractoring
authorChocobozzz <florian.bigard@gmail.com>
Wed, 16 Mar 2016 20:37:17 +0000 (21:37 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Wed, 16 Mar 2016 20:37:17 +0000 (21:37 +0100)
server/lib/videos.js
server/middlewares/reqValidators/videos.js
server/models/videos.js

index 3c6ee4a650605a1b4ccad24bea60a191300fbb58..a5fe7b0c47fe7519e649ab7cd23491a8754b03a3 100644 (file)
@@ -12,10 +12,21 @@ var Videos = require('../models/videos')
 var uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
 
 var videos = {
+  getVideoState: getVideoState,
   seed: seed,
   seedAllExisting: seedAllExisting
 }
 
+function getVideoState (video, callback) {
+  var exist = (video !== null)
+  var owned = false
+  if (exist === true) {
+    owned = (video.namePath !== null)
+  }
+
+  return callback({ exist: exist, owned: owned })
+}
+
 function seed (path, callback) {
   logger.info('Seeding %s...', path)
 
index 4e5f4391fe44e645d69bdf2f7ea9766a33fac9d6..b0a6d0360e546a0e86170bb39dedba1ec8484414 100644 (file)
@@ -2,6 +2,7 @@
 
 var checkErrors = require('./utils').checkErrors
 var logger = require('../../helpers/logger')
+var videos = require('../../lib/videos')
 var Videos = require('../../models/videos')
 
 var reqValidatorsVideos = {
@@ -28,15 +29,17 @@ function videosGet (req, res, next) {
   logger.debug('Checking videosGet parameters', { parameters: req.params })
 
   checkErrors(req, res, function () {
-    Videos.getVideoState(req.params.id, function (err, state) {
+    Videos.get(req.params.id, function (err, video) {
       if (err) {
         logger.error('Error in videosGet request validator.', { error: err })
         res.sendStatus(500)
       }
 
-      if (state.exist === false) return res.status(404).send('Video not found')
+      videos.getVideoState(video, function (state) {
+        if (state.exist === false) return res.status(404).send('Video not found')
 
-      next()
+        next()
+      })
     })
   })
 }
@@ -47,16 +50,18 @@ function videosRemove (req, res, next) {
   logger.debug('Checking videosRemove parameters', { parameters: req.params })
 
   checkErrors(req, res, function () {
-    Videos.getVideoState(req.params.id, function (err, state) {
+    Videos.get(req.params.id, function (err, video) {
       if (err) {
         logger.error('Error in videosRemove request validator.', { error: err })
         res.sendStatus(500)
       }
 
-      if (state.exist === false) return res.status(404).send('Video not found')
-      else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
+      videos.getVideoState(video, function (state) {
+        if (state.exist === false) return res.status(404).send('Video not found')
+        else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod')
 
-      next()
+        next()
+      })
     })
   })
 }
index 436c08bfd473d62474c8fb2375cafd12980f11b4..fd02ec9e1c7edb6f5dbc152181cf45118377a7c4 100644 (file)
@@ -31,8 +31,6 @@ var Videos = {
   add: add,
   addRemotes: addRemotes,
   get: get,
-  getVideoState: getVideoState,
-  isOwned: isOwned,
   list: list,
   listOwned: listOwned,
   removeOwned: removeOwned,
@@ -102,38 +100,6 @@ function get (id, callback) {
   })
 }
 
-function getVideoState (id, callback) {
-  get(id, function (err, video) {
-    if (err) return callback(err)
-
-    var exist = (video !== null)
-    var owned = false
-    if (exist === true) {
-      owned = (video.namePath !== null)
-    }
-
-    return callback(null, { exist: exist, owned: owned })
-  })
-}
-
-function isOwned (id, callback) {
-  VideosDB.findById(id, function (err, video) {
-    if (err || !video) {
-      if (!err) err = new Error('Cannot find this video.')
-      logger.error('Cannot find this video.')
-      return callback(err)
-    }
-
-    if (video.namePath === null) {
-      var error_string = 'Cannot remove the video of another pod.'
-      logger.error(error_string)
-      return callback(new Error(error_string), false, video)
-    }
-
-    callback(null, true, video)
-  })
-}
-
 function list (callback) {
   VideosDB.find(function (err, videos_list) {
     if (err) {