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