diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-31 20:58:43 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-31 20:58:43 +0200 |
commit | e4c556196d7b31111f17596840d2e1d60caa7dcb (patch) | |
tree | 41be84f002600aa0153ac09cc5d79fdd90d126e3 /server | |
parent | e62f6ef741c8d14817e321c554796ad64ea7ae1b (diff) | |
download | PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.gz PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.zst PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.zip |
Server: reorganize express validators
Diffstat (limited to 'server')
-rw-r--r-- | server/helpers/custom-validators/index.js | 15 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.js | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/users.js | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js (renamed from server/helpers/custom-validators.js) | 40 | ||||
-rw-r--r-- | server/initializers/constants.js | 35 | ||||
-rw-r--r-- | server/middlewares/validators/videos.js | 6 | ||||
-rw-r--r-- | server/models/video.js | 18 |
7 files changed, 99 insertions, 51 deletions
diff --git a/server/helpers/custom-validators/index.js b/server/helpers/custom-validators/index.js new file mode 100644 index 000000000..ab3066822 --- /dev/null +++ b/server/helpers/custom-validators/index.js | |||
@@ -0,0 +1,15 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const miscValidators = require('./misc') | ||
4 | const usersValidators = require('./users') | ||
5 | const videosValidators = require('./videos') | ||
6 | |||
7 | const validators = { | ||
8 | misc: miscValidators, | ||
9 | users: usersValidators, | ||
10 | videos: videosValidators | ||
11 | } | ||
12 | |||
13 | // --------------------------------------------------------------------------- | ||
14 | |||
15 | module.exports = validators | ||
diff --git a/server/helpers/custom-validators/misc.js b/server/helpers/custom-validators/misc.js new file mode 100644 index 000000000..782ae3dee --- /dev/null +++ b/server/helpers/custom-validators/misc.js | |||
@@ -0,0 +1,18 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const miscValidators = { | ||
4 | exists: exists, | ||
5 | isArray: isArray | ||
6 | } | ||
7 | |||
8 | function exists (value) { | ||
9 | return value !== undefined && value !== null | ||
10 | } | ||
11 | |||
12 | function isArray (value) { | ||
13 | return Array.isArray(value) | ||
14 | } | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.exports = miscValidators | ||
diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js new file mode 100644 index 000000000..41e00d046 --- /dev/null +++ b/server/helpers/custom-validators/users.js | |||
@@ -0,0 +1,18 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const validator = require('express-validator').validator | ||
4 | |||
5 | const constants = require('../../initializers/constants') | ||
6 | const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS | ||
7 | |||
8 | const usersValidators = { | ||
9 | isUserUsernameValid: isUserUsernameValid | ||
10 | } | ||
11 | |||
12 | function isUserUsernameValid (value) { | ||
13 | return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME) | ||
14 | } | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.exports = usersValidators | ||
diff --git a/server/helpers/custom-validators.js b/server/helpers/custom-validators/videos.js index b666644c0..39a19cbd7 100644 --- a/server/helpers/custom-validators.js +++ b/server/helpers/custom-validators/videos.js | |||
@@ -2,13 +2,13 @@ | |||
2 | 2 | ||
3 | const validator = require('express-validator').validator | 3 | const validator = require('express-validator').validator |
4 | 4 | ||
5 | const constants = require('../initializers/constants') | 5 | const constants = require('../../initializers/constants') |
6 | const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS | 6 | const usersValidators = require('./users') |
7 | const miscValidators = require('./misc') | ||
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | ||
7 | 9 | ||
8 | const customValidators = { | 10 | const videosValidators = { |
9 | exists: exists, | ||
10 | isEachRemoteVideosValid: isEachRemoteVideosValid, | 11 | isEachRemoteVideosValid: isEachRemoteVideosValid, |
11 | isArray: isArray, | ||
12 | isVideoAuthorValid: isVideoAuthorValid, | 12 | isVideoAuthorValid: isVideoAuthorValid, |
13 | isVideoDateValid: isVideoDateValid, | 13 | isVideoDateValid: isVideoDateValid, |
14 | isVideoDescriptionValid: isVideoDescriptionValid, | 14 | isVideoDescriptionValid: isVideoDescriptionValid, |
@@ -21,10 +21,6 @@ const customValidators = { | |||
21 | isVideoThumbnail64Valid: isVideoThumbnail64Valid | 21 | isVideoThumbnail64Valid: isVideoThumbnail64Valid |
22 | } | 22 | } |
23 | 23 | ||
24 | function exists (value) { | ||
25 | return value !== undefined && value !== null | ||
26 | } | ||
27 | |||
28 | function isEachRemoteVideosValid (requests) { | 24 | function isEachRemoteVideosValid (requests) { |
29 | return requests.every(function (request) { | 25 | return requests.every(function (request) { |
30 | const video = request.data | 26 | const video = request.data |
@@ -48,20 +44,8 @@ function isEachRemoteVideosValid (requests) { | |||
48 | }) | 44 | }) |
49 | } | 45 | } |
50 | 46 | ||
51 | function isArray (value) { | ||
52 | return Array.isArray(value) | ||
53 | } | ||
54 | |||
55 | function isRequestTypeAddValid (value) { | ||
56 | return value === 'add' | ||
57 | } | ||
58 | |||
59 | function isRequestTypeRemoveValid (value) { | ||
60 | return value === 'remove' | ||
61 | } | ||
62 | |||
63 | function isVideoAuthorValid (value) { | 47 | function isVideoAuthorValid (value) { |
64 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) | 48 | return usersValidators.isUserUsernameValid(usersValidators) |
65 | } | 49 | } |
66 | 50 | ||
67 | function isVideoDateValid (value) { | 51 | function isVideoDateValid (value) { |
@@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) { | |||
90 | } | 74 | } |
91 | 75 | ||
92 | function isVideoTagsValid (tags) { | 76 | function isVideoTagsValid (tags) { |
93 | return isArray(tags) && | 77 | return miscValidators.isArray(tags) && |
94 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | 78 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && |
95 | tags.every(function (tag) { | 79 | tags.every(function (tag) { |
96 | return validator.isAlphanumeric(tag) && | 80 | return validator.isAlphanumeric(tag) && |
@@ -109,6 +93,14 @@ function isVideoThumbnail64Valid (value) { | |||
109 | 93 | ||
110 | // --------------------------------------------------------------------------- | 94 | // --------------------------------------------------------------------------- |
111 | 95 | ||
112 | module.exports = customValidators | 96 | module.exports = videosValidators |
113 | 97 | ||
114 | // --------------------------------------------------------------------------- | 98 | // --------------------------------------------------------------------------- |
99 | |||
100 | function isRequestTypeAddValid (value) { | ||
101 | return value === 'add' | ||
102 | } | ||
103 | |||
104 | function isRequestTypeRemoveValid (value) { | ||
105 | return value === 'remove' | ||
106 | } | ||
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 467816f2c..5f4aeccc6 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -3,6 +3,23 @@ | |||
3 | // API version of our pod | 3 | // API version of our pod |
4 | const API_VERSION = 'v1' | 4 | const API_VERSION = 'v1' |
5 | 5 | ||
6 | const CONSTRAINTS_FIELDS = { | ||
7 | USERS: { | ||
8 | USERNAME: { min: 3, max: 20 }, // Length | ||
9 | PASSWORD: { min: 6, max: 255 } // Length | ||
10 | }, | ||
11 | VIDEOS: { | ||
12 | NAME: { min: 3, max: 50 }, // Length | ||
13 | DESCRIPTION: { min: 3, max: 250 }, // Length | ||
14 | MAGNET_URI: { min: 10 }, // Length | ||
15 | DURATION: { min: 1, max: 7200 }, // Number | ||
16 | TAGS: { min: 1, max: 3 }, // Number of total tags | ||
17 | TAG: { min: 2, max: 10 }, // Length | ||
18 | THUMBNAIL: { min: 2, max: 30 }, | ||
19 | THUMBNAIL64: { min: 0, max: 20000 } // Bytes | ||
20 | } | ||
21 | } | ||
22 | |||
6 | // Score a pod has when we create it as a friend | 23 | // Score a pod has when we create it as a friend |
7 | const FRIEND_SCORE = { | 24 | const FRIEND_SCORE = { |
8 | BASE: 100, | 25 | BASE: 100, |
@@ -55,29 +72,18 @@ const THUMBNAILS_SIZE = '200x110' | |||
55 | // Path for access to thumbnails with express router | 72 | // Path for access to thumbnails with express router |
56 | const THUMBNAILS_STATIC_PATH = '/static/thumbnails' | 73 | const THUMBNAILS_STATIC_PATH = '/static/thumbnails' |
57 | 74 | ||
58 | const VIDEOS_CONSTRAINTS_FIELDS = { | ||
59 | NAME: { min: 3, max: 50 }, // Length | ||
60 | DESCRIPTION: { min: 3, max: 250 }, // Length | ||
61 | MAGNET_URI: { min: 10 }, // Length | ||
62 | DURATION: { min: 1, max: 7200 }, // Number | ||
63 | AUTHOR: { min: 3, max: 20 }, // Length | ||
64 | TAGS: { min: 1, max: 3 }, // Number of total tags | ||
65 | TAG: { min: 2, max: 10 }, // Length | ||
66 | THUMBNAIL: { min: 2, max: 30 }, | ||
67 | THUMBNAIL64: { min: 0, max: 20000 } // Bytes | ||
68 | } | ||
69 | |||
70 | // Special constants for a test instance | 75 | // Special constants for a test instance |
71 | if (isTestInstance() === true) { | 76 | if (isTestInstance() === true) { |
72 | FRIEND_SCORE.BASE = 20 | 77 | FRIEND_SCORE.BASE = 20 |
73 | INTERVAL = 10000 | 78 | INTERVAL = 10000 |
74 | VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14 | 79 | CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14 |
75 | } | 80 | } |
76 | 81 | ||
77 | // --------------------------------------------------------------------------- | 82 | // --------------------------------------------------------------------------- |
78 | 83 | ||
79 | module.exports = { | 84 | module.exports = { |
80 | API_VERSION: API_VERSION, | 85 | API_VERSION: API_VERSION, |
86 | CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, | ||
81 | FRIEND_SCORE: FRIEND_SCORE, | 87 | FRIEND_SCORE: FRIEND_SCORE, |
82 | INTERVAL: INTERVAL, | 88 | INTERVAL: INTERVAL, |
83 | OAUTH_LIFETIME: OAUTH_LIFETIME, | 89 | OAUTH_LIFETIME: OAUTH_LIFETIME, |
@@ -90,8 +96,7 @@ module.exports = { | |||
90 | SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, | 96 | SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL, |
91 | SORTABLE_COLUMNS: SORTABLE_COLUMNS, | 97 | SORTABLE_COLUMNS: SORTABLE_COLUMNS, |
92 | THUMBNAILS_SIZE: THUMBNAILS_SIZE, | 98 | THUMBNAILS_SIZE: THUMBNAILS_SIZE, |
93 | THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH, | 99 | THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH |
94 | VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS | ||
95 | } | 100 | } |
96 | 101 | ||
97 | // --------------------------------------------------------------------------- | 102 | // --------------------------------------------------------------------------- |
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js index 3e2af06fb..422f3642f 100644 --- a/server/middlewares/validators/videos.js +++ b/server/middlewares/validators/videos.js | |||
@@ -4,7 +4,7 @@ const mongoose = require('mongoose') | |||
4 | 4 | ||
5 | const checkErrors = require('./utils').checkErrors | 5 | const checkErrors = require('./utils').checkErrors |
6 | const constants = require('../../initializers/constants') | 6 | const constants = require('../../initializers/constants') |
7 | const customValidators = require('../../helpers/custom-validators') | 7 | const customVideosValidators = require('../../helpers/custom-validators').videos |
8 | const logger = require('../../helpers/logger') | 8 | const logger = require('../../helpers/logger') |
9 | 9 | ||
10 | const Video = mongoose.model('Video') | 10 | const Video = mongoose.model('Video') |
@@ -33,8 +33,8 @@ function videosAdd (req, res, next) { | |||
33 | return res.status(400).send('Cannot retrieve metadata of the file.') | 33 | return res.status(400).send('Cannot retrieve metadata of the file.') |
34 | } | 34 | } |
35 | 35 | ||
36 | if (!customValidators.isVideoDurationValid(duration)) { | 36 | if (!customVideosValidators.isVideoDurationValid(duration)) { |
37 | return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).') | 37 | return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') |
38 | } | 38 | } |
39 | 39 | ||
40 | videoFile.duration = duration | 40 | videoFile.duration = duration |
diff --git a/server/models/video.js b/server/models/video.js index 396aa505d..acb8353c2 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -9,7 +9,7 @@ const pathUtils = require('path') | |||
9 | const mongoose = require('mongoose') | 9 | const mongoose = require('mongoose') |
10 | 10 | ||
11 | const constants = require('../initializers/constants') | 11 | const constants = require('../initializers/constants') |
12 | const customValidators = require('../helpers/custom-validators') | 12 | const customVideosValidators = require('../helpers/custom-validators').videos |
13 | const logger = require('../helpers/logger') | 13 | const logger = require('../helpers/logger') |
14 | const utils = require('../helpers/utils') | 14 | const utils = require('../helpers/utils') |
15 | const webtorrent = require('../lib/webtorrent') | 15 | const webtorrent = require('../lib/webtorrent') |
@@ -39,18 +39,18 @@ const VideoSchema = mongoose.Schema({ | |||
39 | } | 39 | } |
40 | }) | 40 | }) |
41 | 41 | ||
42 | VideoSchema.path('name').validate(customValidators.isVideoNameValid) | 42 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) |
43 | VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid) | 43 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) |
44 | VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid) | 44 | VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid) |
45 | VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid) | 45 | VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid) |
46 | VideoSchema.path('author').validate(customValidators.isVideoAuthorValid) | 46 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) |
47 | VideoSchema.path('duration').validate(customValidators.isVideoDurationValid) | 47 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) |
48 | // The tumbnail can be the path or the data in base 64 | 48 | // The tumbnail can be the path or the data in base 64 |
49 | // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename | 49 | // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename |
50 | VideoSchema.path('thumbnail').validate(function (value) { | 50 | VideoSchema.path('thumbnail').validate(function (value) { |
51 | return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value) | 51 | return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value) |
52 | }) | 52 | }) |
53 | VideoSchema.path('tags').validate(customValidators.isVideoTagsValid) | 53 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) |
54 | 54 | ||
55 | VideoSchema.methods = { | 55 | VideoSchema.methods = { |
56 | isOwned: isOwned, | 56 | isOwned: isOwned, |