aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/videos.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-10 10:02:18 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-10 10:18:16 +0200
commit35bf0c83c80f59ca79f4b84fac8700f17adeb22d (patch)
treea9b2106096d6ba04d7219051b17fd32cfbe6a885 /server/helpers/custom-validators/videos.ts
parent769d332177a5b02d5c2ffc134687d3b4ed65bae9 (diff)
downloadPeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.gz
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.tar.zst
PeerTube-35bf0c83c80f59ca79f4b84fac8700f17adeb22d.zip
Video blacklist refractoring
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}