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