]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/videos.ts
Add lazy description on server
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
... / ...
CommitLineData
1import { values } from 'lodash'
2import * as validator from 'validator'
3import * as Promise from 'bluebird'
4import * as express from 'express'
5import 'express-validator'
6import 'multer'
7
8import {
9 CONSTRAINTS_FIELDS,
10 VIDEO_CATEGORIES,
11 VIDEO_LICENCES,
12 VIDEO_LANGUAGES,
13 VIDEO_RATE_TYPES,
14 database as db
15} from '../../initializers'
16import { isUserUsernameValid } from './users'
17import { isArray, exists } from './misc'
18import { VideoInstance } from '../../models'
19import { logger } from '../../helpers'
20import { VideoRateType } from '../../../shared'
21
22const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
23const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
24const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
25
26function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined
28}
29
30// Maybe we don't know the remote category, but that doesn't matter
31function isRemoteVideoCategoryValid (value: string) {
32 return validator.isInt('' + value)
33}
34
35function isVideoLicenceValid (value: number) {
36 return VIDEO_LICENCES[value] !== undefined
37}
38
39// Maybe we don't know the remote licence, but that doesn't matter
40function isRemoteVideoLicenceValid (value: string) {
41 return validator.isInt('' + value)
42}
43
44function isVideoLanguageValid (value: number) {
45 return value === null || VIDEO_LANGUAGES[value] !== undefined
46}
47
48// Maybe we don't know the remote language, but that doesn't matter
49function isRemoteVideoLanguageValid (value: string) {
50 return validator.isInt('' + value)
51}
52
53function isVideoNSFWValid (value: any) {
54 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
55}
56
57function isVideoTruncatedDescriptionValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
59}
60
61function isVideoDescriptionValid (value: string) {
62 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
63}
64
65function isVideoDurationValid (value: string) {
66 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
67}
68
69function isVideoNameValid (value: string) {
70 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
71}
72
73function isVideoTagsValid (tags: string[]) {
74 return isArray(tags) &&
75 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
76 tags.every(tag => {
77 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
78 })
79}
80
81function isVideoThumbnailValid (value: string) {
82 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
83}
84
85function isVideoThumbnailDataValid (value: string) {
86 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
87}
88
89function isVideoAbuseReasonValid (value: string) {
90 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
91}
92
93function isVideoAbuseReporterUsernameValid (value: string) {
94 return isUserUsernameValid(value)
95}
96
97function isVideoViewsValid (value: string) {
98 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
99}
100
101function isVideoLikesValid (value: string) {
102 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
103}
104
105function isVideoDislikesValid (value: string) {
106 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
107}
108
109function isVideoEventCountValid (value: string) {
110 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
111}
112
113function isVideoRatingTypeValid (value: string) {
114 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
115}
116
117function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
118 // Should have files
119 if (!files) return false
120 if (isArray(files)) return false
121
122 // Should have videofile file
123 const videofile = files['videofile']
124 if (!videofile || videofile.length === 0) return false
125
126 // The file should exist
127 const file = videofile[0]
128 if (!file || !file.originalname) return false
129
130 return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype)
131}
132
133function isVideoFileSizeValid (value: string) {
134 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
135}
136
137function isVideoFileResolutionValid (value: string) {
138 return exists(value) && validator.isInt(value + '')
139}
140
141function isVideoFileExtnameValid (value: string) {
142 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
143}
144
145function isVideoFileInfoHashValid (value: string) {
146 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
147}
148
149function checkVideoExists (id: string, res: express.Response, callback: () => void) {
150 let promise: Promise<VideoInstance>
151 if (validator.isInt(id)) {
152 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
153 } else { // UUID
154 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
155 }
156
157 promise.then(video => {
158 if (!video) {
159 return res.status(404)
160 .json({ error: 'Video not found' })
161 .end()
162 }
163
164 res.locals.video = video
165 callback()
166 })
167 .catch(err => {
168 logger.error('Error in video request validator.', err)
169 return res.sendStatus(500)
170 })
171}
172
173// ---------------------------------------------------------------------------
174
175export {
176 isVideoCategoryValid,
177 isVideoLicenceValid,
178 isVideoLanguageValid,
179 isVideoNSFWValid,
180 isVideoTruncatedDescriptionValid,
181 isVideoDescriptionValid,
182 isVideoDurationValid,
183 isVideoFileInfoHashValid,
184 isVideoNameValid,
185 isVideoTagsValid,
186 isVideoThumbnailValid,
187 isVideoThumbnailDataValid,
188 isVideoFileExtnameValid,
189 isVideoAbuseReasonValid,
190 isVideoAbuseReporterUsernameValid,
191 isVideoFile,
192 isVideoViewsValid,
193 isVideoLikesValid,
194 isVideoRatingTypeValid,
195 isVideoDislikesValid,
196 isVideoEventCountValid,
197 isVideoFileSizeValid,
198 isVideoFileResolutionValid,
199 checkVideoExists,
200 isRemoteVideoCategoryValid,
201 isRemoteVideoLicenceValid,
202 isRemoteVideoLanguageValid
203}