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