aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r--server/helpers/custom-validators/misc.ts27
-rw-r--r--server/helpers/custom-validators/users.ts26
-rw-r--r--server/helpers/custom-validators/videos.ts29
3 files changed, 49 insertions, 33 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index 3903884ea..8a270b777 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -1,3 +1,4 @@
1import 'multer'
1import * as validator from 'validator' 2import * as validator from 'validator'
2 3
3function exists (value: any) { 4function exists (value: any) {
@@ -28,6 +29,29 @@ function isBooleanValid (value: string) {
28 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) 29 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
29} 30}
30 31
32function isFileValid (
33 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
34 mimeTypeRegex: string,
35 field: string,
36 optional = false
37) {
38 // Should have files
39 if (!files) return optional
40 if (isArray(files)) return optional
41
42 // Should have a file
43 const fileArray = files[ field ]
44 if (!fileArray || fileArray.length === 0) {
45 return optional
46 }
47
48 // The file should exist
49 const file = fileArray[ 0 ]
50 if (!file || !file.originalname) return false
51
52 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
53}
54
31// --------------------------------------------------------------------------- 55// ---------------------------------------------------------------------------
32 56
33export { 57export {
@@ -37,5 +61,6 @@ export {
37 isUUIDValid, 61 isUUIDValid,
38 isIdOrUUIDValid, 62 isIdOrUUIDValid,
39 isDateValid, 63 isDateValid,
40 isBooleanValid 64 isBooleanValid,
65 isFileValid
41} 66}
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index 6ed60c1c4..e805313f8 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -1,9 +1,9 @@
1import * as validator from 'validator'
2import 'express-validator' 1import 'express-validator'
3 2import * as validator from 'validator'
4import { exists, isArray } from './misc'
5import { CONSTRAINTS_FIELDS } from '../../initializers'
6import { UserRole } from '../../../shared' 3import { UserRole } from '../../../shared'
4import { CONSTRAINTS_FIELDS } from '../../initializers'
5
6import { exists, isFileValid } from './misc'
7 7
8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
9 9
@@ -37,20 +37,12 @@ function isUserRoleValid (value: any) {
37 return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined 37 return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
38} 38}
39 39
40const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
41 .map(v => v.replace('.', ''))
42 .join('|')
43const avatarMimeTypesRegex = `image/(${avatarMimeTypes})`
40function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { 44function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
41 // Should have files 45 return isFileValid(files, avatarMimeTypesRegex, 'avatarfile')
42 if (!files) return false
43 if (isArray(files)) return false
44
45 // Should have videofile file
46 const avatarfile = files['avatarfile']
47 if (!avatarfile || avatarfile.length === 0) return false
48
49 // The file should exist
50 const file = avatarfile[0]
51 if (!file || !file.originalname) return false
52
53 return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype)
54} 46}
55 47
56// --------------------------------------------------------------------------- 48// ---------------------------------------------------------------------------
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 0e8a2aab2..8ef3a3c64 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -8,12 +8,12 @@ import {
8 CONSTRAINTS_FIELDS, 8 CONSTRAINTS_FIELDS,
9 VIDEO_CATEGORIES, 9 VIDEO_CATEGORIES,
10 VIDEO_LANGUAGES, 10 VIDEO_LANGUAGES,
11 VIDEO_LICENCES, 11 VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
12 VIDEO_PRIVACIES, 12 VIDEO_PRIVACIES,
13 VIDEO_RATE_TYPES 13 VIDEO_RATE_TYPES
14} from '../../initializers' 14} from '../../initializers'
15import { VideoModel } from '../../models/video/video' 15import { VideoModel } from '../../models/video/video'
16import { exists, isArray } from './misc' 16import { exists, isArray, isFileValid } from './misc'
17 17
18const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 18const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
19const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 19const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
@@ -68,20 +68,18 @@ function isVideoRatingTypeValid (value: string) {
68 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 68 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
69} 69}
70 70
71const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
72const videoFileTypesRegex = videoFileTypes.join('|')
71function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { 73function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
72 // Should have files 74 return isFileValid(files, videoFileTypesRegex, 'videofile')
73 if (!files) return false 75}
74 if (isArray(files)) return false
75
76 // Should have videofile file
77 const videofile = files['videofile']
78 if (!videofile || videofile.length === 0) return false
79
80 // The file should exist
81 const file = videofile[0]
82 if (!file || !file.originalname) return false
83 76
84 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) 77const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
78 .map(v => v.replace('.', ''))
79 .join('|')
80const videoImageTypesRegex = `image/(${videoImageTypes})`
81function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
82 return isFileValid(files, videoImageTypesRegex, field, true)
85} 83}
86 84
87function isVideoPrivacyValid (value: string) { 85function isVideoPrivacyValid (value: string) {
@@ -141,5 +139,6 @@ export {
141 isVideoPrivacyValid, 139 isVideoPrivacyValid,
142 isVideoFileResolutionValid, 140 isVideoFileResolutionValid,
143 isVideoFileSizeValid, 141 isVideoFileSizeValid,
144 isVideoExist 142 isVideoExist,
143 isVideoImage
145} 144}