diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 34 |
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 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as validator from 'validator' | 2 | import * as validator from 'validator' |
3 | import * as Promise from 'bluebird' | ||
4 | import * as express from 'express' | ||
3 | import 'express-validator' | 5 | import 'express-validator' |
4 | import 'multer' | 6 | import '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' |
13 | import { isUserUsernameValid } from './users' | 16 | import { isUserUsernameValid } from './users' |
14 | import { isArray, exists } from './misc' | 17 | import { isArray, exists } from './misc' |
18 | import { VideoInstance } from '../../models' | ||
19 | import { logger } from '../../helpers' | ||
15 | import { VideoRateType } from '../../../shared' | 20 | import { VideoRateType } from '../../../shared' |
16 | 21 | ||
17 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 22 | const 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 | ||
146 | function 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 | ||
143 | export { | 172 | export { |
@@ -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 | } |