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