diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 84 | ||||
-rw-r--r-- | server/helpers/image-utils.ts | 4 | ||||
-rw-r--r-- | server/helpers/uuid.ts | 32 |
3 files changed, 82 insertions, 38 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 229e9f03c..528bfcfb8 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -2,6 +2,7 @@ import 'multer' | |||
2 | import { UploadFilesForCheck } from 'express' | 2 | import { UploadFilesForCheck } from 'express' |
3 | import { sep } from 'path' | 3 | import { sep } from 'path' |
4 | import validator from 'validator' | 4 | import validator from 'validator' |
5 | import { isShortUUID, shortToUUID } from '../uuid' | ||
5 | 6 | ||
6 | function exists (value: any) { | 7 | function exists (value: any) { |
7 | return value !== undefined && value !== null | 8 | return value !== undefined && value !== null |
@@ -50,42 +51,7 @@ function isIntOrNull (value: any) { | |||
50 | return value === null || validator.isInt('' + value) | 51 | return value === null || validator.isInt('' + value) |
51 | } | 52 | } |
52 | 53 | ||
53 | function toIntOrNull (value: string) { | 54 | // --------------------------------------------------------------------------- |
54 | const v = toValueOrNull(value) | ||
55 | |||
56 | if (v === null || v === undefined) return v | ||
57 | if (typeof v === 'number') return v | ||
58 | |||
59 | return validator.toInt('' + v) | ||
60 | } | ||
61 | |||
62 | function toBooleanOrNull (value: any) { | ||
63 | const v = toValueOrNull(value) | ||
64 | |||
65 | if (v === null || v === undefined) return v | ||
66 | if (typeof v === 'boolean') return v | ||
67 | |||
68 | return validator.toBoolean('' + v) | ||
69 | } | ||
70 | |||
71 | function toValueOrNull (value: string) { | ||
72 | if (value === 'null') return null | ||
73 | |||
74 | return value | ||
75 | } | ||
76 | |||
77 | function toArray (value: any) { | ||
78 | if (value && isArray(value) === false) return [ value ] | ||
79 | |||
80 | return value | ||
81 | } | ||
82 | |||
83 | function toIntArray (value: any) { | ||
84 | if (!value) return [] | ||
85 | if (isArray(value) === false) return [ validator.toInt(value) ] | ||
86 | |||
87 | return value.map(v => validator.toInt(v)) | ||
88 | } | ||
89 | 55 | ||
90 | function isFileFieldValid ( | 56 | function isFileFieldValid ( |
91 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | 57 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], |
@@ -160,6 +126,51 @@ function isFileValid ( | |||
160 | 126 | ||
161 | // --------------------------------------------------------------------------- | 127 | // --------------------------------------------------------------------------- |
162 | 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 | |||
163 | export { | 174 | export { |
164 | exists, | 175 | exists, |
165 | isArrayOf, | 176 | isArrayOf, |
@@ -169,6 +180,7 @@ export { | |||
169 | isIdValid, | 180 | isIdValid, |
170 | isSafePath, | 181 | isSafePath, |
171 | isUUIDValid, | 182 | isUUIDValid, |
183 | toCompleteUUID, | ||
172 | isIdOrUUIDValid, | 184 | isIdOrUUIDValid, |
173 | isDateValid, | 185 | isDateValid, |
174 | toValueOrNull, | 186 | toValueOrNull, |
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index 122fb009d..c76ed545b 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | import { copy, readFile, remove, rename } from 'fs-extra' | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
2 | import * as Jimp from 'jimp' | 2 | import * as Jimp from 'jimp' |
3 | import { v4 as uuidv4 } from 'uuid' | ||
4 | import { getLowercaseExtension } from './core-utils' | 3 | import { getLowercaseExtension } from './core-utils' |
5 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' | 4 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' |
6 | import { logger } from './logger' | 5 | import { logger } from './logger' |
6 | import { buildUUID } from './uuid' | ||
7 | 7 | ||
8 | function generateImageFilename (extension = '.jpg') { | 8 | function generateImageFilename (extension = '.jpg') { |
9 | return uuidv4() + extension | 9 | return buildUUID() + extension |
10 | } | 10 | } |
11 | 11 | ||
12 | async function processImage ( | 12 | async function processImage ( |
diff --git a/server/helpers/uuid.ts b/server/helpers/uuid.ts new file mode 100644 index 000000000..3eb06c773 --- /dev/null +++ b/server/helpers/uuid.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | import * as short from 'short-uuid' | ||
2 | |||
3 | const translator = short() | ||
4 | |||
5 | function buildUUID () { | ||
6 | return short.uuid() | ||
7 | } | ||
8 | |||
9 | function uuidToShort (uuid: string) { | ||
10 | if (!uuid) return uuid | ||
11 | |||
12 | return translator.fromUUID(uuid) | ||
13 | } | ||
14 | |||
15 | function shortToUUID (shortUUID: string) { | ||
16 | if (!shortUUID) return shortUUID | ||
17 | |||
18 | return translator.toUUID(shortUUID) | ||
19 | } | ||
20 | |||
21 | function isShortUUID (value: string) { | ||
22 | if (!value) return false | ||
23 | |||
24 | return value.length === translator.maxLength | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | buildUUID, | ||
29 | uuidToShort, | ||
30 | shortToUUID, | ||
31 | isShortUUID | ||
32 | } | ||