Server: optimization for videoGet and videoRemove

pull/30/head
Chocobozzz 2016-12-30 11:51:08 +01:00
parent 79066fdf33
commit 818f7987eb
2 changed files with 13 additions and 30 deletions

View File

@ -200,7 +200,7 @@ function addVideo (req, res, next) {
} }
function updateVideo (req, res, next) { function updateVideo (req, res, next) {
let videoInstance = res.locals.video const videoInstance = res.locals.video
const videoInfosToUpdate = req.body const videoInfosToUpdate = req.body
waterfall([ waterfall([
@ -275,15 +275,8 @@ function updateVideo (req, res, next) {
} }
function getVideo (req, res, next) { function getVideo (req, res, next) {
db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) { const videoInstance = res.locals.video
if (err) return next(err) res.json(videoInstance.toFormatedJSON())
if (!video) {
return res.type('json').status(204).end()
}
res.json(video.toFormatedJSON())
})
} }
function listVideos (req, res, next) { function listVideos (req, res, next) {
@ -295,20 +288,9 @@ function listVideos (req, res, next) {
} }
function removeVideo (req, res, next) { function removeVideo (req, res, next) {
const videoId = req.params.id const videoInstance = res.locals.video
waterfall([ videoInstance.destroy().asCallback(function (err) {
function loadVideo (callback) {
db.Video.load(videoId, function (err, video) {
return callback(err, video)
})
},
function deleteVideo (video, callback) {
// Informations to other pods will be sent by the afterDestroy video hook
video.destroy().asCallback(callback)
}
], function andFinally (err) {
if (err) { if (err) {
logger.error('Errors when removed the video.', { error: err }) logger.error('Errors when removed the video.', { error: err })
return next(err) return next(err)

View File

@ -71,15 +71,16 @@ function videosRemove (req, res, next) {
logger.debug('Checking videosRemove parameters', { parameters: req.params }) logger.debug('Checking videosRemove parameters', { parameters: req.params })
checkErrors(req, res, function () { checkErrors(req, res, function () {
db.Video.loadAndPopulateAuthor(req.params.id, function (err, video) { checkVideoExists(req.params.id, res, function () {
if (err) { // We need to make additional checks
logger.error('Error in videosRemove request validator.', { error: err })
return res.sendStatus(500) if (res.locals.video.isOwned() === false) {
return res.status(403).send('Cannot remove video of another pod')
} }
if (!video) return res.status(404).send('Video not found') if (res.locals.video.authorId !== res.locals.oauth.token.User.id) {
else if (video.isOwned() === false) return res.status(403).send('Cannot remove video of another pod') return res.status(403).send('Cannot remove video of another user')
else if (video.Author.name !== res.locals.oauth.token.user.username) return res.status(403).send('Cannot remove video of another user') }
next() next()
}) })