]>
Commit | Line | Data |
---|---|---|
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 { areValidationErrors, doesVideoExist } from './shared' | |
8 | ||
9 | const addPlaybackMetricValidator = [ | |
10 | body('resolution') | |
11 | .isInt({ min: 0 }), | |
12 | body('fps') | |
13 | .optional() | |
14 | .isInt({ min: 0 }), | |
15 | body('playerMode') | |
16 | .custom(isValidPlayerMode), | |
17 | ||
18 | body('resolutionChanges') | |
19 | .isInt({ min: 0 }), | |
20 | ||
21 | body('errors') | |
22 | .isInt({ min: 0 }), | |
23 | ||
24 | body('downloadedBytesP2P') | |
25 | .isInt({ min: 0 }), | |
26 | body('downloadedBytesHTTP') | |
27 | .isInt({ min: 0 }), | |
28 | ||
29 | body('uploadedBytesP2P') | |
30 | .isInt({ min: 0 }), | |
31 | ||
32 | body('videoId') | |
33 | .customSanitizer(toCompleteUUID) | |
34 | .custom(isIdOrUUIDValid), | |
35 | ||
36 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
37 | if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
38 | ||
39 | const body: PlaybackMetricCreate = req.body | |
40 | ||
41 | if (areValidationErrors(req, res)) return | |
42 | if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return | |
43 | ||
44 | return next() | |
45 | } | |
46 | ] | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | addPlaybackMetricValidator | |
52 | } |