1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { UserRight } from '@shared/models'
4 import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions'
5 import { cleanUpReqFiles } from '../../../helpers/express-utils'
6 import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants'
10 checkUserCanManageVideo,
11 doesVideoCaptionExist,
16 const addVideoCaptionValidator = [
17 isValidVideoIdParam('videoId'),
19 param('captionLanguage')
20 .custom(isVideoCaptionLanguageValid).not().isEmpty(),
23 .custom((_, { req }) => isVideoCaptionFile(req.files, 'captionfile'))
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(', ')
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)
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)
43 const deleteVideoCaptionValidator = [
44 isValidVideoIdParam('videoId'),
46 param('captionLanguage')
47 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
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
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
62 const listVideoCaptionsValidator = [
63 isValidVideoIdParam('videoId'),
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
69 const video = res.locals.onlyVideo
70 if (!await checkCanSeeVideo({ req, res, video, paramId: req.params.videoId })) return
77 addVideoCaptionValidator,
78 listVideoCaptionsValidator,
79 deleteVideoCaptionValidator