video-captions.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as express from 'express'
  2. import { areValidationErrors } from '../utils'
  3. import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
  4. import { body, param } from 'express-validator'
  5. import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants'
  6. import { UserRight } from '../../../../shared'
  7. import { logger } from '../../../helpers/logger'
  8. import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
  9. import { cleanUpReqFiles } from '../../../helpers/express-utils'
  10. import { checkUserCanManageVideo, doesVideoCaptionExist, doesVideoExist } from '../../../helpers/middlewares'
  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((_, { req }) => isVideoCaptionFile(req.files, 'captionfile')).withMessage(
  16. `This caption file is not supported or too large. Please, make sure it is under ${CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE} and one of the following mimetypes: `
  17. + Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT).map(key => `${key} (${MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT[key]})`).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.videoAll, 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.videoAll, 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.videoAll, 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. }