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