video-view.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { HttpStatusCode } from '@peertube/peertube-models'
  2. import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view.js'
  3. import { getCachedVideoDuration } from '@server/lib/video.js'
  4. import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer.js'
  5. import express from 'express'
  6. import { body, param } from 'express-validator'
  7. import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc.js'
  8. import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared/index.js'
  9. export const getVideoLocalViewerValidator = [
  10. param('localViewerId')
  11. .custom(isIdValid),
  12. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  13. if (areValidationErrors(req, res)) return
  14. const localViewer = await LocalVideoViewerModel.loadFullById(+req.params.localViewerId)
  15. if (!localViewer) {
  16. return res.fail({
  17. status: HttpStatusCode.NOT_FOUND_404,
  18. message: 'Local viewer not found'
  19. })
  20. }
  21. res.locals.localViewerFull = localViewer
  22. return next()
  23. }
  24. ]
  25. export const videoViewValidator = [
  26. isValidVideoIdParam('videoId'),
  27. body('currentTime')
  28. .customSanitizer(toIntOrNull)
  29. .isInt(),
  30. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  31. if (areValidationErrors(req, res)) return
  32. if (!await doesVideoExist(req.params.videoId, res, 'unsafe-only-immutable-attributes')) return
  33. const video = res.locals.onlyImmutableVideo
  34. const { duration } = await getCachedVideoDuration(video.id)
  35. const currentTime = req.body.currentTime
  36. if (!isVideoTimeValid(currentTime, duration)) {
  37. return res.fail({
  38. status: HttpStatusCode.BAD_REQUEST_400,
  39. message: `Current time ${currentTime} is invalid (video ${video.uuid} duration: ${duration})`,
  40. logLevel: 'warn'
  41. })
  42. }
  43. return next()
  44. }
  45. ]