]>
Commit | Line | Data |
---|---|---|
fd3c2e87 C |
1 | import express from 'express' |
2 | import { body } from 'express-validator' | |
3 | import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics' | |
4 | import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc' | |
5 | import { CONFIG } from '@server/initializers/config' | |
6 | import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models' | |
7 | import { logger } from '../../helpers/logger' | |
8 | import { areValidationErrors, doesVideoExist } from './shared' | |
9 | ||
10 | const addPlaybackMetricValidator = [ | |
11 | body('resolution') | |
396f6f01 | 12 | .isInt({ min: 0 }), |
fd3c2e87 C |
13 | body('fps') |
14 | .optional() | |
396f6f01 | 15 | .isInt({ min: 0 }), |
fd3c2e87 | 16 | body('playerMode') |
396f6f01 | 17 | .custom(isValidPlayerMode), |
fd3c2e87 C |
18 | |
19 | body('resolutionChanges') | |
396f6f01 | 20 | .isInt({ min: 0 }), |
fd3c2e87 C |
21 | |
22 | body('errors') | |
396f6f01 | 23 | .isInt({ min: 0 }), |
fd3c2e87 C |
24 | |
25 | body('downloadedBytesP2P') | |
396f6f01 | 26 | .isInt({ min: 0 }), |
fd3c2e87 | 27 | body('downloadedBytesHTTP') |
396f6f01 | 28 | .isInt({ min: 0 }), |
fd3c2e87 C |
29 | |
30 | body('uploadedBytesP2P') | |
396f6f01 | 31 | .isInt({ min: 0 }), |
fd3c2e87 C |
32 | |
33 | body('videoId') | |
34 | .customSanitizer(toCompleteUUID) | |
fd3c2e87 C |
35 | .custom(isIdOrUUIDValid), |
36 | ||
37 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
38 | logger.debug('Checking addPlaybackMetricValidator parameters.', { parameters: req.query }) | |
39 | ||
40 | if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
41 | ||
42 | const body: PlaybackMetricCreate = req.body | |
43 | ||
44 | if (areValidationErrors(req, res)) return | |
45 | if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return | |
46 | ||
47 | return next() | |
48 | } | |
49 | ] | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | addPlaybackMetricValidator | |
55 | } |