aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/async.ts14
-rw-r--r--server/middlewares/validators/account.ts26
-rw-r--r--server/middlewares/validators/utils.ts16
-rw-r--r--server/middlewares/validators/video-channels.ts41
-rw-r--r--server/middlewares/validators/videos.ts26
5 files changed, 88 insertions, 35 deletions
diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts
index 29ebd169d..9692f9be7 100644
--- a/server/middlewares/async.ts
+++ b/server/middlewares/async.ts
@@ -1,10 +1,18 @@
1import { Request, Response, NextFunction } from 'express' 1import { Request, Response, NextFunction, RequestHandler } from 'express'
2import { eachSeries } from 'async'
2 3
3// Syntactic sugar to avoid try/catch in express controllers 4// Syntactic sugar to avoid try/catch in express controllers
4// Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 5// Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
5function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) { 6function asyncMiddleware (fun: RequestHandler | RequestHandler[]) {
6 return (req: Request, res: Response, next: NextFunction) => { 7 return (req: Request, res: Response, next: NextFunction) => {
7 return Promise.resolve(fn(req, res, next)) 8 if (Array.isArray(fun) === true) {
9 return eachSeries(fun as RequestHandler[], (f, cb) => {
10 Promise.resolve(f(req, res, cb))
11 .catch(next)
12 }, next)
13 }
14
15 return Promise.resolve((fun as RequestHandler)(req, res, next))
8 .catch(next) 16 .catch(next)
9 } 17 }
10} 18}
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts
index 07ae76b63..47ed6a7bb 100644
--- a/server/middlewares/validators/account.ts
+++ b/server/middlewares/validators/account.ts
@@ -1,9 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import { param } from 'express-validator/check' 2import { param } from 'express-validator/check'
3import { logger } from '../../helpers' 3import { logger } from '../../helpers'
4import { isAccountNameValid } from '../../helpers/custom-validators/accounts' 4import { checkLocalAccountNameExists, isAccountNameValid } from '../../helpers/custom-validators/accounts'
5import { database as db } from '../../initializers/database'
6import { AccountInstance } from '../../models'
7import { checkErrors } from './utils' 5import { checkErrors } from './utils'
8 6
9const localAccountValidator = [ 7const localAccountValidator = [
@@ -13,7 +11,7 @@ const localAccountValidator = [
13 logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) 11 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
14 12
15 checkErrors(req, res, () => { 13 checkErrors(req, res, () => {
16 checkLocalAccountExists(req.params.name, res, next) 14 checkLocalAccountNameExists(req.params.name, res, next)
17 }) 15 })
18 } 16 }
19] 17]
@@ -23,23 +21,3 @@ const localAccountValidator = [
23export { 21export {
24 localAccountValidator 22 localAccountValidator
25} 23}
26
27// ---------------------------------------------------------------------------
28
29function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
30 db.Account.loadLocalByName(name)
31 .then(account => {
32 if (!account) {
33 return res.status(404)
34 .send({ error: 'Account not found' })
35 .end()
36 }
37
38 res.locals.account = account
39 return callback(null, account)
40 })
41 .catch(err => {
42 logger.error('Error in account request validator.', err)
43 return res.sendStatus(500)
44 })
45}
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts
index ea107bbe8..77a1a0d4b 100644
--- a/server/middlewares/validators/utils.ts
+++ b/server/middlewares/validators/utils.ts
@@ -14,8 +14,22 @@ function checkErrors (req: express.Request, res: express.Response, next: express
14 return next() 14 return next()
15} 15}
16 16
17function areValidationErrors (req: express.Request, res: express.Response) {
18 const errors = validationResult(req)
19
20 if (!errors.isEmpty()) {
21 logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() })
22 res.status(400).json({ errors: errors.mapped() })
23
24 return true
25 }
26
27 return false
28}
29
17// --------------------------------------------------------------------------- 30// ---------------------------------------------------------------------------
18 31
19export { 32export {
20 checkErrors 33 checkErrors,
34 areValidationErrors
21} 35}
diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts
index c6fd3b59d..f30fbf0dc 100644
--- a/server/middlewares/validators/video-channels.ts
+++ b/server/middlewares/validators/video-channels.ts
@@ -1,13 +1,19 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param } from 'express-validator/check' 2import { body, param } from 'express-validator/check'
3import { UserRight } from '../../../shared' 3import { UserRight } from '../../../shared'
4import { checkVideoAccountExists } from '../../helpers/custom-validators/accounts' 4import { checkAccountIdExists } from '../../helpers/custom-validators/accounts'
5import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' 5import { isIdValid } from '../../helpers/custom-validators/misc'
6import { checkVideoChannelExists, isIdOrUUIDValid } from '../../helpers/index' 6import {
7 checkVideoChannelExists,
8 isVideoChannelDescriptionValid,
9 isVideoChannelExistsPromise,
10 isVideoChannelNameValid
11} from '../../helpers/custom-validators/video-channels'
12import { isIdOrUUIDValid } from '../../helpers/index'
7import { logger } from '../../helpers/logger' 13import { logger } from '../../helpers/logger'
8import { database as db } from '../../initializers' 14import { database as db } from '../../initializers'
9import { UserInstance } from '../../models' 15import { UserInstance } from '../../models'
10import { checkErrors } from './utils' 16import { areValidationErrors, checkErrors } from './utils'
11 17
12const listVideoAccountChannelsValidator = [ 18const listVideoAccountChannelsValidator = [
13 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), 19 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
@@ -16,7 +22,7 @@ const listVideoAccountChannelsValidator = [
16 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) 22 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
17 23
18 checkErrors(req, res, () => { 24 checkErrors(req, res, () => {
19 checkVideoAccountExists(req.params.accountId, res, next) 25 checkAccountIdExists(req.params.accountId, res, next)
20 }) 26 })
21 } 27 }
22] 28]
@@ -90,6 +96,28 @@ const videoChannelsGetValidator = [
90 } 96 }
91] 97]
92 98
99const videoChannelsShareValidator = [
100 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
101 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
102
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking videoChannelShare parameters', { parameters: req.params })
105
106 if (areValidationErrors(req, res)) return
107 if (!await isVideoChannelExistsPromise(req.params.id, res)) return
108
109 const share = await db.VideoChannelShare.load(res.locals.video.id, req.params.accountId)
110 if (!share) {
111 return res.status(404)
112 .end()
113 }
114
115 res.locals.videoChannelShare = share
116
117 return next()
118 }
119]
120
93// --------------------------------------------------------------------------- 121// ---------------------------------------------------------------------------
94 122
95export { 123export {
@@ -97,7 +125,8 @@ export {
97 videoChannelsAddValidator, 125 videoChannelsAddValidator,
98 videoChannelsUpdateValidator, 126 videoChannelsUpdateValidator,
99 videoChannelsRemoveValidator, 127 videoChannelsRemoveValidator,
100 videoChannelsGetValidator 128 videoChannelsGetValidator,
129 videoChannelsShareValidator
101} 130}
102 131
103// --------------------------------------------------------------------------- 132// ---------------------------------------------------------------------------
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index df0eb7b96..5ffc85210 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -24,7 +24,8 @@ import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
24import { database as db } from '../../initializers/database' 24import { database as db } from '../../initializers/database'
25import { UserInstance } from '../../models/account/user-interface' 25import { UserInstance } from '../../models/account/user-interface'
26import { authenticate } from '../oauth' 26import { authenticate } from '../oauth'
27import { checkErrors } from './utils' 27import { areValidationErrors, checkErrors } from './utils'
28import { isVideoExistsPromise } from '../../helpers/index'
28 29
29const videosAddValidator = [ 30const videosAddValidator = [
30 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( 31 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
@@ -230,6 +231,28 @@ const videoRateValidator = [
230 } 231 }
231] 232]
232 233
234const videosShareValidator = [
235 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
236 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
237
238 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
239 logger.debug('Checking videoShare parameters', { parameters: req.params })
240
241 if (areValidationErrors(req, res)) return
242 if (!await isVideoExistsPromise(req.params.id, res)) return
243
244 const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id)
245 if (!share) {
246 return res.status(404)
247 .end()
248 }
249
250 res.locals.videoShare = share
251
252 return next()
253 }
254]
255
233// --------------------------------------------------------------------------- 256// ---------------------------------------------------------------------------
234 257
235export { 258export {
@@ -238,6 +261,7 @@ export {
238 videosGetValidator, 261 videosGetValidator,
239 videosRemoveValidator, 262 videosRemoveValidator,
240 videosSearchValidator, 263 videosSearchValidator,
264 videosShareValidator,
241 265
242 videoAbuseReportValidator, 266 videoAbuseReportValidator,
243 267