]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/videos.js
Server: add licence video attribute
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.js
index 7f727854dfc8e99c2eea8cea0ffa23d0c52cf3cb..8495e9665965d3b9a759c6d2b09f1829a43e38ad 100644 (file)
@@ -1,16 +1,20 @@
 '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 = {
   isVideoAuthorValid,
   isVideoDateValid,
+  isVideoCategoryValid,
+  isVideoLicenceValid,
   isVideoDescriptionValid,
   isVideoDurationValid,
   isVideoInfoHashValid,
@@ -21,7 +25,13 @@ const videosValidators = {
   isVideoExtnameValid,
   isVideoRemoteIdValid,
   isVideoAbuseReasonValid,
-  isVideoAbuseReporterUsernameValid
+  isVideoAbuseReporterUsernameValid,
+  isVideoFile,
+  isVideoViewsValid,
+  isVideoLikesValid,
+  isVideoRatingTypeValid,
+  isVideoDislikesValid,
+  isVideoEventCountValid
 }
 
 function isVideoAuthorValid (value) {
@@ -32,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)
 }
@@ -56,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)
          })
 }
 
@@ -81,6 +98,41 @@ function isVideoAbuseReporterUsernameValid (value) {
   return usersValidators.isUserUsernameValid(value)
 }
 
+function isVideoViewsValid (value) {
+  return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
+}
+
+function isVideoLikesValid (value) {
+  return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
+}
+
+function isVideoDislikesValid (value) {
+  return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
+}
+
+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