aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-31 20:58:43 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-31 20:58:43 +0200
commite4c556196d7b31111f17596840d2e1d60caa7dcb (patch)
tree41be84f002600aa0153ac09cc5d79fdd90d126e3
parente62f6ef741c8d14817e321c554796ad64ea7ae1b (diff)
downloadPeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.gz
PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.tar.zst
PeerTube-e4c556196d7b31111f17596840d2e1d60caa7dcb.zip
Server: reorganize express validators
-rw-r--r--server.js2
-rw-r--r--server/helpers/custom-validators/index.js15
-rw-r--r--server/helpers/custom-validators/misc.js18
-rw-r--r--server/helpers/custom-validators/users.js18
-rw-r--r--server/helpers/custom-validators/videos.js (renamed from server/helpers/custom-validators.js)40
-rw-r--r--server/initializers/constants.js35
-rw-r--r--server/middlewares/validators/videos.js6
-rw-r--r--server/models/video.js18
8 files changed, 100 insertions, 52 deletions
diff --git a/server.js b/server.js
index b2eeeff70..d38c5830f 100644
--- a/server.js
+++ b/server.js
@@ -53,7 +53,7 @@ app.use(bodyParser.json())
53app.use(bodyParser.urlencoded({ extended: false })) 53app.use(bodyParser.urlencoded({ extended: false }))
54// Validate some params for the API 54// Validate some params for the API
55app.use(expressValidator({ 55app.use(expressValidator({
56 customValidators: customValidators 56 customValidators: Object.assign({}, customValidators.misc, customValidators.users, customValidators.videos)
57})) 57}))
58 58
59// ----------- Views, routes and static files ----------- 59// ----------- Views, routes and static files -----------
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
3const miscValidators = require('./misc')
4const usersValidators = require('./users')
5const videosValidators = require('./videos')
6
7const validators = {
8 misc: miscValidators,
9 users: usersValidators,
10 videos: videosValidators
11}
12
13// ---------------------------------------------------------------------------
14
15module.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
3const miscValidators = {
4 exists: exists,
5 isArray: isArray
6}
7
8function exists (value) {
9 return value !== undefined && value !== null
10}
11
12function isArray (value) {
13 return Array.isArray(value)
14}
15
16// ---------------------------------------------------------------------------
17
18module.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
3const validator = require('express-validator').validator
4
5const constants = require('../../initializers/constants')
6const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
7
8const usersValidators = {
9 isUserUsernameValid: isUserUsernameValid
10}
11
12function isUserUsernameValid (value) {
13 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME)
14}
15
16// ---------------------------------------------------------------------------
17
18module.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
3const validator = require('express-validator').validator 3const validator = require('express-validator').validator
4 4
5const constants = require('../initializers/constants') 5const constants = require('../../initializers/constants')
6const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS 6const usersValidators = require('./users')
7const miscValidators = require('./misc')
8const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
7 9
8const customValidators = { 10const 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
24function exists (value) {
25 return value !== undefined && value !== null
26}
27
28function isEachRemoteVideosValid (requests) { 24function 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
51function isArray (value) {
52 return Array.isArray(value)
53}
54
55function isRequestTypeAddValid (value) {
56 return value === 'add'
57}
58
59function isRequestTypeRemoveValid (value) {
60 return value === 'remove'
61}
62
63function isVideoAuthorValid (value) { 47function isVideoAuthorValid (value) {
64 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) 48 return usersValidators.isUserUsernameValid(usersValidators)
65} 49}
66 50
67function isVideoDateValid (value) { 51function isVideoDateValid (value) {
@@ -90,7 +74,7 @@ function isVideoPodUrlValid (value) {
90} 74}
91 75
92function isVideoTagsValid (tags) { 76function 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
112module.exports = customValidators 96module.exports = videosValidators
113 97
114// --------------------------------------------------------------------------- 98// ---------------------------------------------------------------------------
99
100function isRequestTypeAddValid (value) {
101 return value === 'add'
102}
103
104function 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
4const API_VERSION = 'v1' 4const API_VERSION = 'v1'
5 5
6const 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
7const FRIEND_SCORE = { 24const 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
56const THUMBNAILS_STATIC_PATH = '/static/thumbnails' 73const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
57 74
58const 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
71if (isTestInstance() === true) { 76if (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
79module.exports = { 84module.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
5const checkErrors = require('./utils').checkErrors 5const checkErrors = require('./utils').checkErrors
6const constants = require('../../initializers/constants') 6const constants = require('../../initializers/constants')
7const customValidators = require('../../helpers/custom-validators') 7const customVideosValidators = require('../../helpers/custom-validators').videos
8const logger = require('../../helpers/logger') 8const logger = require('../../helpers/logger')
9 9
10const Video = mongoose.model('Video') 10const 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')
9const mongoose = require('mongoose') 9const mongoose = require('mongoose')
10 10
11const constants = require('../initializers/constants') 11const constants = require('../initializers/constants')
12const customValidators = require('../helpers/custom-validators') 12const customVideosValidators = require('../helpers/custom-validators').videos
13const logger = require('../helpers/logger') 13const logger = require('../helpers/logger')
14const utils = require('../helpers/utils') 14const utils = require('../helpers/utils')
15const webtorrent = require('../lib/webtorrent') 15const webtorrent = require('../lib/webtorrent')
@@ -39,18 +39,18 @@ const VideoSchema = mongoose.Schema({
39 } 39 }
40}) 40})
41 41
42VideoSchema.path('name').validate(customValidators.isVideoNameValid) 42VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
43VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid) 43VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
44VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid) 44VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
45VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid) 45VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
46VideoSchema.path('author').validate(customValidators.isVideoAuthorValid) 46VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
47VideoSchema.path('duration').validate(customValidators.isVideoDurationValid) 47VideoSchema.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
50VideoSchema.path('thumbnail').validate(function (value) { 50VideoSchema.path('thumbnail').validate(function (value) {
51 return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value) 51 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
52}) 52})
53VideoSchema.path('tags').validate(customValidators.isVideoTagsValid) 53VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
54 54
55VideoSchema.methods = { 55VideoSchema.methods = {
56 isOwned: isOwned, 56 isOwned: isOwned,