diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 17:30:46 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:43:01 +0100 |
commit | a2431b7dcbc72c05101dcdbe631ff84a823aeb51 (patch) | |
tree | 09278a822905622a70ff976a75e09d99bc45639a /server/helpers | |
parent | fcaf1e0aa84213a1b1f1b1a44a3276eae35ebe70 (diff) | |
download | PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.gz PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.zst PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.zip |
Refractor validators
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 46 | ||||
-rw-r--r-- | server/helpers/custom-validators/activitypub/account.ts | 63 | ||||
-rw-r--r-- | server/helpers/custom-validators/activitypub/videos.ts | 13 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 38 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 39 |
5 files changed, 41 insertions, 158 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index a6d7f2b82..e3c477414 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -1,17 +1,16 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import * as express from 'express' | 2 | import { Response } from 'express' |
3 | import 'express-validator' | 3 | import 'express-validator' |
4 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
5 | import { database as db } from '../../initializers' | 5 | import { database as db } from '../../initializers' |
6 | import { AccountInstance } from '../../models' | 6 | import { AccountInstance } from '../../models' |
7 | import { logger } from '../logger' | ||
8 | import { isUserUsernameValid } from './users' | 7 | import { isUserUsernameValid } from './users' |
9 | 8 | ||
10 | function isAccountNameValid (value: string) { | 9 | function isAccountNameValid (value: string) { |
11 | return isUserUsernameValid(value) | 10 | return isUserUsernameValid(value) |
12 | } | 11 | } |
13 | 12 | ||
14 | function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 13 | function isAccountIdExist (id: number | string, res: Response) { |
15 | let promise: Bluebird<AccountInstance> | 14 | let promise: Bluebird<AccountInstance> |
16 | 15 | ||
17 | if (validator.isInt('' + id)) { | 16 | if (validator.isInt('' + id)) { |
@@ -20,36 +19,35 @@ function checkAccountIdExists (id: number | string, res: express.Response, callb | |||
20 | promise = db.Account.loadByUUID('' + id) | 19 | promise = db.Account.loadByUUID('' + id) |
21 | } | 20 | } |
22 | 21 | ||
23 | return checkAccountExists(promise, res, callback) | 22 | return isAccountExist(promise, res) |
24 | } | 23 | } |
25 | 24 | ||
26 | function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 25 | function isLocalAccountNameExist (name: string, res: Response) { |
27 | const p = db.Account.loadLocalByName(name) | 26 | const promise = db.Account.loadLocalByName(name) |
28 | 27 | ||
29 | return checkAccountExists(p, res, callback) | 28 | return isAccountExist(promise, res) |
30 | } | 29 | } |
31 | 30 | ||
32 | function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) { | 31 | async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) { |
33 | p.then(account => { | 32 | const account = await p |
34 | if (!account) { | 33 | |
35 | return res.status(404) | 34 | if (!account) { |
36 | .send({ error: 'Account not found' }) | 35 | res.status(404) |
37 | .end() | 36 | .send({ error: 'Account not found' }) |
38 | } | 37 | .end() |
39 | 38 | ||
40 | res.locals.account = account | 39 | return false |
41 | return callback(null, account) | 40 | } |
42 | }) | 41 | |
43 | .catch(err => { | 42 | res.locals.account = account |
44 | logger.error('Error in account request validator.', err) | 43 | |
45 | return res.sendStatus(500) | 44 | return true |
46 | }) | ||
47 | } | 45 | } |
48 | 46 | ||
49 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
50 | 48 | ||
51 | export { | 49 | export { |
52 | checkAccountIdExists, | 50 | isAccountIdExist, |
53 | checkLocalAccountNameExists, | 51 | isLocalAccountNameExist, |
54 | isAccountNameValid | 52 | isAccountNameValid |
55 | } | 53 | } |
diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts index 645f55a5a..cab39a654 100644 --- a/server/helpers/custom-validators/activitypub/account.ts +++ b/server/helpers/custom-validators/activitypub/account.ts | |||
@@ -5,31 +5,19 @@ import { exists, isUUIDValid } from '../misc' | |||
5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | 5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' |
6 | 6 | ||
7 | function isAccountEndpointsObjectValid (endpointObject: any) { | 7 | function isAccountEndpointsObjectValid (endpointObject: any) { |
8 | return isAccountSharedInboxValid(endpointObject.sharedInbox) | 8 | return isActivityPubUrlValid(endpointObject.sharedInbox) |
9 | } | ||
10 | |||
11 | function isAccountSharedInboxValid (sharedInbox: string) { | ||
12 | return isActivityPubUrlValid(sharedInbox) | ||
13 | } | 9 | } |
14 | 10 | ||
15 | function isAccountPublicKeyObjectValid (publicKeyObject: any) { | 11 | function isAccountPublicKeyObjectValid (publicKeyObject: any) { |
16 | return isAccountPublicKeyIdValid(publicKeyObject.id) && | 12 | return isActivityPubUrlValid(publicKeyObject.id) && |
17 | isAccountPublicKeyOwnerValid(publicKeyObject.owner) && | 13 | isActivityPubUrlValid(publicKeyObject.owner) && |
18 | isAccountPublicKeyValid(publicKeyObject.publicKeyPem) | 14 | isAccountPublicKeyValid(publicKeyObject.publicKeyPem) |
19 | } | 15 | } |
20 | 16 | ||
21 | function isAccountPublicKeyIdValid (id: string) { | ||
22 | return isActivityPubUrlValid(id) | ||
23 | } | ||
24 | |||
25 | function isAccountTypeValid (type: string) { | 17 | function isAccountTypeValid (type: string) { |
26 | return type === 'Person' || type === 'Application' | 18 | return type === 'Person' || type === 'Application' |
27 | } | 19 | } |
28 | 20 | ||
29 | function isAccountPublicKeyOwnerValid (owner: string) { | ||
30 | return isActivityPubUrlValid(owner) | ||
31 | } | ||
32 | |||
33 | function isAccountPublicKeyValid (publicKey: string) { | 21 | function isAccountPublicKeyValid (publicKey: string) { |
34 | return exists(publicKey) && | 22 | return exists(publicKey) && |
35 | typeof publicKey === 'string' && | 23 | typeof publicKey === 'string' && |
@@ -38,34 +26,10 @@ function isAccountPublicKeyValid (publicKey: string) { | |||
38 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) | 26 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) |
39 | } | 27 | } |
40 | 28 | ||
41 | function isAccountIdValid (id: string) { | ||
42 | return isActivityPubUrlValid(id) | ||
43 | } | ||
44 | |||
45 | function isAccountFollowingValid (id: string) { | ||
46 | return isActivityPubUrlValid(id) | ||
47 | } | ||
48 | |||
49 | function isAccountFollowersValid (id: string) { | ||
50 | return isActivityPubUrlValid(id) | ||
51 | } | ||
52 | |||
53 | function isAccountInboxValid (inbox: string) { | ||
54 | return isActivityPubUrlValid(inbox) | ||
55 | } | ||
56 | |||
57 | function isAccountOutboxValid (outbox: string) { | ||
58 | return isActivityPubUrlValid(outbox) | ||
59 | } | ||
60 | |||
61 | function isAccountPreferredUsernameValid (preferredUsername: string) { | 29 | function isAccountPreferredUsernameValid (preferredUsername: string) { |
62 | return isAccountNameValid(preferredUsername) | 30 | return isAccountNameValid(preferredUsername) |
63 | } | 31 | } |
64 | 32 | ||
65 | function isAccountUrlValid (url: string) { | ||
66 | return isActivityPubUrlValid(url) | ||
67 | } | ||
68 | |||
69 | function isAccountPrivateKeyValid (privateKey: string) { | 33 | function isAccountPrivateKeyValid (privateKey: string) { |
70 | return exists(privateKey) && | 34 | return exists(privateKey) && |
71 | typeof privateKey === 'string' && | 35 | typeof privateKey === 'string' && |
@@ -75,15 +39,15 @@ function isAccountPrivateKeyValid (privateKey: string) { | |||
75 | } | 39 | } |
76 | 40 | ||
77 | function isRemoteAccountValid (remoteAccount: any) { | 41 | function isRemoteAccountValid (remoteAccount: any) { |
78 | return isAccountIdValid(remoteAccount.id) && | 42 | return isActivityPubUrlValid(remoteAccount.id) && |
79 | isUUIDValid(remoteAccount.uuid) && | 43 | isUUIDValid(remoteAccount.uuid) && |
80 | isAccountTypeValid(remoteAccount.type) && | 44 | isAccountTypeValid(remoteAccount.type) && |
81 | isAccountFollowingValid(remoteAccount.following) && | 45 | isActivityPubUrlValid(remoteAccount.following) && |
82 | isAccountFollowersValid(remoteAccount.followers) && | 46 | isActivityPubUrlValid(remoteAccount.followers) && |
83 | isAccountInboxValid(remoteAccount.inbox) && | 47 | isActivityPubUrlValid(remoteAccount.inbox) && |
84 | isAccountOutboxValid(remoteAccount.outbox) && | 48 | isActivityPubUrlValid(remoteAccount.outbox) && |
85 | isAccountPreferredUsernameValid(remoteAccount.preferredUsername) && | 49 | isAccountPreferredUsernameValid(remoteAccount.preferredUsername) && |
86 | isAccountUrlValid(remoteAccount.url) && | 50 | isActivityPubUrlValid(remoteAccount.url) && |
87 | isAccountPublicKeyObjectValid(remoteAccount.publicKey) && | 51 | isAccountPublicKeyObjectValid(remoteAccount.publicKey) && |
88 | isAccountEndpointsObjectValid(remoteAccount.endpoints) | 52 | isAccountEndpointsObjectValid(remoteAccount.endpoints) |
89 | } | 53 | } |
@@ -113,19 +77,10 @@ function isAccountAcceptActivityValid (activity: any) { | |||
113 | 77 | ||
114 | export { | 78 | export { |
115 | isAccountEndpointsObjectValid, | 79 | isAccountEndpointsObjectValid, |
116 | isAccountSharedInboxValid, | ||
117 | isAccountPublicKeyObjectValid, | 80 | isAccountPublicKeyObjectValid, |
118 | isAccountPublicKeyIdValid, | ||
119 | isAccountTypeValid, | 81 | isAccountTypeValid, |
120 | isAccountPublicKeyOwnerValid, | ||
121 | isAccountPublicKeyValid, | 82 | isAccountPublicKeyValid, |
122 | isAccountIdValid, | ||
123 | isAccountFollowingValid, | ||
124 | isAccountFollowersValid, | ||
125 | isAccountInboxValid, | ||
126 | isAccountOutboxValid, | ||
127 | isAccountPreferredUsernameValid, | 83 | isAccountPreferredUsernameValid, |
128 | isAccountUrlValid, | ||
129 | isAccountPrivateKeyValid, | 84 | isAccountPrivateKeyValid, |
130 | isRemoteAccountValid, | 85 | isRemoteAccountValid, |
131 | isAccountFollowingCountValid, | 86 | isAccountFollowingCountValid, |
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 55e79c4e8..12c672fd2 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts | |||
@@ -8,7 +8,6 @@ import { | |||
8 | isVideoNSFWValid, | 8 | isVideoNSFWValid, |
9 | isVideoTagValid, | 9 | isVideoTagValid, |
10 | isVideoTruncatedDescriptionValid, | 10 | isVideoTruncatedDescriptionValid, |
11 | isVideoUrlValid, | ||
12 | isVideoViewsValid | 11 | isVideoViewsValid |
13 | } from '../videos' | 12 | } from '../videos' |
14 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | 13 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' |
@@ -77,12 +76,11 @@ export { | |||
77 | function setValidRemoteTags (video: any) { | 76 | function setValidRemoteTags (video: any) { |
78 | if (Array.isArray(video.tag) === false) return false | 77 | if (Array.isArray(video.tag) === false) return false |
79 | 78 | ||
80 | const newTag = video.tag.filter(t => { | 79 | video.tag = video.tag.filter(t => { |
81 | return t.type === 'Hashtag' && | 80 | return t.type === 'Hashtag' && |
82 | isVideoTagValid(t.name) | 81 | isVideoTagValid(t.name) |
83 | }) | 82 | }) |
84 | 83 | ||
85 | video.tag = newTag | ||
86 | return true | 84 | return true |
87 | } | 85 | } |
88 | 86 | ||
@@ -96,7 +94,7 @@ function isRemoteVideoContentValid (mediaType: string, content: string) { | |||
96 | 94 | ||
97 | function isRemoteVideoIconValid (icon: any) { | 95 | function isRemoteVideoIconValid (icon: any) { |
98 | return icon.type === 'Image' && | 96 | return icon.type === 'Image' && |
99 | isVideoUrlValid(icon.url) && | 97 | isActivityPubUrlValid(icon.url) && |
100 | icon.mediaType === 'image/jpeg' && | 98 | icon.mediaType === 'image/jpeg' && |
101 | validator.isInt(icon.width + '', { min: 0 }) && | 99 | validator.isInt(icon.width + '', { min: 0 }) && |
102 | validator.isInt(icon.height + '', { min: 0 }) | 100 | validator.isInt(icon.height + '', { min: 0 }) |
@@ -105,8 +103,7 @@ function isRemoteVideoIconValid (icon: any) { | |||
105 | function setValidRemoteVideoUrls (video: any) { | 103 | function setValidRemoteVideoUrls (video: any) { |
106 | if (Array.isArray(video.url) === false) return false | 104 | if (Array.isArray(video.url) === false) return false |
107 | 105 | ||
108 | const newUrl = video.url.filter(u => isRemoteVideoUrlValid(u)) | 106 | video.url = video.url.filter(u => isRemoteVideoUrlValid(u)) |
109 | video.url = newUrl | ||
110 | 107 | ||
111 | return true | 108 | return true |
112 | } | 109 | } |
@@ -115,13 +112,13 @@ function isRemoteVideoUrlValid (url: any) { | |||
115 | return url.type === 'Link' && | 112 | return url.type === 'Link' && |
116 | ( | 113 | ( |
117 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && | 114 | ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && |
118 | isVideoUrlValid(url.url) && | 115 | isActivityPubUrlValid(url.url) && |
119 | validator.isInt(url.width + '', { min: 0 }) && | 116 | validator.isInt(url.width + '', { min: 0 }) && |
120 | validator.isInt(url.size + '', { min: 0 }) | 117 | validator.isInt(url.size + '', { min: 0 }) |
121 | ) || | 118 | ) || |
122 | ( | 119 | ( |
123 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && | 120 | ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && |
124 | isVideoUrlValid(url.url) && | 121 | isActivityPubUrlValid(url.url) && |
125 | validator.isInt(url.width + '', { min: 0 }) | 122 | validator.isInt(url.width + '', { min: 0 }) |
126 | ) || | 123 | ) || |
127 | ( | 124 | ( |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 267d987fc..3de9f041b 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,21 +1,13 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
3 | import 'express-validator' | 2 | import 'express-validator' |
4 | import 'multer' | 3 | import 'multer' |
5 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
6 | |||
7 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' | 5 | import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' |
8 | import { VideoChannelInstance } from '../../models' | 6 | import { VideoChannelInstance } from '../../models' |
9 | import { logger } from '../logger' | ||
10 | import { isActivityPubUrlValid } from './index' | ||
11 | import { exists } from './misc' | 7 | import { exists } from './misc' |
12 | 8 | ||
13 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 9 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
14 | 10 | ||
15 | function isVideoChannelUrlValid (value: string) { | ||
16 | return isActivityPubUrlValid(value) | ||
17 | } | ||
18 | |||
19 | function isVideoChannelDescriptionValid (value: string) { | 11 | function isVideoChannelDescriptionValid (value: string) { |
20 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | 12 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) |
21 | } | 13 | } |
@@ -24,31 +16,7 @@ function isVideoChannelNameValid (value: string) { | |||
24 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 16 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
25 | } | 17 | } |
26 | 18 | ||
27 | function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) { | 19 | async function isVideoChannelExist (id: string, res: express.Response) { |
28 | let promise: Bluebird<VideoChannelInstance> | ||
29 | if (validator.isInt(id)) { | ||
30 | promise = db.VideoChannel.loadAndPopulateAccount(+id) | ||
31 | } else { // UUID | ||
32 | promise = db.VideoChannel.loadByUUIDAndPopulateAccount(id) | ||
33 | } | ||
34 | |||
35 | promise.then(videoChannel => { | ||
36 | if (!videoChannel) { | ||
37 | return res.status(404) | ||
38 | .json({ error: 'Video channel not found' }) | ||
39 | .end() | ||
40 | } | ||
41 | |||
42 | res.locals.videoChannel = videoChannel | ||
43 | callback() | ||
44 | }) | ||
45 | .catch(err => { | ||
46 | logger.error('Error in video channel request validator.', err) | ||
47 | return res.sendStatus(500) | ||
48 | }) | ||
49 | } | ||
50 | |||
51 | async function isVideoChannelExistsPromise (id: string, res: express.Response) { | ||
52 | let videoChannel: VideoChannelInstance | 20 | let videoChannel: VideoChannelInstance |
53 | if (validator.isInt(id)) { | 21 | if (validator.isInt(id)) { |
54 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) | 22 | videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) |
@@ -72,8 +40,6 @@ async function isVideoChannelExistsPromise (id: string, res: express.Response) { | |||
72 | 40 | ||
73 | export { | 41 | export { |
74 | isVideoChannelDescriptionValid, | 42 | isVideoChannelDescriptionValid, |
75 | checkVideoChannelExists, | ||
76 | isVideoChannelNameValid, | 43 | isVideoChannelNameValid, |
77 | isVideoChannelExistsPromise, | 44 | isVideoChannelExist |
78 | isVideoChannelUrlValid | ||
79 | } | 45 | } |
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 276354626..f13178c54 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { Response } from 'express' | 1 | import { Response } from 'express' |
3 | import 'express-validator' | 2 | import 'express-validator' |
4 | import { values } from 'lodash' | 3 | import { values } from 'lodash' |
@@ -6,12 +5,10 @@ import 'multer' | |||
6 | import * as validator from 'validator' | 5 | import * as validator from 'validator' |
7 | import { VideoRateType } from '../../../shared' | 6 | import { VideoRateType } from '../../../shared' |
8 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' | 7 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' |
8 | import { VIDEO_PRIVACIES } from '../../initializers/constants' | ||
9 | import { database as db } from '../../initializers/database' | 9 | import { database as db } from '../../initializers/database' |
10 | import { VideoInstance } from '../../models/video/video-interface' | 10 | import { VideoInstance } from '../../models/video/video-interface' |
11 | import { logger } from '../logger' | ||
12 | import { isActivityPubUrlValid } from './activitypub/misc' | ||
13 | import { exists, isArray } from './misc' | 11 | import { exists, isArray } from './misc' |
14 | import { VIDEO_PRIVACIES } from '../../initializers/constants' | ||
15 | 12 | ||
16 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 13 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS |
17 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES | 14 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES |
@@ -20,10 +17,6 @@ function isVideoCategoryValid (value: number) { | |||
20 | return VIDEO_CATEGORIES[value] !== undefined | 17 | return VIDEO_CATEGORIES[value] !== undefined |
21 | } | 18 | } |
22 | 19 | ||
23 | function isVideoUrlValid (value: string) { | ||
24 | return isActivityPubUrlValid(value) | ||
25 | } | ||
26 | |||
27 | function isVideoLicenceValid (value: number) { | 20 | function isVideoLicenceValid (value: number) { |
28 | return VIDEO_LICENCES[value] !== undefined | 21 | return VIDEO_LICENCES[value] !== undefined |
29 | } | 22 | } |
@@ -106,31 +99,7 @@ function isVideoFileSizeValid (value: string) { | |||
106 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | 99 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) |
107 | } | 100 | } |
108 | 101 | ||
109 | function checkVideoExists (id: string, res: Response, callback: () => void) { | 102 | async function isVideoExist (id: string, res: Response) { |
110 | let promise: Bluebird<VideoInstance> | ||
111 | if (validator.isInt(id)) { | ||
112 | promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) | ||
113 | } else { // UUID | ||
114 | promise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) | ||
115 | } | ||
116 | |||
117 | promise.then(video => { | ||
118 | if (!video) { | ||
119 | return res.status(404) | ||
120 | .json({ error: 'Video not found' }) | ||
121 | .end() | ||
122 | } | ||
123 | |||
124 | res.locals.video = video | ||
125 | callback() | ||
126 | }) | ||
127 | .catch(err => { | ||
128 | logger.error('Error in video request validator.', err) | ||
129 | return res.sendStatus(500) | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | async function isVideoExistsPromise (id: string, res: Response) { | ||
134 | let video: VideoInstance | 103 | let video: VideoInstance |
135 | 104 | ||
136 | if (validator.isInt(id)) { | 105 | if (validator.isInt(id)) { |
@@ -169,10 +138,8 @@ export { | |||
169 | isVideoRatingTypeValid, | 138 | isVideoRatingTypeValid, |
170 | isVideoDurationValid, | 139 | isVideoDurationValid, |
171 | isVideoTagValid, | 140 | isVideoTagValid, |
172 | isVideoUrlValid, | ||
173 | isVideoPrivacyValid, | 141 | isVideoPrivacyValid, |
174 | isVideoFileResolutionValid, | 142 | isVideoFileResolutionValid, |
175 | isVideoFileSizeValid, | 143 | isVideoFileSizeValid, |
176 | checkVideoExists, | 144 | isVideoExist |
177 | isVideoExistsPromise | ||
178 | } | 145 | } |