diff options
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/cache.ts | 12 | ||||
-rw-r--r-- | server/middlewares/validators/feeds.ts | 36 |
2 files changed, 41 insertions, 7 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index ef8611875..cb24d9e0e 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts | |||
@@ -4,12 +4,18 @@ import * as apicache from 'apicache' | |||
4 | // Ensure Redis is initialized | 4 | // Ensure Redis is initialized |
5 | Redis.Instance.init() | 5 | Redis.Instance.init() |
6 | 6 | ||
7 | const options = { | 7 | const defaultOptions = { |
8 | redisClient: Redis.Instance.getClient(), | 8 | redisClient: Redis.Instance.getClient(), |
9 | appendKey: () => Redis.Instance.getPrefix() | 9 | appendKey: () => Redis.Instance.getPrefix(), |
10 | statusCodes: { | ||
11 | exclude: [ 404, 403 ] | ||
12 | } | ||
10 | } | 13 | } |
11 | 14 | ||
12 | const cacheRoute = apicache.options(options).middleware | 15 | const cacheRoute = (extraOptions = {}) => apicache.options({ |
16 | ...defaultOptions, | ||
17 | ...extraOptions | ||
18 | }).middleware | ||
13 | 19 | ||
14 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
15 | 21 | ||
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 | } |