From 6fcd19ba737f1f5614a56c6925adb882dea43b8d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Jul 2017 13:26:25 +0200 Subject: Move to promises Closes https://github.com/Chocobozzz/PeerTube/issues/74 --- server/middlewares/validators/pods.ts | 44 +++++++++---------- server/middlewares/validators/users.ts | 52 +++++++++++------------ server/middlewares/validators/videos.ts | 75 ++++++++++++++++----------------- 3 files changed, 85 insertions(+), 86 deletions(-) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts index d8eb90168..da7fc2bd6 100644 --- a/server/middlewares/validators/pods.ts +++ b/server/middlewares/validators/pods.ts @@ -19,19 +19,19 @@ function makeFriendsValidator (req: express.Request, res: express.Response, next logger.debug('Checking makeFriends parameters', { parameters: req.body }) checkErrors(req, res, function () { - hasFriends(function (err, heHasFriends) { - if (err) { + hasFriends() + .then(heHasFriends => { + if (heHasFriends === true) { + // We need to quit our friends before make new ones + return res.sendStatus(409) + } + + return next() + }) + .catch(err => { logger.error('Cannot know if we have friends.', { error: err }) res.sendStatus(500) - } - - if (heHasFriends === true) { - // We need to quit our friends before make new ones - return res.sendStatus(409) - } - - return next() - }) + }) }) } @@ -42,19 +42,19 @@ function podsAddValidator (req: express.Request, res: express.Response, next: ex logger.debug('Checking podsAdd parameters', { parameters: req.body }) checkErrors(req, res, function () { - db.Pod.loadByHost(req.body.host, function (err, pod) { - if (err) { + db.Pod.loadByHost(req.body.host) + .then(pod => { + // Pod with this host already exists + if (pod) { + return res.sendStatus(409) + } + + return next() + }) + .catch(err => { logger.error('Cannot load pod by host.', { error: err }) res.sendStatus(500) - } - - // Pod with this host already exists - if (pod) { - return res.sendStatus(409) - } - - return next() - }) + }) }) } diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index b7b9ef370..c06735047 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -13,16 +13,16 @@ function usersAddValidator (req: express.Request, res: express.Response, next: e logger.debug('Checking usersAdd parameters', { parameters: req.body }) checkErrors(req, res, function () { - db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) { - if (err) { + db.User.loadByUsernameOrEmail(req.body.username, req.body.email) + .then(user => { + if (user) return res.status(409).send('User already exists.') + + next() + }) + .catch(err => { logger.error('Error in usersAdd request validator.', { error: err }) return res.sendStatus(500) - } - - if (user) return res.status(409).send('User already exists.') - - next() - }) + }) }) } @@ -32,18 +32,18 @@ function usersRemoveValidator (req: express.Request, res: express.Response, next logger.debug('Checking usersRemove parameters', { parameters: req.params }) checkErrors(req, res, function () { - db.User.loadById(req.params.id, function (err, user) { - if (err) { - logger.error('Error in usersRemove request validator.', { error: err }) - return res.sendStatus(500) - } - - if (!user) return res.status(404).send('User not found') + db.User.loadById(req.params.id) + .then(user => { + if (!user) return res.status(404).send('User not found') - if (user.username === 'root') return res.status(400).send('Cannot remove the root user') + if (user.username === 'root') return res.status(400).send('Cannot remove the root user') - next() - }) + next() + }) + .catch(err => { + logger.error('Error in usersRemove request validator.', { error: err }) + return res.sendStatus(500) + }) }) } @@ -64,16 +64,16 @@ function usersVideoRatingValidator (req: express.Request, res: express.Response, logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) checkErrors(req, res, function () { - db.Video.load(req.params.videoId, function (err, video) { - if (err) { + db.Video.load(req.params.videoId) + .then(video => { + if (!video) return res.status(404).send('Video not found') + + next() + }) + .catch(err => { logger.error('Error in user request validator.', { error: err }) return res.sendStatus(500) - } - - if (!video) return res.status(404).send('Video not found') - - next() - }) + }) }) } diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 03742a522..ec452cade 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -1,5 +1,4 @@ import 'express-validator' -import * as multer from 'multer' import * as express from 'express' import { database as db } from '../../initializers/database' @@ -24,18 +23,19 @@ function videosAddValidator (req: express.Request, res: express.Response, next: checkErrors(req, res, function () { const videoFile = req.files.videofile[0] - db.Video.getDurationFromFile(videoFile.path, function (err, duration) { - if (err) { - return res.status(400).send('Cannot retrieve metadata of the file.') - } - - if (!isVideoDurationValid(duration)) { - return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') - } + db.Video.getDurationFromFile(videoFile.path) + .then(duration => { + if (!isVideoDurationValid('' + duration)) { + return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') + } - videoFile['duration'] = duration - next() - }) + videoFile['duration'] = duration + next() + }) + .catch(err => { + logger.error('Error in getting duration from file.', { error: err }) + res.status(400).send('Cannot retrieve metadata of the file.') + }) }) } @@ -157,43 +157,42 @@ export { // --------------------------------------------------------------------------- function checkVideoExists (id: string, res: express.Response, callback: () => void) { - db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { - if (err) { - logger.error('Error in video request validator.', { error: err }) - return res.sendStatus(500) - } - + db.Video.loadAndPopulateAuthorAndPodAndTags(id).then(video => { if (!video) return res.status(404).send('Video not found') res.locals.video = video callback() }) + .catch(err => { + logger.error('Error in video request validator.', { error: err }) + return res.sendStatus(500) + }) } function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { // Retrieve the user who did the request - db.User.loadById(userId, function (err, user) { - if (err) { - logger.error('Error in video request validator.', { error: err }) - return res.sendStatus(500) - } - - // Check if the user can delete the video - // The user can delete it if s/he is an admin - // Or if s/he is the video's author - if (user.isAdmin() === false) { - if (res.locals.video.isOwned() === false) { - return res.status(403).send('Cannot remove video of another pod') - } - - if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) { - return res.status(403).send('Cannot remove video of another user') + db.User.loadById(userId) + .then(user => { + // Check if the user can delete the video + // The user can delete it if s/he is an admin + // Or if s/he is the video's author + if (user.isAdmin() === false) { + if (res.locals.video.isOwned() === false) { + return res.status(403).send('Cannot remove video of another pod') + } + + if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) { + return res.status(403).send('Cannot remove video of another user') + } } - } - // If we reach this comment, we can delete the video - callback() - }) + // If we reach this comment, we can delete the video + callback() + }) + .catch(err => { + logger.error('Error in video request validator.', { error: err }) + return res.sendStatus(500) + }) } function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { -- cgit v1.2.3