diff options
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 38 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 33 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 24 |
3 files changed, 75 insertions, 20 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index fe0fc650a..a6d7f2b82 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import * as Promise from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import 'express-validator' | 3 | import 'express-validator' |
4 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
@@ -11,33 +11,45 @@ function isAccountNameValid (value: string) { | |||
11 | return isUserUsernameValid(value) | 11 | return isUserUsernameValid(value) |
12 | } | 12 | } |
13 | 13 | ||
14 | function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) { | 14 | function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { |
15 | let promise: Promise<AccountInstance> | 15 | let promise: Bluebird<AccountInstance> |
16 | if (validator.isInt(id)) { | 16 | |
17 | if (validator.isInt('' + id)) { | ||
17 | promise = db.Account.load(+id) | 18 | promise = db.Account.load(+id) |
18 | } else { // UUID | 19 | } else { // UUID |
19 | promise = db.Account.loadByUUID(id) | 20 | promise = db.Account.loadByUUID('' + id) |
20 | } | 21 | } |
21 | 22 | ||
22 | promise.then(account => { | 23 | return checkAccountExists(promise, res, callback) |
24 | } | ||
25 | |||
26 | function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | ||
27 | const p = db.Account.loadLocalByName(name) | ||
28 | |||
29 | return checkAccountExists(p, res, callback) | ||
30 | } | ||
31 | |||
32 | function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | ||
33 | p.then(account => { | ||
23 | if (!account) { | 34 | if (!account) { |
24 | return res.status(404) | 35 | return res.status(404) |
25 | .json({ error: 'Video account not found' }) | 36 | .send({ error: 'Account not found' }) |
26 | .end() | 37 | .end() |
27 | } | 38 | } |
28 | 39 | ||
29 | res.locals.account = account | 40 | res.locals.account = account |
30 | callback() | 41 | return callback(null, account) |
31 | }) | ||
32 | .catch(err => { | ||
33 | logger.error('Error in video account request validator.', err) | ||
34 | return res.sendStatus(500) | ||
35 | }) | 42 | }) |
43 | .catch(err => { | ||
44 | logger.error('Error in account request validator.', err) | ||
45 | return res.sendStatus(500) | ||
46 | }) | ||
36 | } | 47 | } |
37 | 48 | ||
38 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
39 | 50 | ||
40 | export { | 51 | export { |
41 | checkVideoAccountExists, | 52 | checkAccountIdExists, |
53 | checkLocalAccountNameExists, | ||
42 | isAccountNameValid | 54 | isAccountNameValid |
43 | } | 55 | } |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 5de01f74b..267d987fc 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,14 +1,14 @@ | |||
1 | import * as Promise from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as validator from 'validator' | ||
3 | import * as express from 'express' | 2 | import * as express from 'express' |
4 | import 'express-validator' | 3 | import 'express-validator' |
5 | import 'multer' | 4 | import 'multer' |
5 | import * as validator from 'validator' | ||
6 | 6 | ||
7 | import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' | 7 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' |
8 | import { VideoChannelInstance } from '../../models' | 8 | import { VideoChannelInstance } from '../../models' |
9 | import { logger } from '../logger' | 9 | import { logger } from '../logger' |
10 | import { exists } from './misc' | ||
11 | import { isActivityPubUrlValid } from './index' | 10 | import { isActivityPubUrlValid } from './index' |
11 | import { exists } from './misc' | ||
12 | 12 | ||
13 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 13 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
14 | 14 | ||
@@ -25,7 +25,7 @@ function isVideoChannelNameValid (value: string) { | |||
25 | } | 25 | } |
26 | 26 | ||
27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { | 27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { |
28 | let promise: Promise<VideoChannelInstance> | 28 | let promise: Bluebird<VideoChannelInstance> |
29 | if (validator.isInt(id)) { | 29 | if (validator.isInt(id)) { |
30 | promise = db.VideoChannel.loadAndPopulateAccount(+id) | 30 | promise = db.VideoChannel.loadAndPopulateAccount(+id) |
31 | } else { // UUID | 31 | } else { // UUID |
@@ -48,11 +48,32 @@ function checkVideoChannelExists (id: string, res: express.Response, callback: ( | |||
48 | }) | 48 | }) |
49 | } | 49 | } |
50 | 50 | ||
51 | async function isVideoChannelExistsPromise (id: string, res: express.Response) { | ||
52 | let videoChannel: VideoChannelInstance | ||
53 | if (validator.isInt(id)) { | ||
54 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) | ||
55 | } else { // UUID | ||
56 | videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id) | ||
57 | } | ||
58 | |||
59 | if (!videoChannel) { | ||
60 | res.status(404) | ||
61 | .json({ error: 'Video channel not found' }) | ||
62 | .end() | ||
63 | |||
64 | return false | ||
65 | } | ||
66 | |||
67 | res.locals.videoChannel = videoChannel | ||
68 | return true | ||
69 | } | ||
70 | |||
51 | // --------------------------------------------------------------------------- | 71 | // --------------------------------------------------------------------------- |
52 | 72 | ||
53 | export { | 73 | export { |
54 | isVideoChannelDescriptionValid, | 74 | isVideoChannelDescriptionValid, |
55 | isVideoChannelNameValid, | ||
56 | checkVideoChannelExists, | 75 | checkVideoChannelExists, |
76 | isVideoChannelNameValid, | ||
77 | isVideoChannelExistsPromise, | ||
57 | isVideoChannelUrlValid | 78 | isVideoChannelUrlValid |
58 | } | 79 | } |
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 205d8c62f..276354626 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -130,6 +130,27 @@ function checkVideoExists (id: string, res: Response, callback: () => void) { | |||
130 | }) | 130 | }) |
131 | } | 131 | } |
132 | 132 | ||
133 | async function isVideoExistsPromise (id: string, res: Response) { | ||
134 | let video: VideoInstance | ||
135 | |||
136 | if (validator.isInt(id)) { | ||
137 | video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id) | ||
138 | } else { // UUID | ||
139 | video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) | ||
140 | } | ||
141 | |||
142 | if (!video) { | ||
143 | res.status(404) | ||
144 | .json({ error: 'Video not found' }) | ||
145 | .end() | ||
146 | |||
147 | return false | ||
148 | } | ||
149 | |||
150 | res.locals.video = video | ||
151 | return true | ||
152 | } | ||
153 | |||
133 | // --------------------------------------------------------------------------- | 154 | // --------------------------------------------------------------------------- |
134 | 155 | ||
135 | export { | 156 | export { |
@@ -152,5 +173,6 @@ export { | |||
152 | isVideoPrivacyValid, | 173 | isVideoPrivacyValid, |
153 | isVideoFileResolutionValid, | 174 | isVideoFileResolutionValid, |
154 | isVideoFileSizeValid, | 175 | isVideoFileSizeValid, |
155 | checkVideoExists | 176 | checkVideoExists, |
177 | isVideoExistsPromise | ||
156 | } | 178 | } |