]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-view.ts
Add sync link to import page
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-view.ts
CommitLineData
b2111066
C
1import express from 'express'
2import { body, param } from 'express-validator'
3import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view'
4import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
5import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
6import { exists, isIdValid, isIntOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7import { logger } from '../../../helpers/logger'
8import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
aa2ce188 9import { getCachedVideoDuration } from '@server/lib/video'
b2111066
C
10
11const 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
34const 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
70export {
71 videoViewValidator,
72 getVideoLocalViewerValidator
73}