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