aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-31 15:09:34 +0200
committerChocobozzz <me@florianbigard.com>2018-07-31 15:09:34 +0200
commitcf7a61b5a2b68fd966c4a355e37e84b048ed296b (patch)
tree4f4867344fe6856cb4f6b18856867c6e34171ea2 /server/middlewares/validators
parentc487d3033cad9c5e0f85ae49ed3d2a7b6c2711d8 (diff)
downloadPeerTube-cf7a61b5a2b68fd966c4a355e37e84b048ed296b.tar.gz
PeerTube-cf7a61b5a2b68fd966c4a355e37e84b048ed296b.tar.zst
PeerTube-cf7a61b5a2b68fd966c4a355e37e84b048ed296b.zip
Cleanup req files on bad request
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/avatar.ts3
-rw-r--r--server/middlewares/validators/video-captions.ts7
-rw-r--r--server/middlewares/validators/video-channels.ts4
-rw-r--r--server/middlewares/validators/videos.ts22
4 files changed, 19 insertions, 17 deletions
diff --git a/server/middlewares/validators/avatar.ts b/server/middlewares/validators/avatar.ts
index f346ea92f..5860735c6 100644
--- a/server/middlewares/validators/avatar.ts
+++ b/server/middlewares/validators/avatar.ts
@@ -4,6 +4,7 @@ import { isAvatarFile } from '../../helpers/custom-validators/users'
4import { areValidationErrors } from './utils' 4import { areValidationErrors } from './utils'
5import { CONSTRAINTS_FIELDS } from '../../initializers' 5import { CONSTRAINTS_FIELDS } from '../../initializers'
6import { logger } from '../../helpers/logger' 6import { logger } from '../../helpers/logger'
7import { cleanUpReqFiles } from '../../helpers/utils'
7 8
8const updateAvatarValidator = [ 9const updateAvatarValidator = [
9 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage( 10 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage(
@@ -14,7 +15,7 @@ const updateAvatarValidator = [
14 (req: express.Request, res: express.Response, next: express.NextFunction) => { 15 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking updateAvatarValidator parameters', { files: req.files }) 16 logger.debug('Checking updateAvatarValidator parameters', { files: req.files })
16 17
17 if (areValidationErrors(req, res)) return 18 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
18 19
19 return next() 20 return next()
20 } 21 }
diff --git a/server/middlewares/validators/video-captions.ts b/server/middlewares/validators/video-captions.ts
index b6d92d380..18d537bc4 100644
--- a/server/middlewares/validators/video-captions.ts
+++ b/server/middlewares/validators/video-captions.ts
@@ -7,6 +7,7 @@ import { CONSTRAINTS_FIELDS } from '../../initializers'
7import { UserRight } from '../../../shared' 7import { UserRight } from '../../../shared'
8import { logger } from '../../helpers/logger' 8import { logger } from '../../helpers/logger'
9import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' 9import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
10import { cleanUpReqFiles } from '../../helpers/utils'
10 11
11const addVideoCaptionValidator = [ 12const addVideoCaptionValidator = [
12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), 13 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
@@ -20,12 +21,12 @@ const addVideoCaptionValidator = [
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking addVideoCaption parameters', { parameters: req.body }) 22 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
22 23
23 if (areValidationErrors(req, res)) return 24 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
24 if (!await isVideoExist(req.params.videoId, res)) return 25 if (!await isVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
25 26
26 // Check if the user who did the request is able to update the video 27 // Check if the user who did the request is able to update the video
27 const user = res.locals.oauth.token.User 28 const user = res.locals.oauth.token.User
28 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return 29 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
29 30
30 return next() 31 return next()
31 } 32 }
diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts
index 7f65f7290..143ce9582 100644
--- a/server/middlewares/validators/video-channels.ts
+++ b/server/middlewares/validators/video-channels.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param } from 'express-validator/check' 2import { body, param } from 'express-validator/check'
3import { UserRight } from '../../../shared' 3import { UserRight } from '../../../shared'
4import { isAccountIdExist, isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' 4import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' 5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6import { 6import {
7 isVideoChannelDescriptionValid, 7 isVideoChannelDescriptionValid,
@@ -13,8 +13,6 @@ import { logger } from '../../helpers/logger'
13import { UserModel } from '../../models/account/user' 13import { UserModel } from '../../models/account/user'
14import { VideoChannelModel } from '../../models/video/video-channel' 14import { VideoChannelModel } from '../../models/video/video-channel'
15import { areValidationErrors } from './utils' 15import { areValidationErrors } from './utils'
16import { isAvatarFile } from '../../helpers/custom-validators/users'
17import { CONSTRAINTS_FIELDS } from '../../initializers'
18 16
19const listVideoAccountChannelsValidator = [ 17const listVideoAccountChannelsValidator = [
20 param('accountName').exists().withMessage('Should have a valid account name'), 18 param('accountName').exists().withMessage('Should have a valid account name'),
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index d9af2aa0a..9357c1e39 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -35,6 +35,7 @@ import { CONSTRAINTS_FIELDS } from '../../initializers'
35import { VideoShareModel } from '../../models/video/video-share' 35import { VideoShareModel } from '../../models/video/video-share'
36import { authenticate } from '../oauth' 36import { authenticate } from '../oauth'
37import { areValidationErrors } from './utils' 37import { areValidationErrors } from './utils'
38import { cleanUpReqFiles } from '../../helpers/utils'
38 39
39const videosAddValidator = getCommonVideoAttributes().concat([ 40const videosAddValidator = getCommonVideoAttributes().concat([
40 body('videofile') 41 body('videofile')
@@ -50,13 +51,13 @@ const videosAddValidator = getCommonVideoAttributes().concat([
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) 52 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
52 53
53 if (areValidationErrors(req, res)) return 54 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
54 if (areErrorsInScheduleUpdate(req, res)) return 55 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
55 56
56 const videoFile: Express.Multer.File = req.files['videofile'][0] 57 const videoFile: Express.Multer.File = req.files['videofile'][0]
57 const user = res.locals.oauth.token.User 58 const user = res.locals.oauth.token.User
58 59
59 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return 60 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
60 61
61 const isAble = await user.isAbleToUploadVideo(videoFile) 62 const isAble = await user.isAbleToUploadVideo(videoFile)
62 if (isAble === false) { 63 if (isAble === false) {
@@ -64,7 +65,7 @@ const videosAddValidator = getCommonVideoAttributes().concat([
64 .json({ error: 'The user video quota is exceeded with this video.' }) 65 .json({ error: 'The user video quota is exceeded with this video.' })
65 .end() 66 .end()
66 67
67 return 68 return cleanUpReqFiles(req)
68 } 69 }
69 70
70 let duration: number 71 let duration: number
@@ -77,7 +78,7 @@ const videosAddValidator = getCommonVideoAttributes().concat([
77 .json({ error: 'Invalid input file.' }) 78 .json({ error: 'Invalid input file.' })
78 .end() 79 .end()
79 80
80 return 81 return cleanUpReqFiles(req)
81 } 82 }
82 83
83 videoFile['duration'] = duration 84 videoFile['duration'] = duration
@@ -99,23 +100,24 @@ const videosUpdateValidator = getCommonVideoAttributes().concat([
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 100 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videosUpdate parameters', { parameters: req.body }) 101 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
101 102
102 if (areValidationErrors(req, res)) return 103 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
103 if (areErrorsInScheduleUpdate(req, res)) return 104 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
104 if (!await isVideoExist(req.params.id, res)) return 105 if (!await isVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
105 106
106 const video = res.locals.video 107 const video = res.locals.video
107 108
108 // Check if the user who did the request is able to update the video 109 // Check if the user who did the request is able to update the video
109 const user = res.locals.oauth.token.User 110 const user = res.locals.oauth.token.User
110 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return 111 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
111 112
112 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) { 113 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
114 cleanUpReqFiles(req)
113 return res.status(409) 115 return res.status(409)
114 .json({ error: 'Cannot set "private" a video that was not private.' }) 116 .json({ error: 'Cannot set "private" a video that was not private.' })
115 .end() 117 .end()
116 } 118 }
117 119
118 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return 120 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
119 121
120 return next() 122 return next()
121 } 123 }