]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
41fb13c3 1import express from 'express'
c8861d5d 2import { body, param } from 'express-validator'
c729caf6 3import { UserRight } from '@shared/models'
3e753302 4import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
6e46de09 5import { cleanUpReqFiles } from '../../../helpers/express-utils'
10363c74
C
6import { logger } from '../../../helpers/logger'
7import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants'
795212f7
C
8import {
9 areValidationErrors,
10 checkCanSeeVideoIfPrivate,
11 checkUserCanManageVideo,
12 doesVideoCaptionExist,
13 doesVideoExist,
14 isValidVideoIdParam
15} from '../shared'
40e87e9e
C
16
17const addVideoCaptionValidator = [
d4a8e7a6
C
18 isValidVideoIdParam('videoId'),
19
20 param('captionLanguage')
21 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
22
40e87e9e 23 body('captionfile')
a1587156
C
24 .custom((_, { req }) => isVideoCaptionFile(req.files, 'captionfile'))
25 .withMessage(
26 'This caption file is not supported or too large. ' +
586a7478
C
27 `Please, make sure it is under ${CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max} bytes ` +
28 'and one of the following mimetypes: ' +
a1587156
C
29 Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT).map(key => `${key} (${MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT[key]})`).join(', ')
30 ),
40e87e9e
C
31
32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
34
cf7a61b5 35 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
0f6acda1 36 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
40e87e9e
C
37
38 // Check if the user who did the request is able to update the video
39 const user = res.locals.oauth.token.User
453e83ea 40 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
40e87e9e
C
41
42 return next()
43 }
44]
45
46const deleteVideoCaptionValidator = [
d4a8e7a6
C
47 isValidVideoIdParam('videoId'),
48
49 param('captionLanguage')
50 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
40e87e9e
C
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
0f6acda1 56 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 57 if (!await doesVideoCaptionExist(res.locals.videoAll, req.params.captionLanguage, res)) return
40e87e9e
C
58
59 // Check if the user who did the request is able to update the video
60 const user = res.locals.oauth.token.User
453e83ea 61 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return
40e87e9e
C
62
63 return next()
64 }
65]
66
67const listVideoCaptionsValidator = [
d4a8e7a6 68 isValidVideoIdParam('videoId'),
40e87e9e
C
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
795212f7
C
74 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
75
76 const video = res.locals.onlyVideo
c729caf6 77 if (!await checkCanSeeVideoIfPrivate(req, res, video)) return
40e87e9e
C
78
79 return next()
80 }
81]
82
83export {
84 addVideoCaptionValidator,
85 listVideoCaptionsValidator,
86 deleteVideoCaptionValidator
87}