aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/misc.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-08 21:16:10 +0100
committerGitHub <noreply@github.com>2020-12-08 21:16:10 +0100
commitf2eb23cd87cf32b8fe545178143b5f49e06a58da (patch)
treeaf7d59945af70e28fd85047e2c688c59a908f548 /server/helpers/custom-validators/misc.ts
parentc977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff)
downloadPeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.gz
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.zst
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.zip
emit more specific status codes on video upload (#3423)
- reduce http status codes list to potentially useful codes - convert more codes to typed ones - factorize html generator for error responses
Diffstat (limited to 'server/helpers/custom-validators/misc.ts')
-rw-r--r--server/helpers/custom-validators/misc.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index 61c03f0c9..effdd98cb 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -86,6 +86,50 @@ function toIntArray (value: any) {
86 return value.map(v => validator.toInt(v)) 86 return value.map(v => validator.toInt(v))
87} 87}
88 88
89function isFileFieldValid (
90 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
91 field: string,
92 optional = false
93) {
94 // Should have files
95 if (!files) return optional
96 if (isArray(files)) return optional
97
98 // Should have a file
99 const fileArray = files[field]
100 if (!fileArray || fileArray.length === 0) {
101 return optional
102 }
103
104 // The file should exist
105 const file = fileArray[0]
106 if (!file || !file.originalname) return false
107 return file
108}
109
110function isFileMimeTypeValid (
111 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
112 mimeTypeRegex: string,
113 field: string,
114 optional = false
115) {
116 // Should have files
117 if (!files) return optional
118 if (isArray(files)) return optional
119
120 // Should have a file
121 const fileArray = files[field]
122 if (!fileArray || fileArray.length === 0) {
123 return optional
124 }
125
126 // The file should exist
127 const file = fileArray[0]
128 if (!file || !file.originalname) return false
129
130 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
131}
132
89function isFileValid ( 133function isFileValid (
90 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], 134 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
91 mimeTypeRegex: string, 135 mimeTypeRegex: string,
@@ -132,5 +176,7 @@ export {
132 toIntOrNull, 176 toIntOrNull,
133 toArray, 177 toArray,
134 toIntArray, 178 toIntArray,
179 isFileFieldValid,
180 isFileMimeTypeValid,
135 isFileValid 181 isFileValid
136} 182}