aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-channels.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-24 19:41:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commit72c7248b6fdcdb2175e726ff51b42e7555f2bd84 (patch)
tree1bfdee99dbe2392cc997edba8e314e2a8a401c72 /server/helpers/custom-validators/video-channels.ts
parent8113a93a0d9f31aa9e23702bbc31b8a76275ae22 (diff)
downloadPeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.gz
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.zst
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.zip
Add video channels
Diffstat (limited to 'server/helpers/custom-validators/video-channels.ts')
-rw-r--r--server/helpers/custom-validators/video-channels.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
new file mode 100644
index 000000000..b6be557e6
--- /dev/null
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -0,0 +1,57 @@
1import * as Promise from 'bluebird'
2import * as validator from 'validator'
3import * as express from 'express'
4import 'express-validator'
5import 'multer'
6
7import { database as db, CONSTRAINTS_FIELDS } from '../../initializers'
8import { VideoChannelInstance } from '../../models'
9import { logger } from '../logger'
10import { exists } from './misc'
11
12const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
13
14function isVideoChannelDescriptionValid (value: string) {
15 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
16}
17
18function isVideoChannelNameValid (value: string) {
19 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
20}
21
22function isVideoChannelUUIDValid (value: string) {
23 return exists(value) && validator.isUUID('' + value, 4)
24}
25
26function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
27 let promise: Promise<VideoChannelInstance>
28 if (validator.isInt(id)) {
29 promise = db.VideoChannel.loadAndPopulateAuthor(+id)
30 } else { // UUID
31 promise = db.VideoChannel.loadByUUIDAndPopulateAuthor(id)
32 }
33
34 promise.then(videoChannel => {
35 if (!videoChannel) {
36 return res.status(404)
37 .json({ error: 'Video channel not found' })
38 .end()
39 }
40
41 res.locals.videoChannel = videoChannel
42 callback()
43 })
44 .catch(err => {
45 logger.error('Error in video channel request validator.', err)
46 return res.sendStatus(500)
47 })
48}
49
50// ---------------------------------------------------------------------------
51
52export {
53 isVideoChannelDescriptionValid,
54 isVideoChannelNameValid,
55 isVideoChannelUUIDValid,
56 checkVideoChannelExists
57}