aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/misc.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-02-13 18:17:05 +0100
committerChocobozzz <me@florianbigard.com>2018-02-14 16:03:09 +0100
commitac81d1a06d57b9ae86663831e7f5edcef57b0fa4 (patch)
treeda31775c9533d3e270f68f921e146f086bf7c0b8 /server/helpers/custom-validators/misc.ts
parente883399fa6caa56bb8519c9a2e22d88001f26661 (diff)
downloadPeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.tar.gz
PeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.tar.zst
PeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.zip
Add ability to set video thumbnail/preview
Diffstat (limited to 'server/helpers/custom-validators/misc.ts')
-rw-r--r--server/helpers/custom-validators/misc.ts27
1 files changed, 26 insertions, 1 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index 3903884ea..8a270b777 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -1,3 +1,4 @@
1import 'multer'
1import * as validator from 'validator' 2import * as validator from 'validator'
2 3
3function exists (value: any) { 4function exists (value: any) {
@@ -28,6 +29,29 @@ function isBooleanValid (value: string) {
28 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) 29 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
29} 30}
30 31
32function isFileValid (
33 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
34 mimeTypeRegex: string,
35 field: string,
36 optional = false
37) {
38 // Should have files
39 if (!files) return optional
40 if (isArray(files)) return optional
41
42 // Should have a file
43 const fileArray = files[ field ]
44 if (!fileArray || fileArray.length === 0) {
45 return optional
46 }
47
48 // The file should exist
49 const file = fileArray[ 0 ]
50 if (!file || !file.originalname) return false
51
52 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
53}
54
31// --------------------------------------------------------------------------- 55// ---------------------------------------------------------------------------
32 56
33export { 57export {
@@ -37,5 +61,6 @@ export {
37 isUUIDValid, 61 isUUIDValid,
38 isIdOrUUIDValid, 62 isIdOrUUIDValid,
39 isDateValid, 63 isDateValid,
40 isBooleanValid 64 isBooleanValid,
65 isFileValid
41} 66}