X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideos.js;h=8495e9665965d3b9a759c6d2b09f1829a43e38ad;hb=6f0c39e2de400685b7baf8340b9e132f2659365a;hp=8448386d94d759958d2ad460c38d3b7a9b21958f;hpb=79066fdf33f79d2d41394f10881e2c226ca26b49;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index 8448386d9..8495e9665 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js @@ -1,61 +1,37 @@ 'use strict' const validator = require('express-validator').validator +const values = require('lodash/values') const constants = require('../../initializers/constants') const usersValidators = require('./users') const miscValidators = require('./misc') const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS +const VIDEO_ABUSES_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_ABUSES +const VIDEO_EVENTS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEO_EVENTS const videosValidators = { - isEachRemoteVideosValid, isVideoAuthorValid, isVideoDateValid, + isVideoCategoryValid, + isVideoLicenceValid, isVideoDescriptionValid, isVideoDurationValid, isVideoInfoHashValid, isVideoNameValid, isVideoTagsValid, isVideoThumbnailValid, - isVideoThumbnailDataValid -} - -function isEachRemoteVideosValid (requests) { - return miscValidators.isArray(requests) && - requests.every(function (request) { - const video = request.data - return ( - isRequestTypeAddValid(request.type) && - isVideoAuthorValid(video.author) && - isVideoDateValid(video.createdAt) && - isVideoDateValid(video.updatedAt) && - isVideoDescriptionValid(video.description) && - isVideoDurationValid(video.duration) && - isVideoInfoHashValid(video.infoHash) && - isVideoNameValid(video.name) && - isVideoTagsValid(video.tags) && - isVideoThumbnailDataValid(video.thumbnailData) && - isVideoRemoteIdValid(video.remoteId) && - isVideoExtnameValid(video.extname) - ) || - ( - isRequestTypeUpdateValid(request.type) && - isVideoDateValid(video.createdAt) && - isVideoDateValid(video.updatedAt) && - isVideoDescriptionValid(video.description) && - isVideoDurationValid(video.duration) && - isVideoInfoHashValid(video.infoHash) && - isVideoNameValid(video.name) && - isVideoTagsValid(video.tags) && - isVideoRemoteIdValid(video.remoteId) && - isVideoExtnameValid(video.extname) - ) || - ( - isRequestTypeRemoveValid(request.type) && - isVideoNameValid(video.name) && - isVideoRemoteIdValid(video.remoteId) - ) - }) + isVideoThumbnailDataValid, + isVideoExtnameValid, + isVideoRemoteIdValid, + isVideoAbuseReasonValid, + isVideoAbuseReporterUsernameValid, + isVideoFile, + isVideoViewsValid, + isVideoLikesValid, + isVideoRatingTypeValid, + isVideoDislikesValid, + isVideoEventCountValid } function isVideoAuthorValid (value) { @@ -66,6 +42,14 @@ function isVideoDateValid (value) { return validator.isDate(value) } +function isVideoCategoryValid (value) { + return constants.VIDEO_CATEGORIES[value] !== undefined +} + +function isVideoLicenceValid (value) { + return constants.VIDEO_LICENCES[value] !== undefined +} + function isVideoDescriptionValid (value) { return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) } @@ -90,8 +74,7 @@ function isVideoTagsValid (tags) { return miscValidators.isArray(tags) && validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && tags.every(function (tag) { - return validator.isAlphanumeric(tag) && - validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) + return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) }) } @@ -107,20 +90,49 @@ function isVideoRemoteIdValid (value) { return validator.isUUID(value, 4) } -// --------------------------------------------------------------------------- +function isVideoAbuseReasonValid (value) { + return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) +} -module.exports = videosValidators +function isVideoAbuseReporterUsernameValid (value) { + return usersValidators.isUserUsernameValid(value) +} -// --------------------------------------------------------------------------- +function isVideoViewsValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) +} -function isRequestTypeAddValid (value) { - return value === 'add' +function isVideoLikesValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) } -function isRequestTypeUpdateValid (value) { - return value === 'update' +function isVideoDislikesValid (value) { + return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) } -function isRequestTypeRemoveValid (value) { - return value === 'remove' +function isVideoEventCountValid (value) { + return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) } + +function isVideoRatingTypeValid (value) { + return values(constants.VIDEO_RATE_TYPES).indexOf(value) !== -1 +} + +function isVideoFile (value, files) { + // Should have files + if (!files) return false + + // Should have videofile file + const videofile = files.videofile + if (!videofile || videofile.length === 0) return false + + // The file should exist + const file = videofile[0] + if (!file || !file.originalname) return false + + return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) +} + +// --------------------------------------------------------------------------- + +module.exports = videosValidators