]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/videos.ts
Add tests for open graph tags
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
1 import { values } from 'lodash'
2 import * as validator from 'validator'
3 import 'multer'
4
5 import {
6 CONSTRAINTS_FIELDS,
7 VIDEO_CATEGORIES,
8 VIDEO_LICENCES,
9 VIDEO_LANGUAGES,
10 VIDEO_RATE_TYPES
11 } from '../../initializers'
12 import { isUserUsernameValid } from './users'
13 import { isArray, exists } from './misc'
14 import { VideoRateType } from '../../../shared'
15
16 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
18 const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
19
20 function isVideoIdOrUUIDValid (value: string) {
21 return validator.isInt(value) || isVideoUUIDValid(value)
22 }
23
24 function isVideoAuthorValid (value: string) {
25 return isUserUsernameValid(value)
26 }
27
28 function isVideoDateValid (value: string) {
29 return exists(value) && validator.isISO8601(value)
30 }
31
32 function isVideoCategoryValid (value: number) {
33 return VIDEO_CATEGORIES[value] !== undefined
34 }
35
36 function isVideoLicenceValid (value: number) {
37 return VIDEO_LICENCES[value] !== undefined
38 }
39
40 function isVideoLanguageValid (value: number) {
41 return value === null || VIDEO_LANGUAGES[value] !== undefined
42 }
43
44 function isVideoNSFWValid (value: any) {
45 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
46 }
47
48 function isVideoDescriptionValid (value: string) {
49 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
50 }
51
52 function isVideoDurationValid (value: string) {
53 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
54 }
55
56 function isVideoExtnameValid (value: string) {
57 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
58 }
59
60 function isVideoInfoHashValid (value: string) {
61 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
62 }
63
64 function isVideoNameValid (value: string) {
65 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
66 }
67
68 function isVideoTagsValid (tags: string[]) {
69 return isArray(tags) &&
70 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
71 tags.every(function (tag) {
72 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
73 })
74 }
75
76 function isVideoThumbnailValid (value: string) {
77 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
78 }
79
80 function isVideoThumbnailDataValid (value: string) {
81 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
82 }
83
84 function isVideoUUIDValid (value: string) {
85 return exists(value) && validator.isUUID('' + value, 4)
86 }
87
88 function isVideoAbuseReasonValid (value: string) {
89 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
90 }
91
92 function isVideoAbuseReporterUsernameValid (value: string) {
93 return isUserUsernameValid(value)
94 }
95
96 function isVideoViewsValid (value: string) {
97 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
98 }
99
100 function isVideoLikesValid (value: string) {
101 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
102 }
103
104 function isVideoDislikesValid (value: string) {
105 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
106 }
107
108 function isVideoEventCountValid (value: string) {
109 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
110 }
111
112 function isVideoRatingTypeValid (value: string) {
113 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
114 }
115
116 function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
117 // Should have files
118 if (!files) return false
119
120 // Should have videofile file
121 const videofile = files.videofile
122 if (!videofile || videofile.length === 0) return false
123
124 // The file should exist
125 const file = videofile[0]
126 if (!file || !file.originalname) return false
127
128 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
129 }
130
131 // ---------------------------------------------------------------------------
132
133 export {
134 isVideoIdOrUUIDValid,
135 isVideoAuthorValid,
136 isVideoDateValid,
137 isVideoCategoryValid,
138 isVideoLicenceValid,
139 isVideoLanguageValid,
140 isVideoNSFWValid,
141 isVideoDescriptionValid,
142 isVideoDurationValid,
143 isVideoInfoHashValid,
144 isVideoNameValid,
145 isVideoTagsValid,
146 isVideoThumbnailValid,
147 isVideoThumbnailDataValid,
148 isVideoExtnameValid,
149 isVideoUUIDValid,
150 isVideoAbuseReasonValid,
151 isVideoAbuseReporterUsernameValid,
152 isVideoFile,
153 isVideoViewsValid,
154 isVideoLikesValid,
155 isVideoRatingTypeValid,
156 isVideoDislikesValid,
157 isVideoEventCountValid
158 }
159
160 declare global {
161 namespace ExpressValidator {
162 export interface Validator {
163 isVideoIdOrUUIDValid,
164 isVideoAuthorValid,
165 isVideoDateValid,
166 isVideoCategoryValid,
167 isVideoLicenceValid,
168 isVideoLanguageValid,
169 isVideoNSFWValid,
170 isVideoDescriptionValid,
171 isVideoDurationValid,
172 isVideoInfoHashValid,
173 isVideoNameValid,
174 isVideoTagsValid,
175 isVideoThumbnailValid,
176 isVideoThumbnailDataValid,
177 isVideoExtnameValid,
178 isVideoUUIDValid,
179 isVideoAbuseReasonValid,
180 isVideoAbuseReporterUsernameValid,
181 isVideoFile,
182 isVideoViewsValid,
183 isVideoLikesValid,
184 isVideoRatingTypeValid,
185 isVideoDislikesValid,
186 isVideoEventCountValid
187 }
188 }
189 }