]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/users.ts
Begin to add avatar to actors
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.ts
CommitLineData
4d4e5cd4 1import * as validator from 'validator'
fdbda9e3 2import 'express-validator'
e4c55619 3
c5911fd3 4import { exists, isArray } from './misc'
954605a8 5import { CONSTRAINTS_FIELDS } from '../../initializers'
ee9e7b61
C
6import { UserRole } from '../../../shared'
7
65fcc311 8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
e4c55619 9
69818c93 10function isUserPasswordValid (value: string) {
9bd26629
C
11 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
12}
13
b0f9f39e
C
14function isUserVideoQuotaValid (value: string) {
15 return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
16}
17
69818c93 18function isUserUsernameValid (value: string) {
9bd26629
C
19 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
20 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
563d032e 21 return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
e4c55619
C
22}
23
7efe153b 24function isBoolean (value: any) {
69818c93 25 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
1d49e1e2
C
26}
27
7efe153b
AL
28function isUserDisplayNSFWValid (value: any) {
29 return isBoolean(value)
30}
31
32function isUserAutoPlayVideoValid (value: any) {
33 return isBoolean(value)
34}
35
954605a8
C
36function isUserRoleValid (value: any) {
37 return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
38}
39
c5911fd3
C
40function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
41 // Should have files
42 if (!files) return false
43 if (isArray(files)) return false
44
45 // Should have videofile file
46 const avatarfile = files['avatarfile']
47 if (!avatarfile || avatarfile.length === 0) return false
48
49 // The file should exist
50 const file = avatarfile[0]
51 if (!file || !file.originalname) return false
52
53 return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype)
54}
55
e4c55619
C
56// ---------------------------------------------------------------------------
57
65fcc311
C
58export {
59 isUserPasswordValid,
60 isUserRoleValid,
b0f9f39e 61 isUserVideoQuotaValid,
65fcc311 62 isUserUsernameValid,
7efe153b 63 isUserDisplayNSFWValid,
c5911fd3
C
64 isUserAutoPlayVideoValid,
65 isAvatarFile
65fcc311 66}