diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-01-09 16:51:51 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-01-10 10:14:04 +0100 |
commit | f2f0eda543ab54eec0f6bcdd8ccf6e382d5cafb6 (patch) | |
tree | 2786e879b381bd637ebaaf84f7fa7f1ad08d8279 /server/middlewares/validators/feeds.ts | |
parent | 9270ccf6dca5b2955ad126947d4296deb385fdcb (diff) | |
download | PeerTube-f2f0eda543ab54eec0f6bcdd8ccf6e382d5cafb6.tar.gz PeerTube-f2f0eda543ab54eec0f6bcdd8ccf6e382d5cafb6.tar.zst PeerTube-f2f0eda543ab54eec0f6bcdd8ccf6e382d5cafb6.zip |
Adapt feeds content-type to accept header
Diffstat (limited to 'server/middlewares/validators/feeds.ts')
-rw-r--r-- | server/middlewares/validators/feeds.ts | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/server/middlewares/validators/feeds.ts b/server/middlewares/validators/feeds.ts index 1bef9891b..29f6c87be 100644 --- a/server/middlewares/validators/feeds.ts +++ b/server/middlewares/validators/feeds.ts | |||
@@ -12,9 +12,37 @@ import { | |||
12 | doesVideoChannelNameWithHostExist | 12 | doesVideoChannelNameWithHostExist |
13 | } from '../../helpers/middlewares' | 13 | } from '../../helpers/middlewares' |
14 | 14 | ||
15 | const videoFeedsValidator = [ | 15 | const feedsFormatValidator = [ |
16 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | 16 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
17 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | 17 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)') |
18 | ] | ||
19 | |||
20 | function 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') { | ||
25 | acceptableContentTypes = ['application/atom+xml', 'application/xml', 'text/xml'] | ||
26 | } else if (format === 'json' || format === 'json1') { | ||
27 | acceptableContentTypes = ['application/json'] | ||
28 | } else if (format === 'rss' || format === 'rss2') { | ||
29 | acceptableContentTypes = ['application/rss+xml', 'application/xml', 'text/xml'] | ||
30 | } else { | ||
31 | acceptableContentTypes = ['application/xml', 'text/xml'] | ||
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 | |||
45 | const videoFeedsValidator = [ | ||
18 | query('accountId').optional().custom(isIdValid), | 46 | query('accountId').optional().custom(isIdValid), |
19 | query('accountName').optional(), | 47 | query('accountName').optional(), |
20 | query('videoChannelId').optional().custom(isIdValid), | 48 | query('videoChannelId').optional().custom(isIdValid), |
@@ -35,8 +63,6 @@ const videoFeedsValidator = [ | |||
35 | ] | 63 | ] |
36 | 64 | ||
37 | const videoCommentsFeedsValidator = [ | 65 | const videoCommentsFeedsValidator = [ |
38 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | ||
39 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | ||
40 | query('videoId').optional().custom(isIdOrUUIDValid), | 66 | query('videoId').optional().custom(isIdOrUUIDValid), |
41 | 67 | ||
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 68 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
@@ -53,6 +79,8 @@ const videoCommentsFeedsValidator = [ | |||
53 | // --------------------------------------------------------------------------- | 79 | // --------------------------------------------------------------------------- |
54 | 80 | ||
55 | export { | 81 | export { |
82 | feedsFormatValidator, | ||
83 | setFeedFormatContentType, | ||
56 | videoFeedsValidator, | 84 | videoFeedsValidator, |
57 | videoCommentsFeedsValidator | 85 | videoCommentsFeedsValidator |
58 | } | 86 | } |