]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-captions.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-captions.ts
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
12 const addVideoCaptionValidator = [
13 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
14 param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
15 body('captionfile')
16 .custom((_, { req }) => isVideoCaptionFile(req.files, 'captionfile'))
17 .withMessage(
18 'This caption file is not supported or too large. ' +
19 `Please, make sure it is under ${CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE} and one of the following mimetypes: ` +
20 Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT).map(key => `${key} (${MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT[key]})`).join(', ')
21 ),
22
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
25
26 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
27 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
28
29 // Check if the user who did the request is able to update the video
30 const user = res.locals.oauth.token.User
31 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
32
33 return next()
34 }
35 ]
36
37 const deleteVideoCaptionValidator = [
38 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
39 param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
40
41 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
43
44 if (areValidationErrors(req, res)) return
45 if (!await doesVideoExist(req.params.videoId, res)) return
46 if (!await doesVideoCaptionExist(res.locals.videoAll, req.params.captionLanguage, res)) return
47
48 // Check if the user who did the request is able to update the video
49 const user = res.locals.oauth.token.User
50 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return
51
52 return next()
53 }
54 ]
55
56 const listVideoCaptionsValidator = [
57 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
58
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
61
62 if (areValidationErrors(req, res)) return
63 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
64
65 return next()
66 }
67 ]
68
69 export {
70 addVideoCaptionValidator,
71 listVideoCaptionsValidator,
72 deleteVideoCaptionValidator
73 }