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 { logger } from '../../../helpers/logger'
7 import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants'
10 checkCanSeeVideoIfPrivate,
11 checkUserCanManageVideo,
12 doesVideoCaptionExist,
17 const addVideoCaptionValidator = [
18 isValidVideoIdParam('videoId'),
20 param('captionLanguage')
21 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
24 .custom((_, { req }) => isVideoCaptionFile(req.files, 'captionfile'))
26 'This caption file is not supported or too large. ' +
27 `Please, make sure it is under ${CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max} bytes ` +
28 'and one of the following mimetypes: ' +
29 Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT).map(key => `${key} (${MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT[key]})`).join(', ')
32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
35 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
36 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
38 // Check if the user who did the request is able to update the video
39 const user = res.locals.oauth.token.User
40 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
46 const deleteVideoCaptionValidator = [
47 isValidVideoIdParam('videoId'),
49 param('captionLanguage')
50 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
55 if (areValidationErrors(req, res)) return
56 if (!await doesVideoExist(req.params.videoId, res)) return
57 if (!await doesVideoCaptionExist(res.locals.videoAll, req.params.captionLanguage, res)) return
59 // Check if the user who did the request is able to update the video
60 const user = res.locals.oauth.token.User
61 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return
67 const listVideoCaptionsValidator = [
68 isValidVideoIdParam('videoId'),
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
73 if (areValidationErrors(req, res)) return
74 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
76 const video = res.locals.onlyVideo
77 if (!await checkCanSeeVideoIfPrivate(req, res, video)) return
84 addVideoCaptionValidator,
85 listVideoCaptionsValidator,
86 deleteVideoCaptionValidator