2
1

redundancy.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import { body, param } from 'express-validator/check'
  4. import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
  5. import { doesVideoExist } from '../../helpers/custom-validators/videos'
  6. import { logger } from '../../helpers/logger'
  7. import { areValidationErrors } from './utils'
  8. import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
  9. import { isHostValid } from '../../helpers/custom-validators/servers'
  10. import { ServerModel } from '../../models/server/server'
  11. const videoFileRedundancyGetValidator = [
  12. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
  13. param('resolution')
  14. .customSanitizer(toIntOrNull)
  15. .custom(exists).withMessage('Should have a valid resolution'),
  16. param('fps')
  17. .optional()
  18. .customSanitizer(toIntOrNull)
  19. .custom(exists).withMessage('Should have a valid fps'),
  20. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  21. logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
  22. if (areValidationErrors(req, res)) return
  23. if (!await doesVideoExist(req.params.videoId, res)) return
  24. const video = res.locals.video
  25. const videoFile = video.VideoFiles.find(f => {
  26. return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
  27. })
  28. if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
  29. res.locals.videoFile = videoFile
  30. const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
  31. if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
  32. res.locals.videoRedundancy = videoRedundancy
  33. return next()
  34. }
  35. ]
  36. const videoPlaylistRedundancyGetValidator = [
  37. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
  38. param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'),
  39. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  40. logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
  41. if (areValidationErrors(req, res)) return
  42. if (!await doesVideoExist(req.params.videoId, res)) return
  43. const video = res.locals.video
  44. const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType)
  45. if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
  46. res.locals.videoStreamingPlaylist = videoStreamingPlaylist
  47. const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
  48. if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
  49. res.locals.videoRedundancy = videoRedundancy
  50. return next()
  51. }
  52. ]
  53. const updateServerRedundancyValidator = [
  54. param('host').custom(isHostValid).withMessage('Should have a valid host'),
  55. body('redundancyAllowed')
  56. .toBoolean()
  57. .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
  58. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  59. logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
  60. if (areValidationErrors(req, res)) return
  61. const server = await ServerModel.loadByHost(req.params.host)
  62. if (!server) {
  63. return res
  64. .status(404)
  65. .json({
  66. error: `Server ${req.params.host} not found.`
  67. })
  68. .end()
  69. }
  70. res.locals.server = server
  71. return next()
  72. }
  73. ]
  74. // ---------------------------------------------------------------------------
  75. export {
  76. videoFileRedundancyGetValidator,
  77. videoPlaylistRedundancyGetValidator,
  78. updateServerRedundancyValidator
  79. }