]>
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' | |
a85d5303 | 4 | import { getCachedVideoDuration } from '@server/lib/video' |
b2111066 C |
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' | |
b2111066 C |
8 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' |
9 | ||
10 | const getVideoLocalViewerValidator = [ | |
11 | param('localViewerId') | |
396f6f01 | 12 | .custom(isIdValid), |
b2111066 C |
13 | |
14 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
b2111066 C |
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) | |
396f6f01 | 37 | .custom(isIntOrNull), |
b2111066 C |
38 | |
39 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
b2111066 | 40 | if (areValidationErrors(req, res)) return |
aa2ce188 | 41 | if (!await doesVideoExist(req.params.videoId, res, 'only-immutable-attributes')) return |
b2111066 | 42 | |
aa2ce188 C |
43 | const video = res.locals.onlyImmutableVideo |
44 | const { duration } = await getCachedVideoDuration(video.id) | |
b2111066 C |
45 | |
46 | if (!exists(req.body.currentTime)) { // TODO: remove in a few versions, introduced in 4.2 | |
aa2ce188 | 47 | req.body.currentTime = Math.min(duration ?? 0, 30) |
b2111066 C |
48 | } |
49 | ||
50 | const currentTime: number = req.body.currentTime | |
51 | ||
aa2ce188 | 52 | if (!isVideoTimeValid(currentTime, duration)) { |
b2111066 C |
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 | } |