aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r--server/helpers/custom-validators/videos.ts34
1 files changed, 32 insertions, 2 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index a31aca019..05d1dc607 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -1,5 +1,7 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as validator from 'validator' 2import * as validator from 'validator'
3import * as Promise from 'bluebird'
4import * as express from 'express'
3import 'express-validator' 5import 'express-validator'
4import 'multer' 6import 'multer'
5 7
@@ -8,10 +10,13 @@ import {
8 VIDEO_CATEGORIES, 10 VIDEO_CATEGORIES,
9 VIDEO_LICENCES, 11 VIDEO_LICENCES,
10 VIDEO_LANGUAGES, 12 VIDEO_LANGUAGES,
11 VIDEO_RATE_TYPES 13 VIDEO_RATE_TYPES,
14 database as db
12} from '../../initializers' 15} from '../../initializers'
13import { isUserUsernameValid } from './users' 16import { isUserUsernameValid } from './users'
14import { isArray, exists } from './misc' 17import { isArray, exists } from './misc'
18import { VideoInstance } from '../../models'
19import { logger } from '../../helpers'
15import { VideoRateType } from '../../../shared' 20import { VideoRateType } from '../../../shared'
16 21
17const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 22const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
@@ -138,6 +143,30 @@ function isVideoFileInfoHashValid (value: string) {
138 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) 143 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
139} 144}
140 145
146function checkVideoExists (id: string, res: express.Response, callback: () => void) {
147 let promise: Promise<VideoInstance>
148 if (validator.isInt(id)) {
149 promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
150 } else { // UUID
151 promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
152 }
153
154 promise.then(video => {
155 if (!video) {
156 return res.status(404)
157 .json({ error: 'Video not found' })
158 .end()
159 }
160
161 res.locals.video = video
162 callback()
163 })
164 .catch(err => {
165 logger.error('Error in video request validator.', err)
166 return res.sendStatus(500)
167 })
168}
169
141// --------------------------------------------------------------------------- 170// ---------------------------------------------------------------------------
142 171
143export { 172export {
@@ -166,5 +195,6 @@ export {
166 isVideoDislikesValid, 195 isVideoDislikesValid,
167 isVideoEventCountValid, 196 isVideoEventCountValid,
168 isVideoFileSizeValid, 197 isVideoFileSizeValid,
169 isVideoFileResolutionValid 198 isVideoFileResolutionValid,
199 checkVideoExists
170} 200}