redundancy.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import express from 'express'
  2. import { body, param, query } from 'express-validator'
  3. import { forceNumber } from '@peertube/peertube-core-utils'
  4. import { HttpStatusCode } from '@peertube/peertube-models'
  5. import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies.js'
  6. import {
  7. exists,
  8. isBooleanValid,
  9. isIdOrUUIDValid,
  10. isIdValid,
  11. toBooleanOrNull,
  12. toCompleteUUID,
  13. toIntOrNull
  14. } from '../../helpers/custom-validators/misc.js'
  15. import { isHostValid } from '../../helpers/custom-validators/servers.js'
  16. import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy.js'
  17. import { ServerModel } from '../../models/server/server.js'
  18. import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared/index.js'
  19. import { canVideoBeFederated } from '@server/lib/activitypub/videos/federate.js'
  20. const videoFileRedundancyGetValidator = [
  21. isValidVideoIdParam('videoId'),
  22. param('resolution')
  23. .customSanitizer(toIntOrNull)
  24. .custom(exists),
  25. param('fps')
  26. .optional()
  27. .customSanitizer(toIntOrNull)
  28. .custom(exists),
  29. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  30. if (areValidationErrors(req, res)) return
  31. if (!await doesVideoExist(req.params.videoId, res)) return
  32. if (!canVideoBeFederated(res.locals.onlyVideo)) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  33. const video = res.locals.videoAll
  34. const paramResolution = req.params.resolution as unknown as number // We casted to int above
  35. const paramFPS = req.params.fps as unknown as number // We casted to int above
  36. const videoFile = video.VideoFiles.find(f => {
  37. return f.resolution === paramResolution && (!req.params.fps || paramFPS)
  38. })
  39. if (!videoFile) {
  40. return res.fail({
  41. status: HttpStatusCode.NOT_FOUND_404,
  42. message: 'Video file not found.'
  43. })
  44. }
  45. res.locals.videoFile = videoFile
  46. const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
  47. if (!videoRedundancy) {
  48. return res.fail({
  49. status: HttpStatusCode.NOT_FOUND_404,
  50. message: 'Video redundancy not found.'
  51. })
  52. }
  53. res.locals.videoRedundancy = videoRedundancy
  54. return next()
  55. }
  56. ]
  57. const videoPlaylistRedundancyGetValidator = [
  58. isValidVideoIdParam('videoId'),
  59. param('streamingPlaylistType')
  60. .customSanitizer(toIntOrNull)
  61. .custom(exists),
  62. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  63. if (areValidationErrors(req, res)) return
  64. if (!await doesVideoExist(req.params.videoId, res)) return
  65. if (!canVideoBeFederated(res.locals.onlyVideo)) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  66. const video = res.locals.videoAll
  67. const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
  68. const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
  69. if (!videoStreamingPlaylist) {
  70. return res.fail({
  71. status: HttpStatusCode.NOT_FOUND_404,
  72. message: 'Video playlist not found.'
  73. })
  74. }
  75. res.locals.videoStreamingPlaylist = videoStreamingPlaylist
  76. const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
  77. if (!videoRedundancy) {
  78. return res.fail({
  79. status: HttpStatusCode.NOT_FOUND_404,
  80. message: 'Video redundancy not found.'
  81. })
  82. }
  83. res.locals.videoRedundancy = videoRedundancy
  84. return next()
  85. }
  86. ]
  87. const updateServerRedundancyValidator = [
  88. param('host')
  89. .custom(isHostValid),
  90. body('redundancyAllowed')
  91. .customSanitizer(toBooleanOrNull)
  92. .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'),
  93. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  94. if (areValidationErrors(req, res)) return
  95. const server = await ServerModel.loadByHost(req.params.host)
  96. if (!server) {
  97. return res.fail({
  98. status: HttpStatusCode.NOT_FOUND_404,
  99. message: `Server ${req.params.host} not found.`
  100. })
  101. }
  102. res.locals.server = server
  103. return next()
  104. }
  105. ]
  106. const listVideoRedundanciesValidator = [
  107. query('target')
  108. .custom(isVideoRedundancyTarget),
  109. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  110. if (areValidationErrors(req, res)) return
  111. return next()
  112. }
  113. ]
  114. const addVideoRedundancyValidator = [
  115. body('videoId')
  116. .customSanitizer(toCompleteUUID)
  117. .custom(isIdOrUUIDValid),
  118. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  119. if (areValidationErrors(req, res)) return
  120. if (!await doesVideoExist(req.body.videoId, res, 'only-video-and-blacklist')) return
  121. if (res.locals.onlyVideo.remote === false) {
  122. return res.fail({ message: 'Cannot create a redundancy on a local video' })
  123. }
  124. if (res.locals.onlyVideo.isLive) {
  125. return res.fail({ message: 'Cannot create a redundancy of a live video' })
  126. }
  127. const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
  128. if (alreadyExists) {
  129. return res.fail({
  130. status: HttpStatusCode.CONFLICT_409,
  131. message: 'This video is already duplicated by your instance.'
  132. })
  133. }
  134. return next()
  135. }
  136. ]
  137. const removeVideoRedundancyValidator = [
  138. param('redundancyId')
  139. .custom(isIdValid),
  140. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  141. if (areValidationErrors(req, res)) return
  142. const redundancy = await VideoRedundancyModel.loadByIdWithVideo(forceNumber(req.params.redundancyId))
  143. if (!redundancy) {
  144. return res.fail({
  145. status: HttpStatusCode.NOT_FOUND_404,
  146. message: 'Video redundancy not found'
  147. })
  148. }
  149. res.locals.videoRedundancy = redundancy
  150. return next()
  151. }
  152. ]
  153. // ---------------------------------------------------------------------------
  154. export {
  155. videoFileRedundancyGetValidator,
  156. videoPlaylistRedundancyGetValidator,
  157. updateServerRedundancyValidator,
  158. listVideoRedundanciesValidator,
  159. addVideoRedundancyValidator,
  160. removeVideoRedundancyValidator
  161. }