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