redundancy.ts 4.2 KB

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