From fb7194043d0486ce0a6a40b2ffbdf32878c33a6f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 Sep 2020 16:19:35 +0200 Subject: Check live duration and size --- server/middlewares/validators/config.ts | 40 ++++++++++++++++++++++---- server/middlewares/validators/users.ts | 2 +- server/middlewares/validators/videos/videos.ts | 5 ++-- 3 files changed, 38 insertions(+), 9 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts index d3669f6be..41a6ae4f9 100644 --- a/server/middlewares/validators/config.ts +++ b/server/middlewares/validators/config.ts @@ -1,12 +1,13 @@ import * as express from 'express' import { body } from 'express-validator' -import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' -import { logger } from '../../helpers/logger' +import { isIntOrNull } from '@server/helpers/custom-validators/misc' +import { isEmailEnabled } from '@server/initializers/config' import { CustomConfig } from '../../../shared/models/server/custom-config.model' -import { areValidationErrors } from './utils' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' +import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' +import { logger } from '../../helpers/logger' import { isThemeRegistered } from '../../lib/plugins/theme-utils' -import { isEmailEnabled } from '@server/initializers/config' +import { areValidationErrors } from './utils' const customConfigUpdateValidator = [ body('instance.name').exists().withMessage('Should have a valid instance name'), @@ -43,6 +44,7 @@ const customConfigUpdateValidator = [ body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'), body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'), body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'), + body('transcoding.resolutions.2160p').isBoolean().withMessage('Should have a valid transcoding 2160p resolution enabled boolean'), body('transcoding.webtorrent.enabled').isBoolean().withMessage('Should have a valid webtorrent transcoding enabled boolean'), body('transcoding.hls.enabled').isBoolean().withMessage('Should have a valid hls transcoding enabled boolean'), @@ -60,6 +62,18 @@ const customConfigUpdateValidator = [ body('broadcastMessage.level').exists().withMessage('Should have a valid broadcast level'), body('broadcastMessage.dismissable').isBoolean().withMessage('Should have a valid broadcast dismissable boolean'), + body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'), + body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'), + body('live.maxDuration').custom(isIntOrNull).withMessage('Should have a valid live max duration'), + body('live.transcoding.enabled').isBoolean().withMessage('Should have a valid live transcoding enabled boolean'), + body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'), + body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'), + body('live.transcoding.resolutions.360p').isBoolean().withMessage('Should have a valid transcoding 360p resolution enabled boolean'), + body('live.transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'), + body('live.transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'), + body('live.transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'), + body('live.transcoding.resolutions.2160p').isBoolean().withMessage('Should have a valid transcoding 2160p resolution enabled boolean'), + body('search.remoteUri.users').isBoolean().withMessage('Should have a remote URI search for users boolean'), body('search.remoteUri.anonymous').isBoolean().withMessage('Should have a valid remote URI search for anonymous boolean'), body('search.searchIndex.enabled').isBoolean().withMessage('Should have a valid search index enabled boolean'), @@ -71,8 +85,9 @@ const customConfigUpdateValidator = [ logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return - if (!checkInvalidConfigIfEmailDisabled(req.body as CustomConfig, res)) return - if (!checkInvalidTranscodingConfig(req.body as CustomConfig, res)) return + if (!checkInvalidConfigIfEmailDisabled(req.body, res)) return + if (!checkInvalidTranscodingConfig(req.body, res)) return + if (!checkInvalidLiveConfig(req.body, res)) return return next() } @@ -109,3 +124,16 @@ function checkInvalidTranscodingConfig (customConfig: CustomConfig, res: express return true } + +function checkInvalidLiveConfig (customConfig: CustomConfig, res: express.Response) { + if (customConfig.live.enabled === false) return true + + if (customConfig.live.allowReplay === true && customConfig.transcoding.enabled === false) { + res.status(400) + .send({ error: 'You cannot allow live replay if transcoding is not enabled' }) + .end() + return false + } + + return true +} diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 76ecff884..452c7fb93 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -497,7 +497,7 @@ export { function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) { const id = parseInt(idArg + '', 10) - return checkUserExist(() => UserModel.loadById(id, withStats), res) + return checkUserExist(() => UserModel.loadByIdWithChannels(id, withStats), res) } function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index b022b2c23..ff90e347a 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts @@ -1,5 +1,6 @@ import * as express from 'express' import { body, param, query, ValidationChain } from 'express-validator' +import { isAbleToUploadVideo } from '@server/lib/user' import { getServerActor } from '@server/models/application/application' import { MVideoFullLight } from '@server/types/models' import { ServerErrorCode, UserRight, VideoChangeOwnershipStatus, VideoPrivacy } from '../../../../shared' @@ -73,7 +74,7 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([ if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) - if (await user.isAbleToUploadVideo(videoFile) === false) { + if (await isAbleToUploadVideo(user.id, videoFile.size) === false) { res.status(403) .json({ error: 'The user video quota is exceeded with this video.' }) @@ -291,7 +292,7 @@ const videosAcceptChangeOwnershipValidator = [ const user = res.locals.oauth.token.User const videoChangeOwnership = res.locals.videoChangeOwnership - const isAble = await user.isAbleToUploadVideo(videoChangeOwnership.Video.getMaxQualityFile()) + const isAble = await isAbleToUploadVideo(user.id, videoChangeOwnership.Video.getMaxQualityFile().size) if (isAble === false) { res.status(403) .json({ error: 'The user video quota is exceeded with this video.' }) -- cgit v1.2.3