diff options
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/admin.ts | 5 | ||||
-rw-r--r-- | server/middlewares/index.ts | 16 | ||||
-rw-r--r-- | server/middlewares/oauth.ts | 6 | ||||
-rw-r--r-- | server/middlewares/pagination.ts | 5 | ||||
-rw-r--r-- | server/middlewares/pods.ts | 9 | ||||
-rw-r--r-- | server/middlewares/search.ts | 5 | ||||
-rw-r--r-- | server/middlewares/secure.ts | 5 | ||||
-rw-r--r-- | server/middlewares/sort.ts | 9 | ||||
-rw-r--r-- | server/middlewares/validators/pagination.ts | 5 | ||||
-rw-r--r-- | server/middlewares/validators/pods.ts | 7 | ||||
-rw-r--r-- | server/middlewares/validators/remote/signature.ts | 5 | ||||
-rw-r--r-- | server/middlewares/validators/remote/videos.ts | 9 | ||||
-rw-r--r-- | server/middlewares/validators/sort.ts | 13 | ||||
-rw-r--r-- | server/middlewares/validators/users.ts | 11 | ||||
-rw-r--r-- | server/middlewares/validators/utils.ts | 5 | ||||
-rw-r--r-- | server/middlewares/validators/videos.ts | 28 |
16 files changed, 93 insertions, 50 deletions
diff --git a/server/middlewares/admin.ts b/server/middlewares/admin.ts index 28b6a9a12..812397352 100644 --- a/server/middlewares/admin.ts +++ b/server/middlewares/admin.ts | |||
@@ -1,6 +1,9 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { logger } from '../helpers' | 4 | import { logger } from '../helpers' |
2 | 5 | ||
3 | function ensureIsAdmin (req, res, next) { | 6 | function ensureIsAdmin (req: express.Request, res: express.Response, next: express.NextFunction) { |
4 | const user = res.locals.oauth.token.user | 7 | const user = res.locals.oauth.token.user |
5 | if (user.isAdmin() === false) { | 8 | if (user.isAdmin() === false) { |
6 | logger.info('A non admin user is trying to access to an admin content.') | 9 | logger.info('A non admin user is trying to access to an admin content.') |
diff --git a/server/middlewares/index.ts b/server/middlewares/index.ts index 2c1c5fa53..d71dd2452 100644 --- a/server/middlewares/index.ts +++ b/server/middlewares/index.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | export * from './validators'; | 1 | export * from './validators' |
2 | export * from './admin'; | 2 | export * from './admin' |
3 | export * from './oauth'; | 3 | export * from './oauth' |
4 | export * from './pagination'; | 4 | export * from './pagination' |
5 | export * from './pods'; | 5 | export * from './pods' |
6 | export * from './search'; | 6 | export * from './search' |
7 | export * from './secure'; | 7 | export * from './secure' |
8 | export * from './sort'; | 8 | export * from './sort' |
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 468e41810..d545b3e58 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts | |||
@@ -1,3 +1,5 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
1 | import * as OAuthServer from 'express-oauth-server' | 3 | import * as OAuthServer from 'express-oauth-server' |
2 | 4 | ||
3 | import { OAUTH_LIFETIME } from '../initializers' | 5 | import { OAUTH_LIFETIME } from '../initializers' |
@@ -9,7 +11,7 @@ const oAuthServer = new OAuthServer({ | |||
9 | model: require('../lib/oauth-model') | 11 | model: require('../lib/oauth-model') |
10 | }) | 12 | }) |
11 | 13 | ||
12 | function authenticate (req, res, next) { | 14 | function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) { |
13 | oAuthServer.authenticate()(req, res, function (err) { | 15 | oAuthServer.authenticate()(req, res, function (err) { |
14 | if (err) { | 16 | if (err) { |
15 | logger.error('Cannot authenticate.', { error: err }) | 17 | logger.error('Cannot authenticate.', { error: err }) |
@@ -22,7 +24,7 @@ function authenticate (req, res, next) { | |||
22 | }) | 24 | }) |
23 | } | 25 | } |
24 | 26 | ||
25 | function token (req, res, next) { | 27 | function token (req: express.Request, res: express.Response, next: express.NextFunction) { |
26 | return oAuthServer.token()(req, res, next) | 28 | return oAuthServer.token()(req, res, next) |
27 | } | 29 | } |
28 | 30 | ||
diff --git a/server/middlewares/pagination.ts b/server/middlewares/pagination.ts index cadd76980..26a8cacf0 100644 --- a/server/middlewares/pagination.ts +++ b/server/middlewares/pagination.ts | |||
@@ -1,6 +1,9 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { PAGINATION_COUNT_DEFAULT } from '../initializers' | 4 | import { PAGINATION_COUNT_DEFAULT } from '../initializers' |
2 | 5 | ||
3 | function setPagination (req, res, next) { | 6 | function setPagination (req: express.Request, res: express.Response, next: express.NextFunction) { |
4 | if (!req.query.start) req.query.start = 0 | 7 | if (!req.query.start) req.query.start = 0 |
5 | else req.query.start = parseInt(req.query.start, 10) | 8 | else req.query.start = parseInt(req.query.start, 10) |
6 | 9 | ||
diff --git a/server/middlewares/pods.ts b/server/middlewares/pods.ts index c255be899..eaf9aa144 100644 --- a/server/middlewares/pods.ts +++ b/server/middlewares/pods.ts | |||
@@ -1,6 +1,9 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { REMOTE_SCHEME } from '../initializers' | 4 | import { REMOTE_SCHEME } from '../initializers' |
2 | 5 | ||
3 | function setBodyHostsPort (req, res, next) { | 6 | function setBodyHostsPort (req: express.Request, res: express.Response, next: express.NextFunction) { |
4 | if (!req.body.hosts) return next() | 7 | if (!req.body.hosts) return next() |
5 | 8 | ||
6 | for (let i = 0; i < req.body.hosts.length; i++) { | 9 | for (let i = 0; i < req.body.hosts.length; i++) { |
@@ -17,7 +20,7 @@ function setBodyHostsPort (req, res, next) { | |||
17 | return next() | 20 | return next() |
18 | } | 21 | } |
19 | 22 | ||
20 | function setBodyHostPort (req, res, next) { | 23 | function setBodyHostPort (req: express.Request, res: express.Response, next: express.NextFunction) { |
21 | if (!req.body.host) return next() | 24 | if (!req.body.host) return next() |
22 | 25 | ||
23 | const hostWithPort = getHostWithPort(req.body.host) | 26 | const hostWithPort = getHostWithPort(req.body.host) |
@@ -41,7 +44,7 @@ export { | |||
41 | 44 | ||
42 | // --------------------------------------------------------------------------- | 45 | // --------------------------------------------------------------------------- |
43 | 46 | ||
44 | function getHostWithPort (host) { | 47 | function getHostWithPort (host: string) { |
45 | const splitted = host.split(':') | 48 | const splitted = host.split(':') |
46 | 49 | ||
47 | // The port was not specified | 50 | // The port was not specified |
diff --git a/server/middlewares/search.ts b/server/middlewares/search.ts index 05a2e7442..6fe83d25b 100644 --- a/server/middlewares/search.ts +++ b/server/middlewares/search.ts | |||
@@ -1,4 +1,7 @@ | |||
1 | function setVideosSearch (req, res, next) { | 1 | import 'express-validator' |
2 | import * as express from 'express' | ||
3 | |||
4 | function setVideosSearch (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
2 | if (!req.query.field) req.query.field = 'name' | 5 | if (!req.query.field) req.query.field = 'name' |
3 | 6 | ||
4 | return next() | 7 | return next() |
diff --git a/server/middlewares/secure.ts b/server/middlewares/secure.ts index bd7cfa918..fbfd08c7b 100644 --- a/server/middlewares/secure.ts +++ b/server/middlewares/secure.ts | |||
@@ -1,10 +1,13 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { database as db } from '../initializers' | 4 | import { database as db } from '../initializers' |
2 | import { | 5 | import { |
3 | logger, | 6 | logger, |
4 | checkSignature as peertubeCryptoCheckSignature | 7 | checkSignature as peertubeCryptoCheckSignature |
5 | } from '../helpers' | 8 | } from '../helpers' |
6 | 9 | ||
7 | function checkSignature (req, res, next) { | 10 | function checkSignature (req: express.Request, res: express.Response, next: express.NextFunction) { |
8 | const host = req.body.signature.host | 11 | const host = req.body.signature.host |
9 | db.Pod.loadByHost(host, function (err, pod) { | 12 | db.Pod.loadByHost(host, function (err, pod) { |
10 | if (err) { | 13 | if (err) { |
diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index ab9ccf524..632b2fae4 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts | |||
@@ -1,16 +1,19 @@ | |||
1 | function setUsersSort (req, res, next) { | 1 | import 'express-validator' |
2 | import * as express from 'express' | ||
3 | |||
4 | function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
2 | if (!req.query.sort) req.query.sort = '-createdAt' | 5 | if (!req.query.sort) req.query.sort = '-createdAt' |
3 | 6 | ||
4 | return next() | 7 | return next() |
5 | } | 8 | } |
6 | 9 | ||
7 | function setVideoAbusesSort (req, res, next) { | 10 | function setVideoAbusesSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
8 | if (!req.query.sort) req.query.sort = '-createdAt' | 11 | if (!req.query.sort) req.query.sort = '-createdAt' |
9 | 12 | ||
10 | return next() | 13 | return next() |
11 | } | 14 | } |
12 | 15 | ||
13 | function setVideosSort (req, res, next) { | 16 | function setVideosSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
14 | if (!req.query.sort) req.query.sort = '-createdAt' | 17 | if (!req.query.sort) req.query.sort = '-createdAt' |
15 | 18 | ||
16 | return next() | 19 | return next() |
diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index de719c05b..cca8295ff 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts | |||
@@ -1,7 +1,10 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { checkErrors } from './utils' | 4 | import { checkErrors } from './utils' |
2 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
3 | 6 | ||
4 | function paginationValidator (req, res, next) { | 7 | function paginationValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
5 | req.checkQuery('start', 'Should have a number start').optional().isInt() | 8 | req.checkQuery('start', 'Should have a number start').optional().isInt() |
6 | req.checkQuery('count', 'Should have a number count').optional().isInt() | 9 | req.checkQuery('count', 'Should have a number count').optional().isInt() |
7 | 10 | ||
diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts index c55a88b85..d8eb90168 100644 --- a/server/middlewares/validators/pods.ts +++ b/server/middlewares/validators/pods.ts | |||
@@ -1,3 +1,6 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { database as db } from '../../initializers/database' | 4 | import { database as db } from '../../initializers/database' |
2 | import { checkErrors } from './utils' | 5 | import { checkErrors } from './utils' |
3 | import { logger } from '../../helpers' | 6 | import { logger } from '../../helpers' |
@@ -5,7 +8,7 @@ import { CONFIG } from '../../initializers' | |||
5 | import { hasFriends } from '../../lib' | 8 | import { hasFriends } from '../../lib' |
6 | import { isTestInstance } from '../../helpers' | 9 | import { isTestInstance } from '../../helpers' |
7 | 10 | ||
8 | function makeFriendsValidator (req, res, next) { | 11 | function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
9 | // Force https if the administrator wants to make friends | 12 | // Force https if the administrator wants to make friends |
10 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { | 13 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { |
11 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') | 14 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') |
@@ -32,7 +35,7 @@ function makeFriendsValidator (req, res, next) { | |||
32 | }) | 35 | }) |
33 | } | 36 | } |
34 | 37 | ||
35 | function podsAddValidator (req, res, next) { | 38 | function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
36 | req.checkBody('host', 'Should have a host').isHostValid() | 39 | req.checkBody('host', 'Should have a host').isHostValid() |
37 | req.checkBody('email', 'Should have an email').isEmail() | 40 | req.checkBody('email', 'Should have an email').isEmail() |
38 | req.checkBody('publicKey', 'Should have a public key').notEmpty() | 41 | req.checkBody('publicKey', 'Should have a public key').notEmpty() |
diff --git a/server/middlewares/validators/remote/signature.ts b/server/middlewares/validators/remote/signature.ts index 6e3ebe7db..eb5c196eb 100644 --- a/server/middlewares/validators/remote/signature.ts +++ b/server/middlewares/validators/remote/signature.ts | |||
@@ -1,7 +1,10 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { logger } from '../../../helpers' | 4 | import { logger } from '../../../helpers' |
2 | import { checkErrors } from '../utils' | 5 | import { checkErrors } from '../utils' |
3 | 6 | ||
4 | function signatureValidator (req, res, next) { | 7 | function signatureValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
5 | req.checkBody('signature.host', 'Should have a signature host').isURL() | 8 | req.checkBody('signature.host', 'Should have a signature host').isURL() |
6 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() | 9 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() |
7 | 10 | ||
diff --git a/server/middlewares/validators/remote/videos.ts b/server/middlewares/validators/remote/videos.ts index 3380c29e2..2037c0085 100644 --- a/server/middlewares/validators/remote/videos.ts +++ b/server/middlewares/validators/remote/videos.ts | |||
@@ -1,7 +1,10 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { logger } from '../../../helpers' | 4 | import { logger } from '../../../helpers' |
2 | import { checkErrors } from '../utils' | 5 | import { checkErrors } from '../utils' |
3 | 6 | ||
4 | function remoteVideosValidator (req, res, next) { | 7 | function remoteVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
5 | req.checkBody('data').isEachRemoteRequestVideosValid() | 8 | req.checkBody('data').isEachRemoteRequestVideosValid() |
6 | 9 | ||
7 | logger.debug('Checking remoteVideos parameters', { parameters: req.body }) | 10 | logger.debug('Checking remoteVideos parameters', { parameters: req.body }) |
@@ -9,7 +12,7 @@ function remoteVideosValidator (req, res, next) { | |||
9 | checkErrors(req, res, next) | 12 | checkErrors(req, res, next) |
10 | } | 13 | } |
11 | 14 | ||
12 | function remoteQaduVideosValidator (req, res, next) { | 15 | function remoteQaduVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
13 | req.checkBody('data').isEachRemoteRequestVideosQaduValid() | 16 | req.checkBody('data').isEachRemoteRequestVideosQaduValid() |
14 | 17 | ||
15 | logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) | 18 | logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) |
@@ -17,7 +20,7 @@ function remoteQaduVideosValidator (req, res, next) { | |||
17 | checkErrors(req, res, next) | 20 | checkErrors(req, res, next) |
18 | } | 21 | } |
19 | 22 | ||
20 | function remoteEventsVideosValidator (req, res, next) { | 23 | function remoteEventsVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
21 | req.checkBody('data').isEachRemoteRequestVideosEventsValid() | 24 | req.checkBody('data').isEachRemoteRequestVideosEventsValid() |
22 | 25 | ||
23 | logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) | 26 | logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) |
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index ebc7333c7..3baee9fb3 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts | |||
@@ -1,3 +1,6 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { checkErrors } from './utils' | 4 | import { checkErrors } from './utils' |
2 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
3 | import { SORTABLE_COLUMNS } from '../../initializers' | 6 | import { SORTABLE_COLUMNS } from '../../initializers' |
@@ -7,15 +10,15 @@ const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | |||
7 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) | 10 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) |
8 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | 11 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) |
9 | 12 | ||
10 | function usersSortValidator (req, res, next) { | 13 | function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
11 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) | 14 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) |
12 | } | 15 | } |
13 | 16 | ||
14 | function videoAbusesSortValidator (req, res, next) { | 17 | function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
15 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) | 18 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) |
16 | } | 19 | } |
17 | 20 | ||
18 | function videosSortValidator (req, res, next) { | 21 | function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
19 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) | 22 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) |
20 | } | 23 | } |
21 | 24 | ||
@@ -29,7 +32,7 @@ export { | |||
29 | 32 | ||
30 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
31 | 34 | ||
32 | function checkSort (req, res, next, sortableColumns) { | 35 | function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) { |
33 | req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) | 36 | req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) |
34 | 37 | ||
35 | logger.debug('Checking sort parameters', { parameters: req.query }) | 38 | logger.debug('Checking sort parameters', { parameters: req.query }) |
@@ -37,7 +40,7 @@ function checkSort (req, res, next, sortableColumns) { | |||
37 | checkErrors(req, res, next) | 40 | checkErrors(req, res, next) |
38 | } | 41 | } |
39 | 42 | ||
40 | function createSortableColumns (sortableColumns) { | 43 | function createSortableColumns (sortableColumns: string[]) { |
41 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) | 44 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) |
42 | 45 | ||
43 | return sortableColumns.concat(sortableColumnDesc) | 46 | return sortableColumns.concat(sortableColumnDesc) |
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index e0d1d917a..b7b9ef370 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts | |||
@@ -1,8 +1,11 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
3 | |||
1 | import { database as db } from '../../initializers/database' | 4 | import { database as db } from '../../initializers/database' |
2 | import { checkErrors } from './utils' | 5 | import { checkErrors } from './utils' |
3 | import { logger } from '../../helpers' | 6 | import { logger } from '../../helpers' |
4 | 7 | ||
5 | function usersAddValidator (req, res, next) { | 8 | function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
6 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() | 9 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() |
7 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() | 10 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() |
8 | req.checkBody('email', 'Should have a valid email').isEmail() | 11 | req.checkBody('email', 'Should have a valid email').isEmail() |
@@ -23,7 +26,7 @@ function usersAddValidator (req, res, next) { | |||
23 | }) | 26 | }) |
24 | } | 27 | } |
25 | 28 | ||
26 | function usersRemoveValidator (req, res, next) { | 29 | function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
27 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | 30 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
28 | 31 | ||
29 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | 32 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) |
@@ -44,7 +47,7 @@ function usersRemoveValidator (req, res, next) { | |||
44 | }) | 47 | }) |
45 | } | 48 | } |
46 | 49 | ||
47 | function usersUpdateValidator (req, res, next) { | 50 | function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
48 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | 51 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
49 | // Add old password verification | 52 | // Add old password verification |
50 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() | 53 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() |
@@ -55,7 +58,7 @@ function usersUpdateValidator (req, res, next) { | |||
55 | checkErrors(req, res, next) | 58 | checkErrors(req, res, next) |
56 | } | 59 | } |
57 | 60 | ||
58 | function usersVideoRatingValidator (req, res, next) { | 61 | function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
59 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) | 62 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) |
60 | 63 | ||
61 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) | 64 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) |
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index 710e65529..0424d5942 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts | |||
@@ -1,9 +1,10 @@ | |||
1 | import 'express-validator' | ||
2 | import * as express from 'express' | ||
1 | import { inspect } from 'util' | 3 | import { inspect } from 'util' |
2 | 4 | ||
3 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
4 | 6 | ||
5 | function checkErrors (req, res, next, statusCode?) { | 7 | function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) { |
6 | if (statusCode === undefined) statusCode = 400 | ||
7 | const errors = req.validationErrors() | 8 | const errors = req.validationErrors() |
8 | 9 | ||
9 | if (errors) { | 10 | if (errors) { |
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 47825975c..e99cdefb1 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts | |||
@@ -1,9 +1,13 @@ | |||
1 | import 'express-validator' | ||
2 | import * as multer from 'multer' | ||
3 | import * as express from 'express' | ||
4 | |||
1 | import { database as db } from '../../initializers/database' | 5 | import { database as db } from '../../initializers/database' |
2 | import { checkErrors } from './utils' | 6 | import { checkErrors } from './utils' |
3 | import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' | 7 | import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' |
4 | import { logger, isVideoDurationValid } from '../../helpers' | 8 | import { logger, isVideoDurationValid } from '../../helpers' |
5 | 9 | ||
6 | function videosAddValidator (req, res, next) { | 10 | function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
7 | req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) | 11 | req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) |
8 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() | 12 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() |
9 | req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() | 13 | req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() |
@@ -27,13 +31,13 @@ function videosAddValidator (req, res, next) { | |||
27 | return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') | 31 | return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') |
28 | } | 32 | } |
29 | 33 | ||
30 | videoFile.duration = duration | 34 | videoFile['duration'] = duration |
31 | next() | 35 | next() |
32 | }) | 36 | }) |
33 | }) | 37 | }) |
34 | } | 38 | } |
35 | 39 | ||
36 | function videosUpdateValidator (req, res, next) { | 40 | function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
37 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 41 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
38 | req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() | 42 | req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() |
39 | req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() | 43 | req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() |
@@ -61,7 +65,7 @@ function videosUpdateValidator (req, res, next) { | |||
61 | }) | 65 | }) |
62 | } | 66 | } |
63 | 67 | ||
64 | function videosGetValidator (req, res, next) { | 68 | function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
65 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 69 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
66 | 70 | ||
67 | logger.debug('Checking videosGet parameters', { parameters: req.params }) | 71 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
@@ -71,7 +75,7 @@ function videosGetValidator (req, res, next) { | |||
71 | }) | 75 | }) |
72 | } | 76 | } |
73 | 77 | ||
74 | function videosRemoveValidator (req, res, next) { | 78 | function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
75 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 79 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
76 | 80 | ||
77 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) | 81 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
@@ -88,7 +92,7 @@ function videosRemoveValidator (req, res, next) { | |||
88 | }) | 92 | }) |
89 | } | 93 | } |
90 | 94 | ||
91 | function videosSearchValidator (req, res, next) { | 95 | function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
92 | const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS | 96 | const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS |
93 | req.checkParams('value', 'Should have a valid search').notEmpty() | 97 | req.checkParams('value', 'Should have a valid search').notEmpty() |
94 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) | 98 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) |
@@ -98,7 +102,7 @@ function videosSearchValidator (req, res, next) { | |||
98 | checkErrors(req, res, next) | 102 | checkErrors(req, res, next) |
99 | } | 103 | } |
100 | 104 | ||
101 | function videoAbuseReportValidator (req, res, next) { | 105 | function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
102 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 106 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
103 | req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() | 107 | req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() |
104 | 108 | ||
@@ -109,7 +113,7 @@ function videoAbuseReportValidator (req, res, next) { | |||
109 | }) | 113 | }) |
110 | } | 114 | } |
111 | 115 | ||
112 | function videoRateValidator (req, res, next) { | 116 | function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
113 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 117 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
114 | req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() | 118 | req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() |
115 | 119 | ||
@@ -120,7 +124,7 @@ function videoRateValidator (req, res, next) { | |||
120 | }) | 124 | }) |
121 | } | 125 | } |
122 | 126 | ||
123 | function videosBlacklistValidator (req, res, next) { | 127 | function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
124 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 128 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
125 | 129 | ||
126 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | 130 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) |
@@ -150,7 +154,7 @@ export { | |||
150 | 154 | ||
151 | // --------------------------------------------------------------------------- | 155 | // --------------------------------------------------------------------------- |
152 | 156 | ||
153 | function checkVideoExists (id, res, callback) { | 157 | function checkVideoExists (id: string, res: express.Response, callback: () => void) { |
154 | db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { | 158 | db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { |
155 | if (err) { | 159 | if (err) { |
156 | logger.error('Error in video request validator.', { error: err }) | 160 | logger.error('Error in video request validator.', { error: err }) |
@@ -164,7 +168,7 @@ function checkVideoExists (id, res, callback) { | |||
164 | }) | 168 | }) |
165 | } | 169 | } |
166 | 170 | ||
167 | function checkUserCanDeleteVideo (userId, res, callback) { | 171 | function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { |
168 | // Retrieve the user who did the request | 172 | // Retrieve the user who did the request |
169 | db.User.loadById(userId, function (err, user) { | 173 | db.User.loadById(userId, function (err, user) { |
170 | if (err) { | 174 | if (err) { |
@@ -190,7 +194,7 @@ function checkUserCanDeleteVideo (userId, res, callback) { | |||
190 | }) | 194 | }) |
191 | } | 195 | } |
192 | 196 | ||
193 | function checkVideoIsBlacklistable (req, res, callback) { | 197 | function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { |
194 | if (res.locals.video.isOwned() === true) { | 198 | if (res.locals.video.isOwned() === true) { |
195 | return res.status(403).send('Cannot blacklist a local video') | 199 | return res.status(403).send('Cannot blacklist a local video') |
196 | } | 200 | } |