app.use(bodyParser.urlencoded({ extended: false }))
// Validate some params for the API
app.use(expressValidator({
- customValidators: customValidators
+ customValidators: Object.assign({}, customValidators.misc, customValidators.users, customValidators.videos)
}))
// ----------- Views, routes and static files -----------
--- /dev/null
+'use strict'
+
+const miscValidators = require('./misc')
+const usersValidators = require('./users')
+const videosValidators = require('./videos')
+
+const validators = {
+ misc: miscValidators,
+ users: usersValidators,
+ videos: videosValidators
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = validators
--- /dev/null
+'use strict'
+
+const miscValidators = {
+ exists: exists,
+ isArray: isArray
+}
+
+function exists (value) {
+ return value !== undefined && value !== null
+}
+
+function isArray (value) {
+ return Array.isArray(value)
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = miscValidators
--- /dev/null
+'use strict'
+
+const validator = require('express-validator').validator
+
+const constants = require('../../initializers/constants')
+const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
+
+const usersValidators = {
+ isUserUsernameValid: isUserUsernameValid
+}
+
+function isUserUsernameValid (value) {
+ return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME)
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = usersValidators
const validator = require('express-validator').validator
-const constants = require('../initializers/constants')
-const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
+const constants = require('../../initializers/constants')
+const usersValidators = require('./users')
+const miscValidators = require('./misc')
+const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS
-const customValidators = {
- exists: exists,
+const videosValidators = {
isEachRemoteVideosValid: isEachRemoteVideosValid,
- isArray: isArray,
isVideoAuthorValid: isVideoAuthorValid,
isVideoDateValid: isVideoDateValid,
isVideoDescriptionValid: isVideoDescriptionValid,
isVideoThumbnail64Valid: isVideoThumbnail64Valid
}
-function exists (value) {
- return value !== undefined && value !== null
-}
-
function isEachRemoteVideosValid (requests) {
return requests.every(function (request) {
const video = request.data
})
}
-function isArray (value) {
- return Array.isArray(value)
-}
-
-function isRequestTypeAddValid (value) {
- return value === 'add'
-}
-
-function isRequestTypeRemoveValid (value) {
- return value === 'remove'
-}
-
function isVideoAuthorValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
+ return usersValidators.isUserUsernameValid(usersValidators)
}
function isVideoDateValid (value) {
}
function isVideoTagsValid (tags) {
- return isArray(tags) &&
+ return miscValidators.isArray(tags) &&
validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
tags.every(function (tag) {
return validator.isAlphanumeric(tag) &&
// ---------------------------------------------------------------------------
-module.exports = customValidators
+module.exports = videosValidators
// ---------------------------------------------------------------------------
+
+function isRequestTypeAddValid (value) {
+ return value === 'add'
+}
+
+function isRequestTypeRemoveValid (value) {
+ return value === 'remove'
+}
// API version of our pod
const API_VERSION = 'v1'
+const CONSTRAINTS_FIELDS = {
+ USERS: {
+ USERNAME: { min: 3, max: 20 }, // Length
+ PASSWORD: { min: 6, max: 255 } // Length
+ },
+ VIDEOS: {
+ NAME: { min: 3, max: 50 }, // Length
+ DESCRIPTION: { min: 3, max: 250 }, // Length
+ MAGNET_URI: { min: 10 }, // Length
+ DURATION: { min: 1, max: 7200 }, // Number
+ TAGS: { min: 1, max: 3 }, // Number of total tags
+ TAG: { min: 2, max: 10 }, // Length
+ THUMBNAIL: { min: 2, max: 30 },
+ THUMBNAIL64: { min: 0, max: 20000 } // Bytes
+ }
+}
+
// Score a pod has when we create it as a friend
const FRIEND_SCORE = {
BASE: 100,
// Path for access to thumbnails with express router
const THUMBNAILS_STATIC_PATH = '/static/thumbnails'
-const VIDEOS_CONSTRAINTS_FIELDS = {
- NAME: { min: 3, max: 50 }, // Length
- DESCRIPTION: { min: 3, max: 250 }, // Length
- MAGNET_URI: { min: 10 }, // Length
- DURATION: { min: 1, max: 7200 }, // Number
- AUTHOR: { min: 3, max: 20 }, // Length
- TAGS: { min: 1, max: 3 }, // Number of total tags
- TAG: { min: 2, max: 10 }, // Length
- THUMBNAIL: { min: 2, max: 30 },
- THUMBNAIL64: { min: 0, max: 20000 } // Bytes
-}
-
// Special constants for a test instance
if (isTestInstance() === true) {
FRIEND_SCORE.BASE = 20
INTERVAL = 10000
- VIDEOS_CONSTRAINTS_FIELDS.DURATION.max = 14
+ CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
}
// ---------------------------------------------------------------------------
module.exports = {
API_VERSION: API_VERSION,
+ CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
FRIEND_SCORE: FRIEND_SCORE,
INTERVAL: INTERVAL,
OAUTH_LIFETIME: OAUTH_LIFETIME,
SEEDS_IN_PARALLEL: SEEDS_IN_PARALLEL,
SORTABLE_COLUMNS: SORTABLE_COLUMNS,
THUMBNAILS_SIZE: THUMBNAILS_SIZE,
- THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH,
- VIDEOS_CONSTRAINTS_FIELDS: VIDEOS_CONSTRAINTS_FIELDS
+ THUMBNAILS_STATIC_PATH: THUMBNAILS_STATIC_PATH
}
// ---------------------------------------------------------------------------
const checkErrors = require('./utils').checkErrors
const constants = require('../../initializers/constants')
-const customValidators = require('../../helpers/custom-validators')
+const customVideosValidators = require('../../helpers/custom-validators').videos
const logger = require('../../helpers/logger')
const Video = mongoose.model('Video')
return res.status(400).send('Cannot retrieve metadata of the file.')
}
- if (!customValidators.isVideoDurationValid(duration)) {
- return res.status(400).send('Duration of the video file is too big (max: ' + constants.VIDEOS_CONSTRAINTS_FIELDS.DURATION.max + 's).')
+ if (!customVideosValidators.isVideoDurationValid(duration)) {
+ return res.status(400).send('Duration of the video file is too big (max: ' + constants.CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).')
}
videoFile.duration = duration
const mongoose = require('mongoose')
const constants = require('../initializers/constants')
-const customValidators = require('../helpers/custom-validators')
+const customVideosValidators = require('../helpers/custom-validators').videos
const logger = require('../helpers/logger')
const utils = require('../helpers/utils')
const webtorrent = require('../lib/webtorrent')
}
})
-VideoSchema.path('name').validate(customValidators.isVideoNameValid)
-VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid)
-VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid)
-VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid)
-VideoSchema.path('author').validate(customValidators.isVideoAuthorValid)
-VideoSchema.path('duration').validate(customValidators.isVideoDurationValid)
+VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
+VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
+VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid)
+VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
+VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
+VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
// The tumbnail can be the path or the data in base 64
// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
VideoSchema.path('thumbnail').validate(function (value) {
- return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value)
+ return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
})
-VideoSchema.path('tags').validate(customValidators.isVideoTagsValid)
+VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
VideoSchema.methods = {
isOwned: isOwned,