]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/feeds.ts
Split ffmpeg utils with ffprobe utils
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
CommitLineData
244e76a5 1import * as express from 'express'
c8861d5d 2import { param, query } from 'express-validator'
57cfff78 3import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
244e76a5
RK
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
3e753302
C
7import { doesVideoExist } from '../../helpers/middlewares/videos'
8import {
9 doesAccountIdExist,
10 doesAccountNameWithHostExist,
11 doesVideoChannelIdExist,
12 doesVideoChannelNameWithHostExist
13} from '../../helpers/middlewares'
244e76a5 14
f2f0eda5 15const feedsFormatValidator = [
244e76a5 16 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
f2f0eda5
RK
17 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
18]
19
20function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
21 const format = req.query.format || req.params.format || 'rss'
22
23 let acceptableContentTypes: string[]
24 if (format === 'atom' || format === 'atom1') {
a1587156 25 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
f2f0eda5 26 } else if (format === 'json' || format === 'json1') {
a1587156 27 acceptableContentTypes = [ 'application/json' ]
f2f0eda5 28 } else if (format === 'rss' || format === 'rss2') {
a1587156 29 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
f2f0eda5 30 } else {
a1587156 31 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
f2f0eda5
RK
32 }
33
34 if (req.accepts(acceptableContentTypes)) {
35 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
36 } else {
37 return res.status(406).send({
38 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
39 }).end()
40 }
41
42 return next()
43}
44
45const videoFeedsValidator = [
57cfff78
C
46 query('accountId').optional().custom(isIdValid),
47 query('accountName').optional(),
48 query('videoChannelId').optional().custom(isIdValid),
49 query('videoChannelName').optional(),
244e76a5
RK
50
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 logger.debug('Checking feeds parameters', { parameters: req.query })
53
54 if (areValidationErrors(req, res)) return
55
0f6acda1
C
56 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
57 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
58 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
59 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
244e76a5
RK
60
61 return next()
62 }
63]
64
fe3a55b0 65const videoCommentsFeedsValidator = [
fe3a55b0
C
66 query('videoId').optional().custom(isIdOrUUIDValid),
67
68 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
69 logger.debug('Checking feeds parameters', { parameters: req.query })
70
71 if (areValidationErrors(req, res)) return
72
00494d6e
RK
73 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
74 return res.status(400).send({
75 message: 'videoId cannot be mixed with a channel filter'
76 }).end()
77 }
78
0f6acda1 79 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
fe3a55b0
C
80
81 return next()
82 }
83]
84
244e76a5
RK
85// ---------------------------------------------------------------------------
86
87export {
f2f0eda5
RK
88 feedsFormatValidator,
89 setFeedFormatContentType,
fe3a55b0
C
90 videoFeedsValidator,
91 videoCommentsFeedsValidator
244e76a5 92}