]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/videos.ts
Fix client compilation
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
4d4e5cd4 2import * as validator from 'validator'
1840c2f7 3import 'multer'
65fcc311
C
4
5import {
6 CONSTRAINTS_FIELDS,
7 VIDEO_CATEGORIES,
8 VIDEO_LICENCES,
9 VIDEO_LANGUAGES,
10 VIDEO_RATE_TYPES
11} from '../../initializers'
12import { isUserUsernameValid } from './users'
69818c93 13import { isArray, exists } from './misc'
ee9e7b61 14import { VideoRateType } from '../../../shared'
65fcc311
C
15
16const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
17const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
18const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
0b697522 19
69818c93 20function isVideoAuthorValid (value: string) {
65fcc311 21 return isUserUsernameValid(value)
be587647
C
22}
23
69818c93
C
24function isVideoDateValid (value: string) {
25 return exists(value) && validator.isISO8601(value)
be587647
C
26}
27
69818c93 28function isVideoCategoryValid (value: number) {
65fcc311 29 return VIDEO_CATEGORIES[value] !== undefined
6e07c3de
C
30}
31
69818c93 32function isVideoLicenceValid (value: number) {
65fcc311 33 return VIDEO_LICENCES[value] !== undefined
6f0c39e2
C
34}
35
69818c93 36function isVideoLanguageValid (value: number) {
65fcc311 37 return value === null || VIDEO_LANGUAGES[value] !== undefined
3092476e
C
38}
39
69818c93
C
40function isVideoNSFWValid (value: any) {
41 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
31b59b47
C
42}
43
69818c93
C
44function isVideoDescriptionValid (value: string) {
45 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
be587647
C
46}
47
69818c93
C
48function isVideoDurationValid (value: string) {
49 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
be587647
C
50}
51
69818c93 52function isVideoExtnameValid (value: string) {
feb4bdfd
C
53 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
54}
55
69818c93
C
56function isVideoInfoHashValid (value: string) {
57 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
be587647
C
58}
59
69818c93
C
60function isVideoNameValid (value: string) {
61 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
be587647
C
62}
63
69818c93 64function isVideoTagsValid (tags: string[]) {
65fcc311 65 return isArray(tags) &&
69818c93 66 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
be587647 67 tags.every(function (tag) {
69818c93 68 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
be587647
C
69 })
70}
71
69818c93
C
72function isVideoThumbnailValid (value: string) {
73 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
aaf61f38
C
74}
75
69818c93
C
76function isVideoThumbnailDataValid (value: string) {
77 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
be587647
C
78}
79
69818c93
C
80function isVideoRemoteIdValid (value: string) {
81 return exists(value) && validator.isUUID(value, 4)
558d7c23
C
82}
83
69818c93
C
84function isVideoAbuseReasonValid (value: string) {
85 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
e4c55619
C
86}
87
69818c93 88function isVideoAbuseReporterUsernameValid (value: string) {
65fcc311 89 return isUserUsernameValid(value)
3d118fb5
C
90}
91
69818c93
C
92function isVideoViewsValid (value: string) {
93 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
9e167724
C
94}
95
69818c93
C
96function isVideoLikesValid (value: string) {
97 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
9e167724
C
98}
99
69818c93
C
100function isVideoDislikesValid (value: string) {
101 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
e4c87ec2
C
102}
103
69818c93
C
104function isVideoEventCountValid (value: string) {
105 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
9e167724
C
106}
107
69818c93 108function isVideoRatingTypeValid (value: string) {
ee9e7b61 109 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
d38b8281
C
110}
111
69818c93 112function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
f6f7dfee
C
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
55fa55a9
C
127// ---------------------------------------------------------------------------
128
65fcc311
C
129export {
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}
69818c93
C
154
155declare 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}