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