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