]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/misc.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
1 import 'multer'
2 import validator from 'validator'
3 import { sep } from 'path'
4
5 function exists (value: any) {
6 return value !== undefined && value !== null
7 }
8
9 function isSafePath (p: string) {
10 return exists(p) &&
11 (p + '').split(sep).every(part => {
12 return [ '..' ].includes(part) === false
13 })
14 }
15
16 function isArray (value: any) {
17 return Array.isArray(value)
18 }
19
20 function isNotEmptyIntArray (value: any) {
21 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
22 }
23
24 function isArrayOf (value: any, validator: (value: any) => boolean) {
25 return isArray(value) && value.every(v => validator(v))
26 }
27
28 function isDateValid (value: string) {
29 return exists(value) && validator.isISO8601(value)
30 }
31
32 function isIdValid (value: string) {
33 return exists(value) && validator.isInt('' + value)
34 }
35
36 function isUUIDValid (value: string) {
37 return exists(value) && validator.isUUID('' + value, 4)
38 }
39
40 function isIdOrUUIDValid (value: string) {
41 return isIdValid(value) || isUUIDValid(value)
42 }
43
44 function isBooleanValid (value: any) {
45 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
46 }
47
48 function isIntOrNull (value: any) {
49 return value === null || validator.isInt('' + value)
50 }
51
52 function toIntOrNull (value: string) {
53 const v = toValueOrNull(value)
54
55 if (v === null || v === undefined) return v
56 if (typeof v === 'number') return v
57
58 return validator.toInt('' + v)
59 }
60
61 function toBooleanOrNull (value: any) {
62 const v = toValueOrNull(value)
63
64 if (v === null || v === undefined) return v
65 if (typeof v === 'boolean') return v
66
67 return validator.toBoolean('' + v)
68 }
69
70 function toValueOrNull (value: string) {
71 if (value === 'null') return null
72
73 return value
74 }
75
76 function toArray (value: any) {
77 if (value && isArray(value) === false) return [ value ]
78
79 return value
80 }
81
82 function toIntArray (value: any) {
83 if (!value) return []
84 if (isArray(value) === false) return [ validator.toInt(value) ]
85
86 return value.map(v => validator.toInt(v))
87 }
88
89 function isFileFieldValid (
90 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
91 field: string,
92 optional = false
93 ) {
94 // Should have files
95 if (!files) return optional
96 if (isArray(files)) return optional
97
98 // Should have a file
99 const fileArray = files[field]
100 if (!fileArray || fileArray.length === 0) {
101 return optional
102 }
103
104 // The file should exist
105 const file = fileArray[0]
106 if (!file || !file.originalname) return false
107 return file
108 }
109
110 function isFileMimeTypeValid (
111 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
112 mimeTypeRegex: string,
113 field: string,
114 optional = false
115 ) {
116 // Should have files
117 if (!files) return optional
118 if (isArray(files)) return optional
119
120 // Should have a file
121 const fileArray = files[field]
122 if (!fileArray || fileArray.length === 0) {
123 return optional
124 }
125
126 // The file should exist
127 const file = fileArray[0]
128 if (!file || !file.originalname) return false
129
130 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
131 }
132
133 function isFileValid (
134 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
135 mimeTypeRegex: string,
136 field: string,
137 maxSize: number | null,
138 optional = false
139 ) {
140 // Should have files
141 if (!files) return optional
142 if (isArray(files)) return optional
143
144 // Should have a file
145 const fileArray = files[field]
146 if (!fileArray || fileArray.length === 0) {
147 return optional
148 }
149
150 // The file should exist
151 const file = fileArray[0]
152 if (!file || !file.originalname) return false
153
154 // Check size
155 if ((maxSize !== null) && file.size > maxSize) return false
156
157 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
158 }
159
160 // ---------------------------------------------------------------------------
161
162 export {
163 exists,
164 isArrayOf,
165 isNotEmptyIntArray,
166 isArray,
167 isIntOrNull,
168 isIdValid,
169 isSafePath,
170 isUUIDValid,
171 isIdOrUUIDValid,
172 isDateValid,
173 toValueOrNull,
174 toBooleanOrNull,
175 isBooleanValid,
176 toIntOrNull,
177 toArray,
178 toIntArray,
179 isFileFieldValid,
180 isFileMimeTypeValid,
181 isFileValid
182 }