From f6f7dfee01e8b7ddf62d2549b8f381d0b12b590e Mon Sep 17 00:00:00 2001
From: Chocobozzz <florian.bigard@gmail.com>
Date: Fri, 10 Feb 2017 11:27:14 +0100
Subject: [PATCH] Server: update express-validator

---
 package.json                               |  2 +-
 server/helpers/custom-validators/videos.js | 18 +++++++++++++++++-
 server/middlewares/validators/videos.js    |  4 +---
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/package.json b/package.json
index 0d650ddeb6..4197f919a4 100644
--- a/package.json
+++ b/package.json
@@ -51,7 +51,7 @@
     "dezalgo": "^1.0.3",
     "express": "^4.12.4",
     "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server",
-    "express-validator": "^2.11.0",
+    "express-validator": "^3.1.0",
     "fluent-ffmpeg": "^2.1.0",
     "js-yaml": "^3.5.4",
     "lodash": "^4.11.1",
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js
index 7f727854df..e2d2c8e6d5 100644
--- a/server/helpers/custom-validators/videos.js
+++ b/server/helpers/custom-validators/videos.js
@@ -21,7 +21,8 @@ const videosValidators = {
   isVideoExtnameValid,
   isVideoRemoteIdValid,
   isVideoAbuseReasonValid,
-  isVideoAbuseReporterUsernameValid
+  isVideoAbuseReporterUsernameValid,
+  isVideoFile
 }
 
 function isVideoAuthorValid (value) {
@@ -81,6 +82,21 @@ function isVideoAbuseReporterUsernameValid (value) {
   return usersValidators.isUserUsernameValid(value)
 }
 
+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
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js
index 4fe6dcd8b3..5c3f3ecf3e 100644
--- a/server/middlewares/validators/videos.js
+++ b/server/middlewares/validators/videos.js
@@ -17,9 +17,7 @@ const validatorsVideos = {
 }
 
 function videosAdd (req, res, next) {
-  req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty()
-  // TODO: move to constants and function
-  req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i)
+  req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files)
   req.checkBody('name', 'Should have a valid name').isVideoNameValid()
   req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid()
   req.checkBody('tags', 'Should have correct tags').isVideoTagsValid()
-- 
GitLab