diff options
Diffstat (limited to 'server/helpers/custom-validators/misc.ts')
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 79 |
1 files changed, 23 insertions, 56 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 81a60ee66..c80c86193 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -61,75 +61,43 @@ function isIntOrNull (value: any) { | |||
61 | 61 | ||
62 | // --------------------------------------------------------------------------- | 62 | // --------------------------------------------------------------------------- |
63 | 63 | ||
64 | function isFileFieldValid ( | 64 | function isFileValid (options: { |
65 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | 65 | files: UploadFilesForCheck |
66 | field: string, | ||
67 | optional = false | ||
68 | ) { | ||
69 | // Should have files | ||
70 | if (!files) return optional | ||
71 | if (isArray(files)) return optional | ||
72 | 66 | ||
73 | // Should have a file | 67 | maxSize: number | null |
74 | const fileArray = files[field] | 68 | mimeTypeRegex: string | null |
75 | if (!fileArray || fileArray.length === 0) { | ||
76 | return optional | ||
77 | } | ||
78 | 69 | ||
79 | // The file should exist | 70 | field?: string |
80 | const file = fileArray[0] | ||
81 | if (!file || !file.originalname) return false | ||
82 | return file | ||
83 | } | ||
84 | 71 | ||
85 | function isFileMimeTypeValid ( | 72 | optional?: boolean // Default false |
86 | files: UploadFilesForCheck, | 73 | }) { |
87 | mimeTypeRegex: string, | 74 | const { files, mimeTypeRegex, field, maxSize, optional = false } = options |
88 | field: string, | ||
89 | optional = false | ||
90 | ) { | ||
91 | // Should have files | ||
92 | if (!files) return optional | ||
93 | if (isArray(files)) return optional | ||
94 | 75 | ||
95 | // Should have a file | ||
96 | const fileArray = files[field] | ||
97 | if (!fileArray || fileArray.length === 0) { | ||
98 | return optional | ||
99 | } | ||
100 | |||
101 | // The file should exist | ||
102 | const file = fileArray[0] | ||
103 | if (!file || !file.originalname) return false | ||
104 | |||
105 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) | ||
106 | } | ||
107 | |||
108 | function isFileValid ( | ||
109 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | ||
110 | mimeTypeRegex: string, | ||
111 | field: string, | ||
112 | maxSize: number | null, | ||
113 | optional = false | ||
114 | ) { | ||
115 | // Should have files | 76 | // Should have files |
116 | if (!files) return optional | 77 | if (!files) return optional |
117 | if (isArray(files)) return optional | ||
118 | 78 | ||
119 | // Should have a file | 79 | const fileArray = isArray(files) |
120 | const fileArray = files[field] | 80 | ? files |
121 | if (!fileArray || fileArray.length === 0) { | 81 | : files[field] |
82 | |||
83 | if (!fileArray || !isArray(fileArray) || fileArray.length === 0) { | ||
122 | return optional | 84 | return optional |
123 | } | 85 | } |
124 | 86 | ||
125 | // The file should exist | 87 | // The file exists |
126 | const file = fileArray[0] | 88 | const file = fileArray[0] |
127 | if (!file || !file.originalname) return false | 89 | if (!file || !file.originalname) return false |
128 | 90 | ||
129 | // Check size | 91 | // Check size |
130 | if ((maxSize !== null) && file.size > maxSize) return false | 92 | if ((maxSize !== null) && file.size > maxSize) return false |
131 | 93 | ||
132 | return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) | 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) | ||
133 | } | 101 | } |
134 | 102 | ||
135 | // --------------------------------------------------------------------------- | 103 | // --------------------------------------------------------------------------- |
@@ -204,7 +172,6 @@ export { | |||
204 | areUUIDsValid, | 172 | areUUIDsValid, |
205 | toArray, | 173 | toArray, |
206 | toIntArray, | 174 | toIntArray, |
207 | isFileFieldValid, | 175 | isFileValid, |
208 | isFileMimeTypeValid, | 176 | checkMimetypeRegex |
209 | isFileValid | ||
210 | } | 177 | } |