]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-captions.ts
Fix big play button
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-captions.ts
CommitLineData
40e87e9e 1import * as express from 'express'
c8861d5d 2import { body, param } from 'express-validator'
6e46de09 3import { UserRight } from '../../../../shared'
10363c74 4import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
3e753302 5import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
6e46de09 6import { cleanUpReqFiles } from '../../../helpers/express-utils'
10363c74
C
7import { logger } from '../../../helpers/logger'
8import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants'
9import { areValidationErrors, checkUserCanManageVideo, doesVideoCaptionExist, doesVideoExist } from '../shared'
40e87e9e
C
10
11const 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')
a1587156
C
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 ),
40e87e9e
C
21
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
24
cf7a61b5 25 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
0f6acda1 26 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
40e87e9e
C
27
28 // Check if the user who did the request is able to update the video
29 const user = res.locals.oauth.token.User
453e83ea 30 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
40e87e9e
C
31
32 return next()
33 }
34]
35
36const 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
0f6acda1 44 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 45 if (!await doesVideoCaptionExist(res.locals.videoAll, req.params.captionLanguage, res)) return
40e87e9e
C
46
47 // Check if the user who did the request is able to update the video
48 const user = res.locals.oauth.token.User
453e83ea 49 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return
40e87e9e
C
50
51 return next()
52 }
53]
54
55const 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
0f6acda1 62 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
40e87e9e
C
63
64 return next()
65 }
66]
67
68export {
69 addVideoCaptionValidator,
70 listVideoCaptionsValidator,
71 deleteVideoCaptionValidator
72}