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