diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-12-07 14:32:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 14:32:36 +0100 |
commit | 2d53be0267acc49cda46707b885096193a1f4e9c (patch) | |
tree | 887061a34bc67f40acbb96a6278f9544bf83caeb /server/middlewares/validators/redundancy.ts | |
parent | adc1f09c0dbd997f34028c1c82d1c118dc8ead80 (diff) | |
download | PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.gz PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.zst PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.zip |
replace numbers with typed http status codes (#3409)
Diffstat (limited to 'server/middlewares/validators/redundancy.ts')
-rw-r--r-- | server/middlewares/validators/redundancy.ts | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index 8cd3bc33d..6d2dd39c9 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts | |||
@@ -8,6 +8,7 @@ import { isHostValid } from '../../helpers/custom-validators/servers' | |||
8 | import { ServerModel } from '../../models/server/server' | 8 | import { ServerModel } from '../../models/server/server' |
9 | import { doesVideoExist } from '../../helpers/middlewares' | 9 | import { doesVideoExist } from '../../helpers/middlewares' |
10 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' | 10 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' |
11 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
11 | 12 | ||
12 | const videoFileRedundancyGetValidator = [ | 13 | const videoFileRedundancyGetValidator = [ |
13 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | 14 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
@@ -34,11 +35,11 @@ const videoFileRedundancyGetValidator = [ | |||
34 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) | 35 | return f.resolution === paramResolution && (!req.params.fps || paramFPS) |
35 | }) | 36 | }) |
36 | 37 | ||
37 | if (!videoFile) return res.status(404).json({ error: 'Video file not found.' }) | 38 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' }) |
38 | res.locals.videoFile = videoFile | 39 | res.locals.videoFile = videoFile |
39 | 40 | ||
40 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) | 41 | const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id) |
41 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) | 42 | if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) |
42 | res.locals.videoRedundancy = videoRedundancy | 43 | res.locals.videoRedundancy = videoRedundancy |
43 | 44 | ||
44 | return next() | 45 | return next() |
@@ -64,11 +65,11 @@ const videoPlaylistRedundancyGetValidator = [ | |||
64 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above | 65 | const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above |
65 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) | 66 | const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType) |
66 | 67 | ||
67 | if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' }) | 68 | if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' }) |
68 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist | 69 | res.locals.videoStreamingPlaylist = videoStreamingPlaylist |
69 | 70 | ||
70 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) | 71 | const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id) |
71 | if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' }) | 72 | if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' }) |
72 | res.locals.videoRedundancy = videoRedundancy | 73 | res.locals.videoRedundancy = videoRedundancy |
73 | 74 | ||
74 | return next() | 75 | return next() |
@@ -90,7 +91,7 @@ const updateServerRedundancyValidator = [ | |||
90 | 91 | ||
91 | if (!server) { | 92 | if (!server) { |
92 | return res | 93 | return res |
93 | .status(404) | 94 | .status(HttpStatusCode.NOT_FOUND_404) |
94 | .json({ | 95 | .json({ |
95 | error: `Server ${req.params.host} not found.` | 96 | error: `Server ${req.params.host} not found.` |
96 | }) | 97 | }) |
@@ -128,15 +129,15 @@ const addVideoRedundancyValidator = [ | |||
128 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return | 129 | if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return |
129 | 130 | ||
130 | if (res.locals.onlyVideo.remote === false) { | 131 | if (res.locals.onlyVideo.remote === false) { |
131 | return res.status(400) | 132 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
132 | .json({ error: 'Cannot create a redundancy on a local video' }) | 133 | .json({ error: 'Cannot create a redundancy on a local video' }) |
133 | .end() | 134 | .end() |
134 | } | 135 | } |
135 | 136 | ||
136 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) | 137 | const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid) |
137 | if (alreadyExists) { | 138 | if (alreadyExists) { |
138 | return res.status(409) | 139 | return res.status(HttpStatusCode.CONFLICT_409) |
139 | .json({ error: 'This video is already duplicated by your instance.' }) | 140 | .json({ error: 'This video is already duplicated by your instance.' }) |
140 | } | 141 | } |
141 | 142 | ||
142 | return next() | 143 | return next() |
@@ -155,7 +156,7 @@ const removeVideoRedundancyValidator = [ | |||
155 | 156 | ||
156 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) | 157 | const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) |
157 | if (!redundancy) { | 158 | if (!redundancy) { |
158 | return res.status(404) | 159 | return res.status(HttpStatusCode.NOT_FOUND_404) |
159 | .json({ error: 'Video redundancy not found' }) | 160 | .json({ error: 'Video redundancy not found' }) |
160 | .end() | 161 | .end() |
161 | } | 162 | } |