]>
Commit | Line | Data |
---|---|---|
72c7248b C |
1 | import * as express from 'express' |
2 | import 'express-validator' | |
3 | import 'multer' | |
4e50b6a1 | 4 | import * as validator from 'validator' |
8a19bee1 | 5 | import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' |
3fd3ab2d | 6 | import { VideoChannelModel } from '../../models/video/video-channel' |
4e50b6a1 | 7 | import { exists } from './misc' |
72c7248b C |
8 | |
9 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | |
10 | ||
11 | function isVideoChannelDescriptionValid (value: string) { | |
12 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | |
13 | } | |
14 | ||
15 | function isVideoChannelNameValid (value: string) { | |
16 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | |
17 | } | |
18 | ||
2422c46b C |
19 | function isVideoChannelSupportValid (value: string) { |
20 | return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) | |
21 | } | |
22 | ||
8a19bee1 C |
23 | async function isLocalVideoChannelNameExist (name: string, res: express.Response) { |
24 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | |
06a05d5f C |
25 | |
26 | return processVideoChannelExist(videoChannel, res) | |
27 | } | |
28 | ||
8a19bee1 | 29 | async function isVideoChannelIdExist (id: string, res: express.Response) { |
3fd3ab2d | 30 | let videoChannel: VideoChannelModel |
4e50b6a1 | 31 | if (validator.isInt(id)) { |
3fd3ab2d | 32 | videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) |
4e50b6a1 | 33 | } else { // UUID |
3fd3ab2d | 34 | videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id) |
4e50b6a1 C |
35 | } |
36 | ||
06a05d5f | 37 | return processVideoChannelExist(videoChannel, res) |
4e50b6a1 C |
38 | } |
39 | ||
8a19bee1 C |
40 | async function isVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { |
41 | const [ name, host ] = nameWithDomain.split('@') | |
42 | let videoChannel: VideoChannelModel | |
43 | ||
44 | if (!host || host === CONFIG.WEBSERVER.HOST) videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | |
45 | else videoChannel = await VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) | |
46 | ||
47 | return processVideoChannelExist(videoChannel, res) | |
48 | } | |
49 | ||
72c7248b C |
50 | // --------------------------------------------------------------------------- |
51 | ||
52 | export { | |
8a19bee1 | 53 | isVideoChannelNameWithHostExist, |
06a05d5f | 54 | isLocalVideoChannelNameExist, |
72c7248b | 55 | isVideoChannelDescriptionValid, |
4e50b6a1 | 56 | isVideoChannelNameValid, |
2422c46b | 57 | isVideoChannelSupportValid, |
8a19bee1 | 58 | isVideoChannelIdExist |
72c7248b | 59 | } |
06a05d5f C |
60 | |
61 | function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { | |
62 | if (!videoChannel) { | |
63 | res.status(404) | |
64 | .json({ error: 'Video channel not found' }) | |
65 | .end() | |
66 | ||
67 | return false | |
68 | } | |
69 | ||
70 | res.locals.videoChannel = videoChannel | |
71 | return true | |
72 | } |