diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-23 10:40:39 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 3e753302d8c911b59971c16a8018df0e1ab78465 (patch) | |
tree | efce7ece3273589228c5c948ea6757b2bdf65429 /server/helpers/custom-validators | |
parent | a8b666e9f1ed002230869606308749614390c82f (diff) | |
download | PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.gz PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.zst PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.zip |
Refactor middleware helpers
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 40 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-abuses.ts | 16 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-blacklist.ts | 20 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-captions.ts | 21 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 21 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-playlists.ts | 18 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 73 |
7 files changed, 2 insertions, 207 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index 31a2de5ca..be196d2a4 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -1,7 +1,4 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { Response } from 'express' | ||
3 | import 'express-validator' | 1 | import 'express-validator' |
4 | import { AccountModel } from '../../models/account/account' | ||
5 | import { isUserDescriptionValid, isUserUsernameValid } from './users' | 2 | import { isUserDescriptionValid, isUserUsernameValid } from './users' |
6 | import { exists } from './misc' | 3 | import { exists } from './misc' |
7 | 4 | ||
@@ -17,47 +14,10 @@ function isAccountDescriptionValid (value: string) { | |||
17 | return isUserDescriptionValid(value) | 14 | return isUserDescriptionValid(value) |
18 | } | 15 | } |
19 | 16 | ||
20 | function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { | ||
21 | const promise = AccountModel.load(id) | ||
22 | |||
23 | return doesAccountExist(promise, res, sendNotFound) | ||
24 | } | ||
25 | |||
26 | function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | ||
27 | const promise = AccountModel.loadLocalByName(name) | ||
28 | |||
29 | return doesAccountExist(promise, res, sendNotFound) | ||
30 | } | ||
31 | |||
32 | function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | ||
33 | return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound) | ||
34 | } | ||
35 | |||
36 | async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) { | ||
37 | const account = await p | ||
38 | |||
39 | if (!account) { | ||
40 | if (sendNotFound === true) { | ||
41 | res.status(404) | ||
42 | .send({ error: 'Account not found' }) | ||
43 | .end() | ||
44 | } | ||
45 | |||
46 | return false | ||
47 | } | ||
48 | |||
49 | res.locals.account = account | ||
50 | |||
51 | return true | ||
52 | } | ||
53 | |||
54 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
55 | 18 | ||
56 | export { | 19 | export { |
57 | isAccountIdValid, | 20 | isAccountIdValid, |
58 | doesAccountIdExist, | ||
59 | doesLocalAccountNameExist, | ||
60 | isAccountDescriptionValid, | 21 | isAccountDescriptionValid, |
61 | doesAccountNameWithHostExist, | ||
62 | isAccountNameValid | 22 | isAccountNameValid |
63 | } | 23 | } |
diff --git a/server/helpers/custom-validators/video-abuses.ts b/server/helpers/custom-validators/video-abuses.ts index a61dcee1c..e43d12be8 100644 --- a/server/helpers/custom-validators/video-abuses.ts +++ b/server/helpers/custom-validators/video-abuses.ts | |||
@@ -18,25 +18,9 @@ function isVideoAbuseStateValid (value: string) { | |||
18 | return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined | 18 | return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined |
19 | } | 19 | } |
20 | 20 | ||
21 | async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) { | ||
22 | const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) | ||
23 | |||
24 | if (videoAbuse === null) { | ||
25 | res.status(404) | ||
26 | .json({ error: 'Video abuse not found' }) | ||
27 | .end() | ||
28 | |||
29 | return false | ||
30 | } | ||
31 | |||
32 | res.locals.videoAbuse = videoAbuse | ||
33 | return true | ||
34 | } | ||
35 | |||
36 | // --------------------------------------------------------------------------- | 21 | // --------------------------------------------------------------------------- |
37 | 22 | ||
38 | export { | 23 | export { |
39 | doesVideoAbuseExist, | ||
40 | isVideoAbuseStateValid, | 24 | isVideoAbuseStateValid, |
41 | isVideoAbuseReasonValid, | 25 | isVideoAbuseReasonValid, |
42 | isVideoAbuseModerationCommentValid | 26 | isVideoAbuseModerationCommentValid |
diff --git a/server/helpers/custom-validators/video-blacklist.ts b/server/helpers/custom-validators/video-blacklist.ts index 3743f7023..9a44332ef 100644 --- a/server/helpers/custom-validators/video-blacklist.ts +++ b/server/helpers/custom-validators/video-blacklist.ts | |||
@@ -1,8 +1,6 @@ | |||
1 | import { Response } from 'express' | ||
2 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
3 | import { exists } from './misc' | 2 | import { exists } from './misc' |
4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 3 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
5 | import { VideoBlacklistModel } from '../../models/video/video-blacklist' | ||
6 | import { VideoBlacklistType } from '../../../shared/models/videos' | 4 | import { VideoBlacklistType } from '../../../shared/models/videos' |
7 | 5 | ||
8 | const VIDEO_BLACKLIST_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_BLACKLIST | 6 | const VIDEO_BLACKLIST_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_BLACKLIST |
@@ -11,21 +9,6 @@ function isVideoBlacklistReasonValid (value: string) { | |||
11 | return value === null || validator.isLength(value, VIDEO_BLACKLIST_CONSTRAINTS_FIELDS.REASON) | 9 | return value === null || validator.isLength(value, VIDEO_BLACKLIST_CONSTRAINTS_FIELDS.REASON) |
12 | } | 10 | } |
13 | 11 | ||
14 | async function doesVideoBlacklistExist (videoId: number, res: Response) { | ||
15 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) | ||
16 | |||
17 | if (videoBlacklist === null) { | ||
18 | res.status(404) | ||
19 | .json({ error: 'Blacklisted video not found' }) | ||
20 | .end() | ||
21 | |||
22 | return false | ||
23 | } | ||
24 | |||
25 | res.locals.videoBlacklist = videoBlacklist | ||
26 | return true | ||
27 | } | ||
28 | |||
29 | function isVideoBlacklistTypeValid (value: any) { | 12 | function isVideoBlacklistTypeValid (value: any) { |
30 | return exists(value) && validator.isInt('' + value) && VideoBlacklistType[value] !== undefined | 13 | return exists(value) && validator.isInt('' + value) && VideoBlacklistType[value] !== undefined |
31 | } | 14 | } |
@@ -34,6 +17,5 @@ function isVideoBlacklistTypeValid (value: any) { | |||
34 | 17 | ||
35 | export { | 18 | export { |
36 | isVideoBlacklistReasonValid, | 19 | isVideoBlacklistReasonValid, |
37 | isVideoBlacklistTypeValid, | 20 | isVideoBlacklistTypeValid |
38 | doesVideoBlacklistExist | ||
39 | } | 21 | } |
diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts index 3b6569a8a..d06eb3695 100644 --- a/server/helpers/custom-validators/video-captions.ts +++ b/server/helpers/custom-validators/video-captions.ts | |||
@@ -1,8 +1,5 @@ | |||
1 | import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' | 1 | import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' |
2 | import { exists, isFileValid } from './misc' | 2 | import { exists, isFileValid } from './misc' |
3 | import { Response } from 'express' | ||
4 | import { VideoModel } from '../../models/video/video' | ||
5 | import { VideoCaptionModel } from '../../models/video/video-caption' | ||
6 | 3 | ||
7 | function isVideoCaptionLanguageValid (value: any) { | 4 | function isVideoCaptionLanguageValid (value: any) { |
8 | return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined | 5 | return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined |
@@ -16,25 +13,9 @@ function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File | |||
16 | return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) | 13 | return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) |
17 | } | 14 | } |
18 | 15 | ||
19 | async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) { | ||
20 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) | ||
21 | |||
22 | if (!videoCaption) { | ||
23 | res.status(404) | ||
24 | .json({ error: 'Video caption not found' }) | ||
25 | .end() | ||
26 | |||
27 | return false | ||
28 | } | ||
29 | |||
30 | res.locals.videoCaption = videoCaption | ||
31 | return true | ||
32 | } | ||
33 | |||
34 | // --------------------------------------------------------------------------- | 16 | // --------------------------------------------------------------------------- |
35 | 17 | ||
36 | export { | 18 | export { |
37 | isVideoCaptionFile, | 19 | isVideoCaptionFile, |
38 | isVideoCaptionLanguageValid, | 20 | isVideoCaptionLanguageValid |
39 | doesVideoCaptionExist | ||
40 | } | 21 | } |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index f818ce8f1..0471f6ec4 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -20,33 +20,12 @@ function isVideoChannelSupportValid (value: string) { | |||
20 | return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) | 20 | return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) |
21 | } | 21 | } |
22 | 22 | ||
23 | async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { | ||
24 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | ||
25 | |||
26 | return processVideoChannelExist(videoChannel, res) | ||
27 | } | ||
28 | |||
29 | async function doesVideoChannelIdExist (id: number, res: express.Response) { | ||
30 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) | ||
31 | |||
32 | return processVideoChannelExist(videoChannel, res) | ||
33 | } | ||
34 | |||
35 | async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { | ||
36 | const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) | ||
37 | |||
38 | return processVideoChannelExist(videoChannel, res) | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | 23 | // --------------------------------------------------------------------------- |
42 | 24 | ||
43 | export { | 25 | export { |
44 | doesVideoChannelNameWithHostExist, | ||
45 | doesLocalVideoChannelNameExist, | ||
46 | isVideoChannelDescriptionValid, | 26 | isVideoChannelDescriptionValid, |
47 | isVideoChannelNameValid, | 27 | isVideoChannelNameValid, |
48 | isVideoChannelSupportValid, | 28 | isVideoChannelSupportValid, |
49 | doesVideoChannelIdExist | ||
50 | } | 29 | } |
51 | 30 | ||
52 | function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { | 31 | function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { |
diff --git a/server/helpers/custom-validators/video-playlists.ts b/server/helpers/custom-validators/video-playlists.ts index 2fe426560..60125dcda 100644 --- a/server/helpers/custom-validators/video-playlists.ts +++ b/server/helpers/custom-validators/video-playlists.ts | |||
@@ -26,27 +26,9 @@ function isVideoPlaylistTypeValid (value: any) { | |||
26 | return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined | 26 | return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined |
27 | } | 27 | } |
28 | 28 | ||
29 | async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') { | ||
30 | const videoPlaylist = fetchType === 'summary' | ||
31 | ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) | ||
32 | : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) | ||
33 | |||
34 | if (!videoPlaylist) { | ||
35 | res.status(404) | ||
36 | .json({ error: 'Video playlist not found' }) | ||
37 | .end() | ||
38 | |||
39 | return false | ||
40 | } | ||
41 | |||
42 | res.locals.videoPlaylist = videoPlaylist | ||
43 | return true | ||
44 | } | ||
45 | |||
46 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
47 | 30 | ||
48 | export { | 31 | export { |
49 | doesVideoPlaylistExist, | ||
50 | isVideoPlaylistNameValid, | 32 | isVideoPlaylistNameValid, |
51 | isVideoPlaylistDescriptionValid, | 33 | isVideoPlaylistDescriptionValid, |
52 | isVideoPlaylistPrivacyValid, | 34 | isVideoPlaylistPrivacyValid, |
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 214db17a1..157e1a8e3 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -15,10 +15,8 @@ import { | |||
15 | } from '../../initializers/constants' | 15 | } from '../../initializers/constants' |
16 | import { VideoModel } from '../../models/video/video' | 16 | import { VideoModel } from '../../models/video/video' |
17 | import { exists, isArray, isDateValid, isFileValid } from './misc' | 17 | import { exists, isArray, isDateValid, isFileValid } from './misc' |
18 | import { VideoChannelModel } from '../../models/video/video-channel' | ||
19 | import { UserModel } from '../../models/account/user' | 18 | import { UserModel } from '../../models/account/user' |
20 | import * as magnetUtil from 'magnet-uri' | 19 | import * as magnetUtil from 'magnet-uri' |
21 | import { fetchVideo, VideoFetchType } from '../video' | ||
22 | 20 | ||
23 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 21 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS |
24 | 22 | ||
@@ -143,79 +141,10 @@ function isVideoMagnetUriValid (value: string) { | |||
143 | return parsed && isVideoFileInfoHashValid(parsed.infoHash) | 141 | return parsed && isVideoFileInfoHashValid(parsed.infoHash) |
144 | } | 142 | } |
145 | 143 | ||
146 | function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { | ||
147 | // Retrieve the user who did the request | ||
148 | if (video.isOwned() === false) { | ||
149 | res.status(403) | ||
150 | .json({ error: 'Cannot manage a video of another server.' }) | ||
151 | .end() | ||
152 | return false | ||
153 | } | ||
154 | |||
155 | // Check if the user can delete the video | ||
156 | // The user can delete it if he has the right | ||
157 | // Or if s/he is the video's account | ||
158 | const account = video.VideoChannel.Account | ||
159 | if (user.hasRight(right) === false && account.userId !== user.id) { | ||
160 | res.status(403) | ||
161 | .json({ error: 'Cannot manage a video of another user.' }) | ||
162 | .end() | ||
163 | return false | ||
164 | } | ||
165 | |||
166 | return true | ||
167 | } | ||
168 | |||
169 | async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { | ||
170 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined | ||
171 | |||
172 | const video = await fetchVideo(id, fetchType, userId) | ||
173 | |||
174 | if (video === null) { | ||
175 | res.status(404) | ||
176 | .json({ error: 'Video not found' }) | ||
177 | .end() | ||
178 | |||
179 | return false | ||
180 | } | ||
181 | |||
182 | if (fetchType !== 'none') res.locals.video = video | ||
183 | return true | ||
184 | } | ||
185 | |||
186 | async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { | ||
187 | if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { | ||
188 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) | ||
189 | if (videoChannel === null) { | ||
190 | res.status(400) | ||
191 | .json({ error: 'Unknown video `video channel` on this instance.' }) | ||
192 | .end() | ||
193 | |||
194 | return false | ||
195 | } | ||
196 | |||
197 | res.locals.videoChannel = videoChannel | ||
198 | return true | ||
199 | } | ||
200 | |||
201 | const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) | ||
202 | if (videoChannel === null) { | ||
203 | res.status(400) | ||
204 | .json({ error: 'Unknown video `video channel` for this account.' }) | ||
205 | .end() | ||
206 | |||
207 | return false | ||
208 | } | ||
209 | |||
210 | res.locals.videoChannel = videoChannel | ||
211 | return true | ||
212 | } | ||
213 | |||
214 | // --------------------------------------------------------------------------- | 144 | // --------------------------------------------------------------------------- |
215 | 145 | ||
216 | export { | 146 | export { |
217 | isVideoCategoryValid, | 147 | isVideoCategoryValid, |
218 | checkUserCanManageVideo, | ||
219 | isVideoLicenceValid, | 148 | isVideoLicenceValid, |
220 | isVideoLanguageValid, | 149 | isVideoLanguageValid, |
221 | isVideoTruncatedDescriptionValid, | 150 | isVideoTruncatedDescriptionValid, |
@@ -237,9 +166,7 @@ export { | |||
237 | isVideoPrivacyValid, | 166 | isVideoPrivacyValid, |
238 | isVideoFileResolutionValid, | 167 | isVideoFileResolutionValid, |
239 | isVideoFileSizeValid, | 168 | isVideoFileSizeValid, |
240 | doesVideoExist, | ||
241 | isVideoImage, | 169 | isVideoImage, |
242 | doesVideoChannelOfAccountExist, | ||
243 | isVideoSupportValid, | 170 | isVideoSupportValid, |
244 | isVideoFilterValid | 171 | isVideoFilterValid |
245 | } | 172 | } |