Begin advanced search
[oweals/peertube.git] / server / middlewares / validators / video-captions.ts
1 import * as express from 'express'
2 import { areValidationErrors } from './utils'
3 import { checkUserCanManageVideo, isVideoExist } 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'
7 import { UserRight } from '../../../shared'
8 import { logger } from '../../helpers/logger'
9 import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
10
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
20   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21     logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
22
23     if (areValidationErrors(req, res)) return
24     if (!await isVideoExist(req.params.videoId, res)) return
25
26     // Check if the user who did the request is able to update the video
27     const user = res.locals.oauth.token.User
28     if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
29
30     return next()
31   }
32 ]
33
34 const deleteVideoCaptionValidator = [
35   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
36   param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
37
38   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39     logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
40
41     if (areValidationErrors(req, res)) return
42     if (!await isVideoExist(req.params.videoId, res)) return
43     if (!await isVideoCaptionExist(res.locals.video, req.params.captionLanguage, res)) return
44
45     // Check if the user who did the request is able to update the video
46     const user = res.locals.oauth.token.User
47     if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
48
49     return next()
50   }
51 ]
52
53 const listVideoCaptionsValidator = [
54   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
55
56   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
57     logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
58
59     if (areValidationErrors(req, res)) return
60     if (!await isVideoExist(req.params.videoId, res)) return
61
62     return next()
63   }
64 ]
65
66 export {
67   addVideoCaptionValidator,
68   listVideoCaptionsValidator,
69   deleteVideoCaptionValidator
70 }