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