aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-08 21:16:10 +0100
committerGitHub <noreply@github.com>2020-12-08 21:16:10 +0100
commitf2eb23cd87cf32b8fe545178143b5f49e06a58da (patch)
treeaf7d59945af70e28fd85047e2c688c59a908f548 /server/middlewares
parentc977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff)
downloadPeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.gz
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.zst
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.zip
emit more specific status codes on video upload (#3423)
- reduce http status codes list to potentially useful codes - convert more codes to typed ones - factorize html generator for error responses
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/cache.ts6
-rw-r--r--server/middlewares/validators/videos/videos.ts41
2 files changed, 36 insertions, 11 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts
index cb24d9e0e..0708ee8e8 100644
--- a/server/middlewares/cache.ts
+++ b/server/middlewares/cache.ts
@@ -1,5 +1,6 @@
1import { Redis } from '../lib/redis' 1import { Redis } from '../lib/redis'
2import * as apicache from 'apicache' 2import * as apicache from 'apicache'
3import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
3 4
4// Ensure Redis is initialized 5// Ensure Redis is initialized
5Redis.Instance.init() 6Redis.Instance.init()
@@ -8,7 +9,10 @@ const defaultOptions = {
8 redisClient: Redis.Instance.getClient(), 9 redisClient: Redis.Instance.getClient(),
9 appendKey: () => Redis.Instance.getPrefix(), 10 appendKey: () => Redis.Instance.getPrefix(),
10 statusCodes: { 11 statusCodes: {
11 exclude: [ 404, 403 ] 12 exclude: [
13 HttpStatusCode.FORBIDDEN_403,
14 HttpStatusCode.NOT_FOUND_404
15 ]
12 } 16 }
13} 17}
14 18
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts
index 9834f714b..8bc37b0ab 100644
--- a/server/middlewares/validators/videos/videos.ts
+++ b/server/middlewares/validators/videos/videos.ts
@@ -8,6 +8,7 @@ import { VideoChangeOwnershipAccept } from '../../../../shared/models/videos/vid
8import { 8import {
9 isBooleanValid, 9 isBooleanValid,
10 isDateValid, 10 isDateValid,
11 isFileFieldValid,
11 isIdOrUUIDValid, 12 isIdOrUUIDValid,
12 isIdValid, 13 isIdValid,
13 isUUIDValid, 14 isUUIDValid,
@@ -22,7 +23,8 @@ import {
22 isScheduleVideoUpdatePrivacyValid, 23 isScheduleVideoUpdatePrivacyValid,
23 isVideoCategoryValid, 24 isVideoCategoryValid,
24 isVideoDescriptionValid, 25 isVideoDescriptionValid,
25 isVideoFile, 26 isVideoFileMimeTypeValid,
27 isVideoFileSizeValid,
26 isVideoFilterValid, 28 isVideoFilterValid,
27 isVideoImage, 29 isVideoImage,
28 isVideoLanguageValid, 30 isVideoLanguageValid,
@@ -55,11 +57,11 @@ import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-c
55 57
56const videosAddValidator = getCommonVideoEditAttributes().concat([ 58const videosAddValidator = getCommonVideoEditAttributes().concat([
57 body('videofile') 59 body('videofile')
58 .custom((value, { req }) => isVideoFile(req.files)).withMessage( 60 .custom((value, { req }) => isFileFieldValid(req.files, 'videofile'))
59 'This file is not supported or too large. Please, make sure it is of the following type: ' + 61 .withMessage('Should have a file'),
60 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ') 62 body('name')
61 ), 63 .custom(isVideoNameValid)
62 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), 64 .withMessage('Should have a valid name'),
63 body('channelId') 65 body('channelId')
64 .customSanitizer(toIntOrNull) 66 .customSanitizer(toIntOrNull)
65 .custom(isIdValid).withMessage('Should have correct video channel id'), 67 .custom(isIdValid).withMessage('Should have correct video channel id'),
@@ -75,8 +77,27 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([
75 77
76 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) 78 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
77 79
80 if (!isVideoFileMimeTypeValid(req.files)) {
81 res.status(HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415)
82 .json({
83 error: 'This file is not supported. Please, make sure it is of the following type: ' +
84 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
85 })
86
87 return cleanUpReqFiles(req)
88 }
89
90 if (!isVideoFileSizeValid(videoFile.size.toString())) {
91 res.status(HttpStatusCode.PAYLOAD_TOO_LARGE_413)
92 .json({
93 error: 'This file is too large.'
94 })
95
96 return cleanUpReqFiles(req)
97 }
98
78 if (await isAbleToUploadVideo(user.id, videoFile.size) === false) { 99 if (await isAbleToUploadVideo(user.id, videoFile.size) === false) {
79 res.status(HttpStatusCode.FORBIDDEN_403) 100 res.status(HttpStatusCode.PAYLOAD_TOO_LARGE_413)
80 .json({ error: 'The user video quota is exceeded with this video.' }) 101 .json({ error: 'The user video quota is exceeded with this video.' })
81 102
82 return cleanUpReqFiles(req) 103 return cleanUpReqFiles(req)
@@ -88,8 +109,8 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([
88 duration = await getDurationFromVideoFile(videoFile.path) 109 duration = await getDurationFromVideoFile(videoFile.path)
89 } catch (err) { 110 } catch (err) {
90 logger.error('Invalid input file in videosAddValidator.', { err }) 111 logger.error('Invalid input file in videosAddValidator.', { err })
91 res.status(HttpStatusCode.BAD_REQUEST_400) 112 res.status(HttpStatusCode.UNPROCESSABLE_ENTITY_422)
92 .json({ error: 'Invalid input file.' }) 113 .json({ error: 'Video file unreadable.' })
93 114
94 return cleanUpReqFiles(req) 115 return cleanUpReqFiles(req)
95 } 116 }
@@ -295,7 +316,7 @@ const videosAcceptChangeOwnershipValidator = [
295 const videoChangeOwnership = res.locals.videoChangeOwnership 316 const videoChangeOwnership = res.locals.videoChangeOwnership
296 const isAble = await isAbleToUploadVideo(user.id, videoChangeOwnership.Video.getMaxQualityFile().size) 317 const isAble = await isAbleToUploadVideo(user.id, videoChangeOwnership.Video.getMaxQualityFile().size)
297 if (isAble === false) { 318 if (isAble === false) {
298 res.status(HttpStatusCode.FORBIDDEN_403) 319 res.status(HttpStatusCode.PAYLOAD_TOO_LARGE_413)
299 .json({ error: 'The user video quota is exceeded with this video.' }) 320 .json({ error: 'The user video quota is exceeded with this video.' })
300 321
301 return 322 return