diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/middlewares | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/admin.ts (renamed from server/middlewares/admin.js) | 10 | ||||
-rw-r--r-- | server/middlewares/index.js | 25 | ||||
-rw-r--r-- | server/middlewares/index.ts | 8 | ||||
-rw-r--r-- | server/middlewares/oauth.ts (renamed from server/middlewares/oauth.js) | 14 | ||||
-rw-r--r-- | server/middlewares/pagination.ts (renamed from server/middlewares/pagination.js) | 11 | ||||
-rw-r--r-- | server/middlewares/pods.ts (renamed from server/middlewares/pods.js) | 10 | ||||
-rw-r--r-- | server/middlewares/search.ts (renamed from server/middlewares/search.js) | 10 | ||||
-rw-r--r-- | server/middlewares/secure.ts (renamed from server/middlewares/secure.js) | 10 | ||||
-rw-r--r-- | server/middlewares/sort.ts (renamed from server/middlewares/sort.js) | 14 | ||||
-rw-r--r-- | server/middlewares/validators/index.js | 21 | ||||
-rw-r--r-- | server/middlewares/validators/index.ts | 6 | ||||
-rw-r--r-- | server/middlewares/validators/pagination.ts (renamed from server/middlewares/validators/pagination.js) | 16 | ||||
-rw-r--r-- | server/middlewares/validators/pods.ts (renamed from server/middlewares/validators/pods.js) | 32 | ||||
-rw-r--r-- | server/middlewares/validators/remote/index.js | 13 | ||||
-rw-r--r-- | server/middlewares/validators/remote/index.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/remote/signature.ts (renamed from server/middlewares/validators/remote/signature.js) | 16 | ||||
-rw-r--r-- | server/middlewares/validators/remote/videos.ts (renamed from server/middlewares/validators/remote/videos.js) | 25 | ||||
-rw-r--r-- | server/middlewares/validators/sort.ts (renamed from server/middlewares/validators/sort.js) | 32 | ||||
-rw-r--r-- | server/middlewares/validators/users.ts (renamed from server/middlewares/validators/users.js) | 28 | ||||
-rw-r--r-- | server/middlewares/validators/utils.ts (renamed from server/middlewares/validators/utils.js) | 18 | ||||
-rw-r--r-- | server/middlewares/validators/videos.ts (renamed from server/middlewares/validators/videos.js) | 59 |
21 files changed, 140 insertions, 240 deletions
diff --git a/server/middlewares/admin.js b/server/middlewares/admin.ts index 3288f4c6b..ebafa36a4 100644 --- a/server/middlewares/admin.js +++ b/server/middlewares/admin.ts | |||
@@ -1,11 +1,5 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const logger = require('../helpers/logger') | 1 | const logger = require('../helpers/logger') |
4 | 2 | ||
5 | const adminMiddleware = { | ||
6 | ensureIsAdmin | ||
7 | } | ||
8 | |||
9 | function ensureIsAdmin (req, res, next) { | 3 | function ensureIsAdmin (req, res, next) { |
10 | const user = res.locals.oauth.token.user | 4 | const user = res.locals.oauth.token.user |
11 | if (user.isAdmin() === false) { | 5 | if (user.isAdmin() === false) { |
@@ -18,4 +12,6 @@ function ensureIsAdmin (req, res, next) { | |||
18 | 12 | ||
19 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
20 | 14 | ||
21 | module.exports = adminMiddleware | 15 | export { |
16 | ensureIsAdmin | ||
17 | } | ||
diff --git a/server/middlewares/index.js b/server/middlewares/index.js deleted file mode 100644 index 3f253e31b..000000000 --- a/server/middlewares/index.js +++ /dev/null | |||
@@ -1,25 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const adminMiddleware = require('./admin') | ||
4 | const oauthMiddleware = require('./oauth') | ||
5 | const paginationMiddleware = require('./pagination') | ||
6 | const podsMiddleware = require('./pods') | ||
7 | const validatorsMiddleware = require('./validators') | ||
8 | const searchMiddleware = require('./search') | ||
9 | const sortMiddleware = require('./sort') | ||
10 | const secureMiddleware = require('./secure') | ||
11 | |||
12 | const middlewares = { | ||
13 | admin: adminMiddleware, | ||
14 | oauth: oauthMiddleware, | ||
15 | pagination: paginationMiddleware, | ||
16 | pods: podsMiddleware, | ||
17 | search: searchMiddleware, | ||
18 | secure: secureMiddleware, | ||
19 | sort: sortMiddleware, | ||
20 | validators: validatorsMiddleware | ||
21 | } | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | module.exports = middlewares | ||
diff --git a/server/middlewares/index.ts b/server/middlewares/index.ts new file mode 100644 index 000000000..2c1c5fa53 --- /dev/null +++ b/server/middlewares/index.ts | |||
@@ -0,0 +1,8 @@ | |||
1 | export * from './validators'; | ||
2 | export * from './admin'; | ||
3 | export * from './oauth'; | ||
4 | export * from './pagination'; | ||
5 | export * from './pods'; | ||
6 | export * from './search'; | ||
7 | export * from './secure'; | ||
8 | export * from './sort'; | ||
diff --git a/server/middlewares/oauth.js b/server/middlewares/oauth.ts index 3a02b9b48..31ae1e000 100644 --- a/server/middlewares/oauth.js +++ b/server/middlewares/oauth.ts | |||
@@ -1,6 +1,4 @@ | |||
1 | 'use strict' | 1 | import OAuthServer = require('express-oauth-server') |
2 | |||
3 | const OAuthServer = require('express-oauth-server') | ||
4 | 2 | ||
5 | const constants = require('../initializers/constants') | 3 | const constants = require('../initializers/constants') |
6 | const logger = require('../helpers/logger') | 4 | const logger = require('../helpers/logger') |
@@ -11,11 +9,6 @@ const oAuthServer = new OAuthServer({ | |||
11 | model: require('../lib/oauth-model') | 9 | model: require('../lib/oauth-model') |
12 | }) | 10 | }) |
13 | 11 | ||
14 | const oAuth = { | ||
15 | authenticate, | ||
16 | token | ||
17 | } | ||
18 | |||
19 | function authenticate (req, res, next) { | 12 | function authenticate (req, res, next) { |
20 | oAuthServer.authenticate()(req, res, function (err) { | 13 | oAuthServer.authenticate()(req, res, function (err) { |
21 | if (err) { | 14 | if (err) { |
@@ -35,4 +28,7 @@ function token (req, res, next) { | |||
35 | 28 | ||
36 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
37 | 30 | ||
38 | module.exports = oAuth | 31 | export { |
32 | authenticate, | ||
33 | token | ||
34 | } | ||
diff --git a/server/middlewares/pagination.js b/server/middlewares/pagination.ts index a90f60aab..8fe9f9082 100644 --- a/server/middlewares/pagination.js +++ b/server/middlewares/pagination.ts | |||
@@ -1,14 +1,9 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const constants = require('../initializers/constants') | 1 | const constants = require('../initializers/constants') |
4 | 2 | ||
5 | const paginationMiddleware = { | ||
6 | setPagination | ||
7 | } | ||
8 | |||
9 | function setPagination (req, res, next) { | 3 | function setPagination (req, res, next) { |
10 | if (!req.query.start) req.query.start = 0 | 4 | if (!req.query.start) req.query.start = 0 |
11 | else req.query.start = parseInt(req.query.start, 10) | 5 | else req.query.start = parseInt(req.query.start, 10) |
6 | |||
12 | if (!req.query.count) req.query.count = constants.PAGINATION_COUNT_DEFAULT | 7 | if (!req.query.count) req.query.count = constants.PAGINATION_COUNT_DEFAULT |
13 | else req.query.count = parseInt(req.query.count, 10) | 8 | else req.query.count = parseInt(req.query.count, 10) |
14 | 9 | ||
@@ -17,4 +12,6 @@ function setPagination (req, res, next) { | |||
17 | 12 | ||
18 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
19 | 14 | ||
20 | module.exports = paginationMiddleware | 15 | export { |
16 | setPagination | ||
17 | } | ||
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.ts index 2647f9ff0..e405f265e 100644 --- a/server/middlewares/pods.js +++ b/server/middlewares/pods.ts | |||
@@ -2,11 +2,6 @@ | |||
2 | 2 | ||
3 | const constants = require('../initializers/constants') | 3 | const constants = require('../initializers/constants') |
4 | 4 | ||
5 | const podsMiddleware = { | ||
6 | setBodyHostsPort, | ||
7 | setBodyHostPort | ||
8 | } | ||
9 | |||
10 | function setBodyHostsPort (req, res, next) { | 5 | function setBodyHostsPort (req, res, next) { |
11 | if (!req.body.hosts) return next() | 6 | if (!req.body.hosts) return next() |
12 | 7 | ||
@@ -41,7 +36,10 @@ function setBodyHostPort (req, res, next) { | |||
41 | 36 | ||
42 | // --------------------------------------------------------------------------- | 37 | // --------------------------------------------------------------------------- |
43 | 38 | ||
44 | module.exports = podsMiddleware | 39 | export { |
40 | setBodyHostsPort, | ||
41 | setBodyHostPort | ||
42 | } | ||
45 | 43 | ||
46 | // --------------------------------------------------------------------------- | 44 | // --------------------------------------------------------------------------- |
47 | 45 | ||
diff --git a/server/middlewares/search.js b/server/middlewares/search.ts index bb88faf54..05a2e7442 100644 --- a/server/middlewares/search.js +++ b/server/middlewares/search.ts | |||
@@ -1,9 +1,3 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const searchMiddleware = { | ||
4 | setVideosSearch | ||
5 | } | ||
6 | |||
7 | function setVideosSearch (req, res, next) { | 1 | function setVideosSearch (req, res, next) { |
8 | if (!req.query.field) req.query.field = 'name' | 2 | if (!req.query.field) req.query.field = 'name' |
9 | 3 | ||
@@ -12,4 +6,6 @@ function setVideosSearch (req, res, next) { | |||
12 | 6 | ||
13 | // --------------------------------------------------------------------------- | 7 | // --------------------------------------------------------------------------- |
14 | 8 | ||
15 | module.exports = searchMiddleware | 9 | export { |
10 | setVideosSearch | ||
11 | } | ||
diff --git a/server/middlewares/secure.js b/server/middlewares/secure.ts index 7c5c72508..ee8545028 100644 --- a/server/middlewares/secure.js +++ b/server/middlewares/secure.ts | |||
@@ -1,13 +1,7 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const db = require('../initializers/database') | 1 | const db = require('../initializers/database') |
4 | const logger = require('../helpers/logger') | 2 | const logger = require('../helpers/logger') |
5 | const peertubeCrypto = require('../helpers/peertube-crypto') | 3 | const peertubeCrypto = require('../helpers/peertube-crypto') |
6 | 4 | ||
7 | const secureMiddleware = { | ||
8 | checkSignature | ||
9 | } | ||
10 | |||
11 | function checkSignature (req, res, next) { | 5 | function checkSignature (req, res, next) { |
12 | const host = req.body.signature.host | 6 | const host = req.body.signature.host |
13 | db.Pod.loadByHost(host, function (err, pod) { | 7 | db.Pod.loadByHost(host, function (err, pod) { |
@@ -49,4 +43,6 @@ function checkSignature (req, res, next) { | |||
49 | 43 | ||
50 | // --------------------------------------------------------------------------- | 44 | // --------------------------------------------------------------------------- |
51 | 45 | ||
52 | module.exports = secureMiddleware | 46 | export { |
47 | checkSignature | ||
48 | } | ||
diff --git a/server/middlewares/sort.js b/server/middlewares/sort.ts index 39e167265..ab9ccf524 100644 --- a/server/middlewares/sort.js +++ b/server/middlewares/sort.ts | |||
@@ -1,11 +1,3 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const sortMiddleware = { | ||
4 | setUsersSort, | ||
5 | setVideoAbusesSort, | ||
6 | setVideosSort | ||
7 | } | ||
8 | |||
9 | function setUsersSort (req, res, next) { | 1 | function setUsersSort (req, res, next) { |
10 | if (!req.query.sort) req.query.sort = '-createdAt' | 2 | if (!req.query.sort) req.query.sort = '-createdAt' |
11 | 3 | ||
@@ -26,4 +18,8 @@ function setVideosSort (req, res, next) { | |||
26 | 18 | ||
27 | // --------------------------------------------------------------------------- | 19 | // --------------------------------------------------------------------------- |
28 | 20 | ||
29 | module.exports = sortMiddleware | 21 | export { |
22 | setUsersSort, | ||
23 | setVideoAbusesSort, | ||
24 | setVideosSort | ||
25 | } | ||
diff --git a/server/middlewares/validators/index.js b/server/middlewares/validators/index.js deleted file mode 100644 index 6c3a9c2b4..000000000 --- a/server/middlewares/validators/index.js +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const paginationValidators = require('./pagination') | ||
4 | const podsValidators = require('./pods') | ||
5 | const remoteValidators = require('./remote') | ||
6 | const sortValidators = require('./sort') | ||
7 | const usersValidators = require('./users') | ||
8 | const videosValidators = require('./videos') | ||
9 | |||
10 | const validators = { | ||
11 | pagination: paginationValidators, | ||
12 | pods: podsValidators, | ||
13 | remote: remoteValidators, | ||
14 | sort: sortValidators, | ||
15 | users: usersValidators, | ||
16 | videos: videosValidators | ||
17 | } | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | module.exports = validators | ||
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts new file mode 100644 index 000000000..42ba465ec --- /dev/null +++ b/server/middlewares/validators/index.ts | |||
@@ -0,0 +1,6 @@ | |||
1 | export * from './remote' | ||
2 | export * from './pagination' | ||
3 | export * from './pods' | ||
4 | export * from './sort' | ||
5 | export * from './users' | ||
6 | export * from './videos' | ||
diff --git a/server/middlewares/validators/pagination.js b/server/middlewares/validators/pagination.ts index 16682696e..de719c05b 100644 --- a/server/middlewares/validators/pagination.js +++ b/server/middlewares/validators/pagination.ts | |||
@@ -1,13 +1,7 @@ | |||
1 | 'use strict' | 1 | import { checkErrors } from './utils' |
2 | import { logger } from '../../helpers' | ||
2 | 3 | ||
3 | const checkErrors = require('./utils').checkErrors | 4 | function paginationValidator (req, res, next) { |
4 | const logger = require('../../helpers/logger') | ||
5 | |||
6 | const validatorsPagination = { | ||
7 | pagination | ||
8 | } | ||
9 | |||
10 | function pagination (req, res, next) { | ||
11 | req.checkQuery('start', 'Should have a number start').optional().isInt() | 5 | req.checkQuery('start', 'Should have a number start').optional().isInt() |
12 | req.checkQuery('count', 'Should have a number count').optional().isInt() | 6 | req.checkQuery('count', 'Should have a number count').optional().isInt() |
13 | 7 | ||
@@ -18,4 +12,6 @@ function pagination (req, res, next) { | |||
18 | 12 | ||
19 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
20 | 14 | ||
21 | module.exports = validatorsPagination | 15 | export { |
16 | paginationValidator | ||
17 | } | ||
diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.ts index 0bf4b1844..fbfd268d0 100644 --- a/server/middlewares/validators/pods.js +++ b/server/middlewares/validators/pods.ts | |||
@@ -1,20 +1,13 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const constants = require('../../initializers/constants') | ||
5 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
6 | const friends = require('../../lib/friends') | 2 | import { checkErrors } from './utils' |
7 | const logger = require('../../helpers/logger') | 3 | import { logger } from '../../helpers' |
8 | const utils = require('../../helpers/utils') | 4 | import { CONFIG } from '../../initializers' |
9 | 5 | import { hasFriends } from '../../lib' | |
10 | const validatorsPod = { | 6 | import { isTestInstance } from '../../helpers' |
11 | makeFriends, | ||
12 | podsAdd | ||
13 | } | ||
14 | 7 | ||
15 | function makeFriends (req, res, next) { | 8 | function makeFriendsValidator (req, res, next) { |
16 | // Force https if the administrator wants to make friends | 9 | // Force https if the administrator wants to make friends |
17 | if (utils.isTestInstance() === false && constants.CONFIG.WEBSERVER.SCHEME === 'http') { | 10 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { |
18 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') | 11 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') |
19 | } | 12 | } |
20 | 13 | ||
@@ -23,13 +16,13 @@ function makeFriends (req, res, next) { | |||
23 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) | 16 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) |
24 | 17 | ||
25 | checkErrors(req, res, function () { | 18 | checkErrors(req, res, function () { |
26 | friends.hasFriends(function (err, hasFriends) { | 19 | hasFriends(function (err, heHasFriends) { |
27 | if (err) { | 20 | if (err) { |
28 | logger.error('Cannot know if we have friends.', { error: err }) | 21 | logger.error('Cannot know if we have friends.', { error: err }) |
29 | res.sendStatus(500) | 22 | res.sendStatus(500) |
30 | } | 23 | } |
31 | 24 | ||
32 | if (hasFriends === true) { | 25 | if (heHasFriends === true) { |
33 | // We need to quit our friends before make new ones | 26 | // We need to quit our friends before make new ones |
34 | return res.sendStatus(409) | 27 | return res.sendStatus(409) |
35 | } | 28 | } |
@@ -39,7 +32,7 @@ function makeFriends (req, res, next) { | |||
39 | }) | 32 | }) |
40 | } | 33 | } |
41 | 34 | ||
42 | function podsAdd (req, res, next) { | 35 | function podsAddValidator (req, res, next) { |
43 | req.checkBody('host', 'Should have a host').isHostValid() | 36 | req.checkBody('host', 'Should have a host').isHostValid() |
44 | req.checkBody('email', 'Should have an email').isEmail() | 37 | req.checkBody('email', 'Should have an email').isEmail() |
45 | req.checkBody('publicKey', 'Should have a public key').notEmpty() | 38 | req.checkBody('publicKey', 'Should have a public key').notEmpty() |
@@ -64,4 +57,7 @@ function podsAdd (req, res, next) { | |||
64 | 57 | ||
65 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
66 | 59 | ||
67 | module.exports = validatorsPod | 60 | export { |
61 | makeFriendsValidator, | ||
62 | podsAddValidator | ||
63 | } | ||
diff --git a/server/middlewares/validators/remote/index.js b/server/middlewares/validators/remote/index.js deleted file mode 100644 index 022a2fe50..000000000 --- a/server/middlewares/validators/remote/index.js +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const remoteSignatureValidators = require('./signature') | ||
4 | const remoteVideosValidators = require('./videos') | ||
5 | |||
6 | const validators = { | ||
7 | signature: remoteSignatureValidators, | ||
8 | videos: remoteVideosValidators | ||
9 | } | ||
10 | |||
11 | // --------------------------------------------------------------------------- | ||
12 | |||
13 | module.exports = validators | ||
diff --git a/server/middlewares/validators/remote/index.ts b/server/middlewares/validators/remote/index.ts new file mode 100644 index 000000000..d0d7740b1 --- /dev/null +++ b/server/middlewares/validators/remote/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './signature' | ||
2 | export * from './videos' | ||
diff --git a/server/middlewares/validators/remote/signature.js b/server/middlewares/validators/remote/signature.ts index 002232c05..6e3ebe7db 100644 --- a/server/middlewares/validators/remote/signature.js +++ b/server/middlewares/validators/remote/signature.ts | |||
@@ -1,13 +1,7 @@ | |||
1 | 'use strict' | 1 | import { logger } from '../../../helpers' |
2 | import { checkErrors } from '../utils' | ||
2 | 3 | ||
3 | const checkErrors = require('../utils').checkErrors | 4 | function signatureValidator (req, res, next) { |
4 | const logger = require('../../../helpers/logger') | ||
5 | |||
6 | const validatorsRemoteSignature = { | ||
7 | signature | ||
8 | } | ||
9 | |||
10 | function signature (req, res, next) { | ||
11 | req.checkBody('signature.host', 'Should have a signature host').isURL() | 5 | req.checkBody('signature.host', 'Should have a signature host').isURL() |
12 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() | 6 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() |
13 | 7 | ||
@@ -18,4 +12,6 @@ function signature (req, res, next) { | |||
18 | 12 | ||
19 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
20 | 14 | ||
21 | module.exports = validatorsRemoteSignature | 15 | export { |
16 | signatureValidator | ||
17 | } | ||
diff --git a/server/middlewares/validators/remote/videos.js b/server/middlewares/validators/remote/videos.ts index f2c6cba5e..3380c29e2 100644 --- a/server/middlewares/validators/remote/videos.js +++ b/server/middlewares/validators/remote/videos.ts | |||
@@ -1,15 +1,7 @@ | |||
1 | 'use strict' | 1 | import { logger } from '../../../helpers' |
2 | import { checkErrors } from '../utils' | ||
2 | 3 | ||
3 | const checkErrors = require('../utils').checkErrors | 4 | function remoteVideosValidator (req, res, next) { |
4 | const logger = require('../../../helpers/logger') | ||
5 | |||
6 | const validatorsRemoteVideos = { | ||
7 | remoteVideos, | ||
8 | remoteQaduVideos, | ||
9 | remoteEventsVideos | ||
10 | } | ||
11 | |||
12 | function remoteVideos (req, res, next) { | ||
13 | req.checkBody('data').isEachRemoteRequestVideosValid() | 5 | req.checkBody('data').isEachRemoteRequestVideosValid() |
14 | 6 | ||
15 | logger.debug('Checking remoteVideos parameters', { parameters: req.body }) | 7 | logger.debug('Checking remoteVideos parameters', { parameters: req.body }) |
@@ -17,7 +9,7 @@ function remoteVideos (req, res, next) { | |||
17 | checkErrors(req, res, next) | 9 | checkErrors(req, res, next) |
18 | } | 10 | } |
19 | 11 | ||
20 | function remoteQaduVideos (req, res, next) { | 12 | function remoteQaduVideosValidator (req, res, next) { |
21 | req.checkBody('data').isEachRemoteRequestVideosQaduValid() | 13 | req.checkBody('data').isEachRemoteRequestVideosQaduValid() |
22 | 14 | ||
23 | logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) | 15 | logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) |
@@ -25,13 +17,18 @@ function remoteQaduVideos (req, res, next) { | |||
25 | checkErrors(req, res, next) | 17 | checkErrors(req, res, next) |
26 | } | 18 | } |
27 | 19 | ||
28 | function remoteEventsVideos (req, res, next) { | 20 | function remoteEventsVideosValidator (req, res, next) { |
29 | req.checkBody('data').isEachRemoteRequestVideosEventsValid() | 21 | req.checkBody('data').isEachRemoteRequestVideosEventsValid() |
30 | 22 | ||
31 | logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) | 23 | logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) |
32 | 24 | ||
33 | checkErrors(req, res, next) | 25 | checkErrors(req, res, next) |
34 | } | 26 | } |
27 | |||
35 | // --------------------------------------------------------------------------- | 28 | // --------------------------------------------------------------------------- |
36 | 29 | ||
37 | module.exports = validatorsRemoteVideos | 30 | export { |
31 | remoteVideosValidator, | ||
32 | remoteQaduVideosValidator, | ||
33 | remoteEventsVideosValidator | ||
34 | } | ||
diff --git a/server/middlewares/validators/sort.js b/server/middlewares/validators/sort.ts index 017d266e6..ebc7333c7 100644 --- a/server/middlewares/validators/sort.js +++ b/server/middlewares/validators/sort.ts | |||
@@ -1,35 +1,31 @@ | |||
1 | 'use strict' | 1 | import { checkErrors } from './utils' |
2 | 2 | import { logger } from '../../helpers' | |
3 | const checkErrors = require('./utils').checkErrors | 3 | import { SORTABLE_COLUMNS } from '../../initializers' |
4 | const constants = require('../../initializers/constants') | ||
5 | const logger = require('../../helpers/logger') | ||
6 | |||
7 | const validatorsSort = { | ||
8 | usersSort, | ||
9 | videoAbusesSort, | ||
10 | videosSort | ||
11 | } | ||
12 | 4 | ||
13 | // Initialize constants here for better performances | 5 | // Initialize constants here for better performances |
14 | const SORTABLE_USERS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.USERS) | 6 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) |
15 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEO_ABUSES) | 7 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) |
16 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEOS) | 8 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) |
17 | 9 | ||
18 | function usersSort (req, res, next) { | 10 | function usersSortValidator (req, res, next) { |
19 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) | 11 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) |
20 | } | 12 | } |
21 | 13 | ||
22 | function videoAbusesSort (req, res, next) { | 14 | function videoAbusesSortValidator (req, res, next) { |
23 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) | 15 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) |
24 | } | 16 | } |
25 | 17 | ||
26 | function videosSort (req, res, next) { | 18 | function videosSortValidator (req, res, next) { |
27 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) | 19 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) |
28 | } | 20 | } |
29 | 21 | ||
30 | // --------------------------------------------------------------------------- | 22 | // --------------------------------------------------------------------------- |
31 | 23 | ||
32 | module.exports = validatorsSort | 24 | export { |
25 | usersSortValidator, | ||
26 | videoAbusesSortValidator, | ||
27 | videosSortValidator | ||
28 | } | ||
33 | 29 | ||
34 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
35 | 31 | ||
diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.ts index 1e7a64793..a9149fe1b 100644 --- a/server/middlewares/validators/users.js +++ b/server/middlewares/validators/users.ts | |||
@@ -1,17 +1,8 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
5 | const logger = require('../../helpers/logger') | 2 | import { checkErrors } from './utils' |
6 | 3 | import { logger } from '../../helpers' | |
7 | const validatorsUsers = { | ||
8 | usersAdd, | ||
9 | usersRemove, | ||
10 | usersUpdate, | ||
11 | usersVideoRating | ||
12 | } | ||
13 | 4 | ||
14 | function usersAdd (req, res, next) { | 5 | function usersAddValidator (req, res, next) { |
15 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() | 6 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() |
16 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() | 7 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() |
17 | req.checkBody('email', 'Should have a valid email').isEmail() | 8 | req.checkBody('email', 'Should have a valid email').isEmail() |
@@ -32,7 +23,7 @@ function usersAdd (req, res, next) { | |||
32 | }) | 23 | }) |
33 | } | 24 | } |
34 | 25 | ||
35 | function usersRemove (req, res, next) { | 26 | function usersRemoveValidator (req, res, next) { |
36 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | 27 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
37 | 28 | ||
38 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | 29 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) |
@@ -53,7 +44,7 @@ function usersRemove (req, res, next) { | |||
53 | }) | 44 | }) |
54 | } | 45 | } |
55 | 46 | ||
56 | function usersUpdate (req, res, next) { | 47 | function usersUpdateValidator (req, res, next) { |
57 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | 48 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
58 | // Add old password verification | 49 | // Add old password verification |
59 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() | 50 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() |
@@ -64,7 +55,7 @@ function usersUpdate (req, res, next) { | |||
64 | checkErrors(req, res, next) | 55 | checkErrors(req, res, next) |
65 | } | 56 | } |
66 | 57 | ||
67 | function usersVideoRating (req, res, next) { | 58 | function usersVideoRatingValidator (req, res, next) { |
68 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) | 59 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) |
69 | 60 | ||
70 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) | 61 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) |
@@ -85,4 +76,9 @@ function usersVideoRating (req, res, next) { | |||
85 | 76 | ||
86 | // --------------------------------------------------------------------------- | 77 | // --------------------------------------------------------------------------- |
87 | 78 | ||
88 | module.exports = validatorsUsers | 79 | export { |
80 | usersAddValidator, | ||
81 | usersRemoveValidator, | ||
82 | usersUpdateValidator, | ||
83 | usersVideoRatingValidator | ||
84 | } | ||
diff --git a/server/middlewares/validators/utils.js b/server/middlewares/validators/utils.ts index 3741b84c6..710e65529 100644 --- a/server/middlewares/validators/utils.js +++ b/server/middlewares/validators/utils.ts | |||
@@ -1,20 +1,14 @@ | |||
1 | 'use strict' | 1 | import { inspect } from 'util' |
2 | 2 | ||
3 | const util = require('util') | 3 | import { logger } from '../../helpers' |
4 | 4 | ||
5 | const logger = require('../../helpers/logger') | 5 | function checkErrors (req, res, next, statusCode?) { |
6 | |||
7 | const validatorsUtils = { | ||
8 | checkErrors | ||
9 | } | ||
10 | |||
11 | function checkErrors (req, res, next, statusCode) { | ||
12 | if (statusCode === undefined) statusCode = 400 | 6 | if (statusCode === undefined) statusCode = 400 |
13 | const errors = req.validationErrors() | 7 | const errors = req.validationErrors() |
14 | 8 | ||
15 | if (errors) { | 9 | if (errors) { |
16 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) | 10 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) |
17 | return res.status(statusCode).send('There have been validation errors: ' + util.inspect(errors)) | 11 | return res.status(statusCode).send('There have been validation errors: ' + inspect(errors)) |
18 | } | 12 | } |
19 | 13 | ||
20 | return next() | 14 | return next() |
@@ -22,4 +16,6 @@ function checkErrors (req, res, next, statusCode) { | |||
22 | 16 | ||
23 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
24 | 18 | ||
25 | module.exports = validatorsUtils | 19 | export { |
20 | checkErrors | ||
21 | } | ||
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.ts index f18ca1597..5a49cf73c 100644 --- a/server/middlewares/validators/videos.js +++ b/server/middlewares/validators/videos.ts | |||
@@ -1,26 +1,9 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const constants = require('../../initializers/constants') | ||
5 | const customVideosValidators = require('../../helpers/custom-validators').videos | ||
6 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
7 | const logger = require('../../helpers/logger') | 2 | import { checkErrors } from './utils' |
8 | 3 | import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' | |
9 | const validatorsVideos = { | 4 | import { logger, isVideoDurationValid } from '../../helpers' |
10 | videosAdd, | ||
11 | videosUpdate, | ||
12 | videosGet, | ||
13 | videosRemove, | ||
14 | videosSearch, | ||
15 | |||
16 | videoAbuseReport, | ||
17 | |||
18 | videoRate, | ||
19 | |||
20 | videosBlacklist | ||
21 | } | ||
22 | 5 | ||
23 | function videosAdd (req, res, next) { | 6 | function videosAddValidator (req, res, next) { |
24 | req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) | 7 | req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) |
25 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() | 8 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() |
26 | req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() | 9 | req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() |
@@ -40,8 +23,8 @@ function videosAdd (req, res, next) { | |||
40 | return res.status(400).send('Cannot retrieve metadata of the file.') | 23 | return res.status(400).send('Cannot retrieve metadata of the file.') |
41 | } | 24 | } |
42 | 25 | ||
43 | if (!customVideosValidators.isVideoDurationValid(duration)) { | 26 | if (!isVideoDurationValid(duration)) { |
44 | return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') | 27 | return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') |
45 | } | 28 | } |
46 | 29 | ||
47 | videoFile.duration = duration | 30 | videoFile.duration = duration |
@@ -50,7 +33,7 @@ function videosAdd (req, res, next) { | |||
50 | }) | 33 | }) |
51 | } | 34 | } |
52 | 35 | ||
53 | function videosUpdate (req, res, next) { | 36 | function videosUpdateValidator (req, res, next) { |
54 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 37 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
55 | req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() | 38 | req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() |
56 | req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() | 39 | req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() |
@@ -78,7 +61,7 @@ function videosUpdate (req, res, next) { | |||
78 | }) | 61 | }) |
79 | } | 62 | } |
80 | 63 | ||
81 | function videosGet (req, res, next) { | 64 | function videosGetValidator (req, res, next) { |
82 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 65 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
83 | 66 | ||
84 | logger.debug('Checking videosGet parameters', { parameters: req.params }) | 67 | logger.debug('Checking videosGet parameters', { parameters: req.params }) |
@@ -88,7 +71,7 @@ function videosGet (req, res, next) { | |||
88 | }) | 71 | }) |
89 | } | 72 | } |
90 | 73 | ||
91 | function videosRemove (req, res, next) { | 74 | function videosRemoveValidator (req, res, next) { |
92 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 75 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
93 | 76 | ||
94 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) | 77 | logger.debug('Checking videosRemove parameters', { parameters: req.params }) |
@@ -105,8 +88,8 @@ function videosRemove (req, res, next) { | |||
105 | }) | 88 | }) |
106 | } | 89 | } |
107 | 90 | ||
108 | function videosSearch (req, res, next) { | 91 | function videosSearchValidator (req, res, next) { |
109 | const searchableColumns = constants.SEARCHABLE_COLUMNS.VIDEOS | 92 | const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS |
110 | req.checkParams('value', 'Should have a valid search').notEmpty() | 93 | req.checkParams('value', 'Should have a valid search').notEmpty() |
111 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) | 94 | req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) |
112 | 95 | ||
@@ -115,7 +98,7 @@ function videosSearch (req, res, next) { | |||
115 | checkErrors(req, res, next) | 98 | checkErrors(req, res, next) |
116 | } | 99 | } |
117 | 100 | ||
118 | function videoAbuseReport (req, res, next) { | 101 | function videoAbuseReportValidator (req, res, next) { |
119 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 102 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
120 | req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() | 103 | req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() |
121 | 104 | ||
@@ -126,7 +109,7 @@ function videoAbuseReport (req, res, next) { | |||
126 | }) | 109 | }) |
127 | } | 110 | } |
128 | 111 | ||
129 | function videoRate (req, res, next) { | 112 | function videoRateValidator (req, res, next) { |
130 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 113 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
131 | req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() | 114 | req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() |
132 | 115 | ||
@@ -137,7 +120,7 @@ function videoRate (req, res, next) { | |||
137 | }) | 120 | }) |
138 | } | 121 | } |
139 | 122 | ||
140 | function videosBlacklist (req, res, next) { | 123 | function videosBlacklistValidator (req, res, next) { |
141 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) | 124 | req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) |
142 | 125 | ||
143 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) | 126 | logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) |
@@ -151,7 +134,19 @@ function videosBlacklist (req, res, next) { | |||
151 | 134 | ||
152 | // --------------------------------------------------------------------------- | 135 | // --------------------------------------------------------------------------- |
153 | 136 | ||
154 | module.exports = validatorsVideos | 137 | export { |
138 | videosAddValidator, | ||
139 | videosUpdateValidator, | ||
140 | videosGetValidator, | ||
141 | videosRemoveValidator, | ||
142 | videosSearchValidator, | ||
143 | |||
144 | videoAbuseReportValidator, | ||
145 | |||
146 | videoRateValidator, | ||
147 | |||
148 | videosBlacklistValidator | ||
149 | } | ||
155 | 150 | ||
156 | // --------------------------------------------------------------------------- | 151 | // --------------------------------------------------------------------------- |
157 | 152 | ||