]>
Commit | Line | Data |
---|---|---|
ac81d1a0 | 1 | import 'multer' |
f6d6e7f8 | 2 | import { UploadFilesForCheck } from 'express' |
345da516 | 3 | import { sep } from 'path' |
f6d6e7f8 | 4 | import validator from 'validator' |
0628157f | 5 | import { isShortUUID, shortToUUID } from '@shared/extra-utils' |
fdbda9e3 | 6 | |
69818c93 | 7 | function exists (value: any) { |
e4c55619 C |
8 | return value !== undefined && value !== null |
9 | } | |
10 | ||
345da516 C |
11 | function isSafePath (p: string) { |
12 | return exists(p) && | |
13 | (p + '').split(sep).every(part => { | |
f023a19c | 14 | return [ '..' ].includes(part) === false |
345da516 C |
15 | }) |
16 | } | |
17 | ||
c158a5fa | 18 | function isArray (value: any): value is any[] { |
e4c55619 C |
19 | return Array.isArray(value) |
20 | } | |
21 | ||
2f1548fd C |
22 | function isNotEmptyIntArray (value: any) { |
23 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 | |
cef534ed C |
24 | } |
25 | ||
b033851f C |
26 | function isNotEmptyStringArray (value: any) { |
27 | return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0 | |
28 | } | |
29 | ||
09209296 C |
30 | function isArrayOf (value: any, validator: (value: any) => boolean) { |
31 | return isArray(value) && value.every(v => validator(v)) | |
32 | } | |
33 | ||
72c7248b C |
34 | function isDateValid (value: string) { |
35 | return exists(value) && validator.isISO8601(value) | |
36 | } | |
37 | ||
38 | function isIdValid (value: string) { | |
39 | return exists(value) && validator.isInt('' + value) | |
40 | } | |
41 | ||
42 | function isUUIDValid (value: string) { | |
43 | return exists(value) && validator.isUUID('' + value, 4) | |
44 | } | |
45 | ||
fbd67e7f C |
46 | function areUUIDsValid (values: string[]) { |
47 | return isArray(values) && values.every(v => isUUIDValid(v)) | |
48 | } | |
49 | ||
72c7248b C |
50 | function isIdOrUUIDValid (value: string) { |
51 | return isIdValid(value) || isUUIDValid(value) | |
52 | } | |
53 | ||
360329cc | 54 | function isBooleanValid (value: any) { |
47564bbe C |
55 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
56 | } | |
57 | ||
fb719404 C |
58 | function isIntOrNull (value: any) { |
59 | return value === null || validator.isInt('' + value) | |
60 | } | |
61 | ||
d4a8e7a6 | 62 | // --------------------------------------------------------------------------- |
f0a39880 | 63 | |
c729caf6 C |
64 | function isFileValid (options: { |
65 | files: UploadFilesForCheck | |
f2eb23cd | 66 | |
c729caf6 C |
67 | maxSize: number | null |
68 | mimeTypeRegex: string | null | |
f2eb23cd | 69 | |
c729caf6 | 70 | field?: string |
f2eb23cd | 71 | |
c729caf6 C |
72 | optional?: boolean // Default false |
73 | }) { | |
74 | const { files, mimeTypeRegex, field, maxSize, optional = false } = options | |
f2eb23cd | 75 | |
ac81d1a0 C |
76 | // Should have files |
77 | if (!files) return optional | |
ac81d1a0 | 78 | |
c729caf6 C |
79 | const fileArray = isArray(files) |
80 | ? files | |
81 | : files[field] | |
82 | ||
83 | if (!fileArray || !isArray(fileArray) || fileArray.length === 0) { | |
ac81d1a0 C |
84 | return optional |
85 | } | |
86 | ||
c729caf6 | 87 | // The file exists |
a1587156 | 88 | const file = fileArray[0] |
99b75748 | 89 | if (!file?.originalname) return false |
ac81d1a0 | 90 | |
0c237b19 | 91 | // Check size |
c1e791ba | 92 | if ((maxSize !== null) && file.size > maxSize) return false |
0c237b19 | 93 | |
c729caf6 C |
94 | if (mimeTypeRegex === null) return true |
95 | ||
96 | return checkMimetypeRegex(file.mimetype, mimeTypeRegex) | |
97 | } | |
98 | ||
99 | function checkMimetypeRegex (fileMimeType: string, mimeTypeRegex: string) { | |
100 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(fileMimeType) | |
ac81d1a0 C |
101 | } |
102 | ||
e4c55619 C |
103 | // --------------------------------------------------------------------------- |
104 | ||
d4a8e7a6 | 105 | function toCompleteUUID (value: string) { |
ded68a97 C |
106 | if (isShortUUID(value)) { |
107 | try { | |
108 | return shortToUUID(value) | |
109 | } catch { | |
110 | return null | |
111 | } | |
112 | } | |
d4a8e7a6 C |
113 | |
114 | return value | |
115 | } | |
116 | ||
fbd67e7f C |
117 | function toCompleteUUIDs (values: string[]) { |
118 | return values.map(v => toCompleteUUID(v)) | |
119 | } | |
120 | ||
d4a8e7a6 C |
121 | function toIntOrNull (value: string) { |
122 | const v = toValueOrNull(value) | |
123 | ||
124 | if (v === null || v === undefined) return v | |
125 | if (typeof v === 'number') return v | |
126 | ||
127 | return validator.toInt('' + v) | |
128 | } | |
129 | ||
130 | function toBooleanOrNull (value: any) { | |
131 | const v = toValueOrNull(value) | |
132 | ||
133 | if (v === null || v === undefined) return v | |
134 | if (typeof v === 'boolean') return v | |
135 | ||
136 | return validator.toBoolean('' + v) | |
137 | } | |
138 | ||
139 | function toValueOrNull (value: string) { | |
140 | if (value === 'null') return null | |
141 | ||
142 | return value | |
143 | } | |
144 | ||
d4a8e7a6 C |
145 | function toIntArray (value: any) { |
146 | if (!value) return [] | |
147 | if (isArray(value) === false) return [ validator.toInt(value) ] | |
148 | ||
149 | return value.map(v => validator.toInt(v)) | |
150 | } | |
151 | ||
152 | // --------------------------------------------------------------------------- | |
153 | ||
65fcc311 C |
154 | export { |
155 | exists, | |
09209296 | 156 | isArrayOf, |
2f1548fd | 157 | isNotEmptyIntArray, |
72c7248b | 158 | isArray, |
fb719404 | 159 | isIntOrNull, |
72c7248b | 160 | isIdValid, |
345da516 | 161 | isSafePath, |
b033851f | 162 | isNotEmptyStringArray, |
72c7248b | 163 | isUUIDValid, |
fbd67e7f | 164 | toCompleteUUIDs, |
d4a8e7a6 | 165 | toCompleteUUID, |
72c7248b | 166 | isIdOrUUIDValid, |
47564bbe | 167 | isDateValid, |
2efd32f6 | 168 | toValueOrNull, |
c8861d5d | 169 | toBooleanOrNull, |
ac81d1a0 | 170 | isBooleanValid, |
360329cc | 171 | toIntOrNull, |
fbd67e7f | 172 | areUUIDsValid, |
f0a39880 | 173 | toIntArray, |
c729caf6 C |
174 | isFileValid, |
175 | checkMimetypeRegex | |
65fcc311 | 176 | } |