aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
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.js25
-rw-r--r--server/middlewares/index.ts8
-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.js21
-rw-r--r--server/middlewares/validators/index.ts6
-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.js13
-rw-r--r--server/middlewares/validators/remote/index.ts2
-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
3const logger = require('../helpers/logger') 1const logger = require('../helpers/logger')
4 2
5const adminMiddleware = {
6 ensureIsAdmin
7}
8
9function ensureIsAdmin (req, res, next) { 3function 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
21module.exports = adminMiddleware 15export {
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
3const adminMiddleware = require('./admin')
4const oauthMiddleware = require('./oauth')
5const paginationMiddleware = require('./pagination')
6const podsMiddleware = require('./pods')
7const validatorsMiddleware = require('./validators')
8const searchMiddleware = require('./search')
9const sortMiddleware = require('./sort')
10const secureMiddleware = require('./secure')
11
12const 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
25module.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 @@
1export * from './validators';
2export * from './admin';
3export * from './oauth';
4export * from './pagination';
5export * from './pods';
6export * from './search';
7export * from './secure';
8export * 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' 1import OAuthServer = require('express-oauth-server')
2
3const OAuthServer = require('express-oauth-server')
4 2
5const constants = require('../initializers/constants') 3const constants = require('../initializers/constants')
6const logger = require('../helpers/logger') 4const 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
14const oAuth = {
15 authenticate,
16 token
17}
18
19function authenticate (req, res, next) { 12function 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
38module.exports = oAuth 31export {
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
3const constants = require('../initializers/constants') 1const constants = require('../initializers/constants')
4 2
5const paginationMiddleware = {
6 setPagination
7}
8
9function setPagination (req, res, next) { 3function 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
20module.exports = paginationMiddleware 15export {
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
3const constants = require('../initializers/constants') 3const constants = require('../initializers/constants')
4 4
5const podsMiddleware = {
6 setBodyHostsPort,
7 setBodyHostPort
8}
9
10function setBodyHostsPort (req, res, next) { 5function 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
44module.exports = podsMiddleware 39export {
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
3const searchMiddleware = {
4 setVideosSearch
5}
6
7function setVideosSearch (req, res, next) { 1function 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
15module.exports = searchMiddleware 9export {
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
3const db = require('../initializers/database') 1const db = require('../initializers/database')
4const logger = require('../helpers/logger') 2const logger = require('../helpers/logger')
5const peertubeCrypto = require('../helpers/peertube-crypto') 3const peertubeCrypto = require('../helpers/peertube-crypto')
6 4
7const secureMiddleware = {
8 checkSignature
9}
10
11function checkSignature (req, res, next) { 5function 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
52module.exports = secureMiddleware 46export {
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
3const sortMiddleware = {
4 setUsersSort,
5 setVideoAbusesSort,
6 setVideosSort
7}
8
9function setUsersSort (req, res, next) { 1function 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
29module.exports = sortMiddleware 21export {
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
3const paginationValidators = require('./pagination')
4const podsValidators = require('./pods')
5const remoteValidators = require('./remote')
6const sortValidators = require('./sort')
7const usersValidators = require('./users')
8const videosValidators = require('./videos')
9
10const validators = {
11 pagination: paginationValidators,
12 pods: podsValidators,
13 remote: remoteValidators,
14 sort: sortValidators,
15 users: usersValidators,
16 videos: videosValidators
17}
18
19// ---------------------------------------------------------------------------
20
21module.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 @@
1export * from './remote'
2export * from './pagination'
3export * from './pods'
4export * from './sort'
5export * from './users'
6export * 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' 1import { checkErrors } from './utils'
2import { logger } from '../../helpers'
2 3
3const checkErrors = require('./utils').checkErrors 4function paginationValidator (req, res, next) {
4const logger = require('../../helpers/logger')
5
6const validatorsPagination = {
7 pagination
8}
9
10function 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
21module.exports = validatorsPagination 15export {
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
3const checkErrors = require('./utils').checkErrors
4const constants = require('../../initializers/constants')
5const db = require('../../initializers/database') 1const db = require('../../initializers/database')
6const friends = require('../../lib/friends') 2import { checkErrors } from './utils'
7const logger = require('../../helpers/logger') 3import { logger } from '../../helpers'
8const utils = require('../../helpers/utils') 4import { CONFIG } from '../../initializers'
9 5import { hasFriends } from '../../lib'
10const validatorsPod = { 6import { isTestInstance } from '../../helpers'
11 makeFriends,
12 podsAdd
13}
14 7
15function makeFriends (req, res, next) { 8function 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
42function podsAdd (req, res, next) { 35function 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
67module.exports = validatorsPod 60export {
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
3const remoteSignatureValidators = require('./signature')
4const remoteVideosValidators = require('./videos')
5
6const validators = {
7 signature: remoteSignatureValidators,
8 videos: remoteVideosValidators
9}
10
11// ---------------------------------------------------------------------------
12
13module.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 @@
1export * from './signature'
2export * 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' 1import { logger } from '../../../helpers'
2import { checkErrors } from '../utils'
2 3
3const checkErrors = require('../utils').checkErrors 4function signatureValidator (req, res, next) {
4const logger = require('../../../helpers/logger')
5
6const validatorsRemoteSignature = {
7 signature
8}
9
10function 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
21module.exports = validatorsRemoteSignature 15export {
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' 1import { logger } from '../../../helpers'
2import { checkErrors } from '../utils'
2 3
3const checkErrors = require('../utils').checkErrors 4function remoteVideosValidator (req, res, next) {
4const logger = require('../../../helpers/logger')
5
6const validatorsRemoteVideos = {
7 remoteVideos,
8 remoteQaduVideos,
9 remoteEventsVideos
10}
11
12function 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
20function remoteQaduVideos (req, res, next) { 12function 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
28function remoteEventsVideos (req, res, next) { 20function 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
37module.exports = validatorsRemoteVideos 30export {
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' 1import { checkErrors } from './utils'
2 2import { logger } from '../../helpers'
3const checkErrors = require('./utils').checkErrors 3import { SORTABLE_COLUMNS } from '../../initializers'
4const constants = require('../../initializers/constants')
5const logger = require('../../helpers/logger')
6
7const validatorsSort = {
8 usersSort,
9 videoAbusesSort,
10 videosSort
11}
12 4
13// Initialize constants here for better performances 5// Initialize constants here for better performances
14const SORTABLE_USERS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.USERS) 6const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
15const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEO_ABUSES) 7const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
16const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEOS) 8const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
17 9
18function usersSort (req, res, next) { 10function usersSortValidator (req, res, next) {
19 checkSort(req, res, next, SORTABLE_USERS_COLUMNS) 11 checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
20} 12}
21 13
22function videoAbusesSort (req, res, next) { 14function 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
26function videosSort (req, res, next) { 18function 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
32module.exports = validatorsSort 24export {
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
3const checkErrors = require('./utils').checkErrors
4const db = require('../../initializers/database') 1const db = require('../../initializers/database')
5const logger = require('../../helpers/logger') 2import { checkErrors } from './utils'
6 3import { logger } from '../../helpers'
7const validatorsUsers = {
8 usersAdd,
9 usersRemove,
10 usersUpdate,
11 usersVideoRating
12}
13 4
14function usersAdd (req, res, next) { 5function 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
35function usersRemove (req, res, next) { 26function 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
56function usersUpdate (req, res, next) { 47function 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
67function usersVideoRating (req, res, next) { 58function 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
88module.exports = validatorsUsers 79export {
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' 1import { inspect } from 'util'
2 2
3const util = require('util') 3import { logger } from '../../helpers'
4 4
5const logger = require('../../helpers/logger') 5function checkErrors (req, res, next, statusCode?) {
6
7const validatorsUtils = {
8 checkErrors
9}
10
11function 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
25module.exports = validatorsUtils 19export {
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
3const checkErrors = require('./utils').checkErrors
4const constants = require('../../initializers/constants')
5const customVideosValidators = require('../../helpers/custom-validators').videos
6const db = require('../../initializers/database') 1const db = require('../../initializers/database')
7const logger = require('../../helpers/logger') 2import { checkErrors } from './utils'
8 3import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
9const validatorsVideos = { 4import { 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
23function videosAdd (req, res, next) { 6function 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
53function videosUpdate (req, res, next) { 36function 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
81function videosGet (req, res, next) { 64function 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
91function videosRemove (req, res, next) { 74function 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
108function videosSearch (req, res, next) { 91function 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
118function videoAbuseReport (req, res, next) { 101function 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
129function videoRate (req, res, next) { 112function 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
140function videosBlacklist (req, res, next) { 123function 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
154module.exports = validatorsVideos 137export {
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