]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/videos.js
Server: implement video views
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.js
index 7f727854dfc8e99c2eea8cea0ffa23d0c52cf3cb..c5a1f3cb5684d24af5932671497c2e528b9d10ae 100644 (file)
@@ -7,6 +7,7 @@ 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,
@@ -21,7 +22,12 @@ const videosValidators = {
   isVideoExtnameValid,
   isVideoRemoteIdValid,
   isVideoAbuseReasonValid,
-  isVideoAbuseReporterUsernameValid
+  isVideoAbuseReporterUsernameValid,
+  isVideoFile,
+  isVideoViewsValid,
+  isVideoLikesValid,
+  isVideoDislikesValid,
+  isVideoEventCountValid
 }
 
 function isVideoAuthorValid (value) {
@@ -81,6 +87,37 @@ 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 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