]>
Commit | Line | Data |
---|---|---|
b2111066 C |
1 | import express from 'express' |
2 | import { body, param } from 'express-validator' | |
3 | import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view' | |
4 | import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer' | |
5 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
6 | import { exists, isIdValid, isIntOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' | |
7 | import { logger } from '../../../helpers/logger' | |
8 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' | |
aa2ce188 | 9 | import { getCachedVideoDuration } from '@server/lib/video' |
b2111066 C |
10 | |
11 | const getVideoLocalViewerValidator = [ | |
12 | param('localViewerId') | |
13 | .custom(isIdValid).withMessage('Should have a valid local viewer id'), | |
14 | ||
15 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
16 | logger.debug('Checking getVideoLocalViewerValidator parameters', { parameters: req.params }) | |
17 | ||
18 | if (areValidationErrors(req, res)) return | |
19 | ||
20 | const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId) | |
21 | if (!localViewer) { | |
22 | return res.fail({ | |
23 | status: HttpStatusCode.NOT_FOUND_404, | |
24 | message: 'Local viewer not found' | |
25 | }) | |
26 | } | |
27 | ||
28 | res.locals.localViewerFull = localViewer | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
34 | const videoViewValidator = [ | |
35 | isValidVideoIdParam('videoId'), | |
36 | ||
37 | body('currentTime') | |
38 | .optional() // TODO: remove optional in a few versions, introduced in 4.2 | |
39 | .customSanitizer(toIntOrNull) | |
40 | .custom(isIntOrNull).withMessage('Should have correct current time'), | |
41 | ||
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
43 | logger.debug('Checking videoView parameters', { parameters: req.body }) | |
44 | ||
45 | if (areValidationErrors(req, res)) return | |
aa2ce188 | 46 | if (!await doesVideoExist(req.params.videoId, res, 'only-immutable-attributes')) return |
b2111066 | 47 | |
aa2ce188 C |
48 | const video = res.locals.onlyImmutableVideo |
49 | const { duration } = await getCachedVideoDuration(video.id) | |
b2111066 C |
50 | |
51 | if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2 | |
aa2ce188 | 52 | req.body.currentTime = Math.min(duration ?? 0, 30) |
b2111066 C |
53 | } |
54 | ||
55 | const currentTime: number = req.body.currentTime | |
56 | ||
aa2ce188 | 57 | if (!isVideoTimeValid(currentTime, duration)) { |
b2111066 C |
58 | return res.fail({ |
59 | status: HttpStatusCode.BAD_REQUEST_400, | |
60 | message: 'Current time is invalid' | |
61 | }) | |
62 | } | |
63 | ||
64 | return next() | |
65 | } | |
66 | ] | |
67 | ||
68 | // --------------------------------------------------------------------------- | |
69 | ||
70 | export { | |
71 | videoViewValidator, | |
72 | getVideoLocalViewerValidator | |
73 | } |