X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fredundancy.ts;h=c80f9b728251e214e931d778b49bdad4a90bf166;hb=2c015b54192f2080f756c424173bac2bd53e7ca9;hp=da24f4c9b73d0033e24be16f14f579768efebbf7;hpb=10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index da24f4c9b..c80f9b728 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts @@ -1,27 +1,34 @@ -import * as express from 'express' +import express from 'express' import { body, param, query } from 'express-validator' import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' +import { forceNumber } from '@shared/core-utils' +import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' +import { + exists, + isBooleanValid, + isIdOrUUIDValid, + isIdValid, + toBooleanOrNull, + toCompleteUUID, + toIntOrNull +} from '../../helpers/custom-validators/misc' import { isHostValid } from '../../helpers/custom-validators/servers' -import { logger } from '../../helpers/logger' import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' import { ServerModel } from '../../models/server/server' -import { areValidationErrors, doesVideoExist } from './shared' +import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared' const videoFileRedundancyGetValidator = [ - param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), + isValidVideoIdParam('videoId'), + param('resolution') .customSanitizer(toIntOrNull) - .custom(exists).withMessage('Should have a valid resolution'), + .custom(exists), param('fps') .optional() .customSanitizer(toIntOrNull) - .custom(exists).withMessage('Should have a valid fps'), + .custom(exists), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params }) - if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return @@ -56,16 +63,13 @@ const videoFileRedundancyGetValidator = [ ] const videoPlaylistRedundancyGetValidator = [ - param('videoId') - .custom(isIdOrUUIDValid) - .not().isEmpty().withMessage('Should have a valid video id'), + isValidVideoIdParam('videoId'), + param('streamingPlaylistType') .customSanitizer(toIntOrNull) - .custom(exists).withMessage('Should have a valid streaming playlist type'), + .custom(exists), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params }) - if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.params.videoId, res)) return @@ -96,14 +100,14 @@ const videoPlaylistRedundancyGetValidator = [ ] const updateServerRedundancyValidator = [ - param('host').custom(isHostValid).withMessage('Should have a valid host'), + param('host') + .custom(isHostValid), + body('redundancyAllowed') .customSanitizer(toBooleanOrNull) - .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), + .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params }) - if (areValidationErrors(req, res)) return const server = await ServerModel.loadByHost(req.params.host) @@ -122,11 +126,9 @@ const updateServerRedundancyValidator = [ const listVideoRedundanciesValidator = [ query('target') - .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'), + .custom(isVideoRedundancyTarget), (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query }) - if (areValidationErrors(req, res)) return return next() @@ -135,12 +137,10 @@ const listVideoRedundanciesValidator = [ const addVideoRedundancyValidator = [ body('videoId') - .custom(isIdValid) - .withMessage('Should have a valid video id'), + .customSanitizer(toCompleteUUID) + .custom(isIdOrUUIDValid), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query }) - if (areValidationErrors(req, res)) return if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return @@ -167,15 +167,12 @@ const addVideoRedundancyValidator = [ const removeVideoRedundancyValidator = [ param('redundancyId') - .custom(isIdValid) - .withMessage('Should have a valid redundancy id'), + .custom(isIdValid), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query }) - if (areValidationErrors(req, res)) return - const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10)) + const redundancy = await VideoRedundancyModel.loadByIdWithVideo(forceNumber(req.params.redundancyId)) if (!redundancy) { return res.fail({ status: HttpStatusCode.NOT_FOUND_404,