video-captions.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as express from 'express'
  2. import { areValidationErrors } from '../utils'
  3. import { checkUserCanManageVideo, doesVideoExist } from '../../../helpers/custom-validators/videos'
  4. import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
  5. import { body, param } from 'express-validator/check'
  6. import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
  7. import { UserRight } from '../../../../shared'
  8. import { logger } from '../../../helpers/logger'
  9. import { doesVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
  10. import { cleanUpReqFiles } from '../../../helpers/express-utils'
  11. const addVideoCaptionValidator = [
  12. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
  13. param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
  14. body('captionfile')
  15. .custom((value, { req }) => isVideoCaptionFile(req.files, 'captionfile')).withMessage(
  16. 'This caption file is not supported or too large. Please, make sure it is of the following type : '
  17. + CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME.join(', ')
  18. ),
  19. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  20. logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
  21. if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
  22. if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
  23. // Check if the user who did the request is able to update the video
  24. const user = res.locals.oauth.token.User
  25. if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
  26. return next()
  27. }
  28. ]
  29. const deleteVideoCaptionValidator = [
  30. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
  31. param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
  32. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  33. logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
  34. if (areValidationErrors(req, res)) return
  35. if (!await doesVideoExist(req.params.videoId, res)) return
  36. if (!await doesVideoCaptionExist(res.locals.video, req.params.captionLanguage, res)) return
  37. // Check if the user who did the request is able to update the video
  38. const user = res.locals.oauth.token.User
  39. if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
  40. return next()
  41. }
  42. ]
  43. const listVideoCaptionsValidator = [
  44. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
  45. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  46. logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
  47. if (areValidationErrors(req, res)) return
  48. if (!await doesVideoExist(req.params.videoId, res, 'id')) return
  49. return next()
  50. }
  51. ]
  52. export {
  53. addVideoCaptionValidator,
  54. listVideoCaptionsValidator,
  55. deleteVideoCaptionValidator
  56. }