X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmiddlewares%2Fvalidators%2Fvideos.ts;h=9fe5a253b5d0a2f78030f5b4ad6f7d40517c240c;hb=2baea0c77cc765f7cbca9c9a2f4272268892a35c;hp=1dc8429c8b38c6fa0b4dbfcb5127d47e5b0acf04;hpb=6221f311de0eb8f2a9e7e4a77b8cb0ecbde6dfcd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 1dc8429c8..9fe5a253b 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -2,10 +2,20 @@ import * as express from 'express' import 'express-validator' import { body, param, query } from 'express-validator/check' import { UserRight, VideoPrivacy } from '../../../shared' -import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid } from '../../helpers/custom-validators/misc' import { + isBooleanValid, + isDateValid, + isIdOrUUIDValid, + isIdValid, + isUUIDValid, + toIntOrNull, + toValueOrNull +} from '../../helpers/custom-validators/misc' +import { + isScheduleVideoUpdatePrivacyValid, isVideoAbuseReasonValid, isVideoCategoryValid, + isVideoChannelOfAccountExist, isVideoDescriptionValid, isVideoExist, isVideoFile, @@ -14,7 +24,8 @@ import { isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, - isVideoRatingTypeValid, isVideoSupportValid, + isVideoRatingTypeValid, + isVideoSupportValid, isVideoTagsValid } from '../../helpers/custom-validators/videos' import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils' @@ -22,7 +33,6 @@ import { logger } from '../../helpers/logger' import { CONSTRAINTS_FIELDS } from '../../initializers' import { UserModel } from '../../models/account/user' import { VideoModel } from '../../models/video/video' -import { VideoChannelModel } from '../../models/video/video-channel' import { VideoShareModel } from '../../models/video/video-share' import { authenticate } from '../oauth' import { areValidationErrors } from './utils' @@ -41,36 +51,68 @@ const videosAddValidator = [ + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') ), body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), - body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), - body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), - body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), - body('nsfw').custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), - body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), - body('support').optional().custom(isVideoSupportValid).withMessage('Should have a valid support text'), - body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'), - body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), - body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), - body('commentsEnabled').custom(isBooleanValid).withMessage('Should have comments enabled boolean'), + body('category') + .optional() + .customSanitizer(toIntOrNull) + .custom(isVideoCategoryValid).withMessage('Should have a valid category'), + body('licence') + .optional() + .customSanitizer(toIntOrNull) + .custom(isVideoLicenceValid).withMessage('Should have a valid licence'), + body('language') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoLanguageValid).withMessage('Should have a valid language'), + body('nsfw') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), + body('waitTranscoding') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'), + body('description') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoDescriptionValid).withMessage('Should have a valid description'), + body('support') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoSupportValid).withMessage('Should have a valid support text'), + body('tags') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoTagsValid).withMessage('Should have correct tags'), + body('commentsEnabled') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have comments enabled boolean'), + body('privacy') + .optional() + .toInt() + .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), + body('channelId') + .toInt() + .custom(isIdValid).withMessage('Should have correct video channel id'), + body('scheduleUpdate.updateAt') + .optional() + .custom(isDateValid).withMessage('Should have a valid schedule update date'), + body('scheduleUpdate.privacy') + .optional() + .toInt() + .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) if (areValidationErrors(req, res)) return if (areErrorsInVideoImageFiles(req, res)) return + if (areErrorsInScheduleUpdate(req, res)) return const videoFile: Express.Multer.File = req.files['videofile'][0] const user = res.locals.oauth.token.User - const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id) - if (!videoChannel) { - res.status(400) - .json({ error: 'Unknown video video channel for this account.' }) - .end() - - return - } - - res.locals.videoChannel = videoChannel + if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return const isAble = await user.isAbleToUploadVideo(videoFile) if (isAble === false) { @@ -86,7 +128,7 @@ const videosAddValidator = [ try { duration = await getDurationFromVideoFile(videoFile.path) } catch (err) { - logger.error('Invalid input file in videosAddValidator.', err) + logger.error('Invalid input file in videosAddValidator.', { err }) res.status(400) .json({ error: 'Invalid input file.' }) .end() @@ -110,28 +152,74 @@ const videosUpdateValidator = [ 'This preview file is not supported. Please, make sure it is of the following type : ' + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ') ), - body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'), - body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'), - body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'), - body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'), - body('nsfw').optional().custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), - body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), - body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'), - body('support').optional().custom(isVideoSupportValid).withMessage('Should have a valid support text'), - body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'), - body('commentsEnabled').optional().custom(isBooleanValid).withMessage('Should have comments enabled boolean'), + body('name') + .optional() + .custom(isVideoNameValid).withMessage('Should have a valid name'), + body('category') + .optional() + .customSanitizer(toIntOrNull) + .custom(isVideoCategoryValid).withMessage('Should have a valid category'), + body('licence') + .optional() + .customSanitizer(toIntOrNull) + .custom(isVideoLicenceValid).withMessage('Should have a valid licence'), + body('language') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoLanguageValid).withMessage('Should have a valid language'), + body('nsfw') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), + body('waitTranscoding') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'), + body('privacy') + .optional() + .toInt() + .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), + body('description') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoDescriptionValid).withMessage('Should have a valid description'), + body('support') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoSupportValid).withMessage('Should have a valid support text'), + body('tags') + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoTagsValid).withMessage('Should have correct tags'), + body('commentsEnabled') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have comments enabled boolean'), + body('channelId') + .optional() + .toInt() + .custom(isIdValid).withMessage('Should have correct video channel id'), + body('scheduleUpdate.updateAt') + .optional() + .custom(isDateValid).withMessage('Should have a valid schedule update date'), + body('scheduleUpdate.privacy') + .optional() + .toInt() + .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videosUpdate parameters', { parameters: req.body }) if (areValidationErrors(req, res)) return if (areErrorsInVideoImageFiles(req, res)) return + if (areErrorsInScheduleUpdate(req, res)) return if (!await isVideoExist(req.params.id, res)) return const video = res.locals.video // Check if the user who did the request is able to update the video - if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return + const user = res.locals.oauth.token.User + if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) { return res.status(409) @@ -139,6 +227,8 @@ const videosUpdateValidator = [ .end() } + if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return + return next() } ] @@ -276,7 +366,7 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use // Retrieve the user who did the request if (video.isOwned() === false) { res.status(403) - .json({ error: 'Cannot remove video of another server, blacklist it' }) + .json({ error: 'Cannot manage a video of another server.' }) .end() return false } @@ -287,7 +377,7 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use const account = video.VideoChannel.Account if (user.hasRight(right) === false && account.userId !== user.id) { res.status(403) - .json({ error: 'Cannot remove video of another user' }) + .json({ error: 'Cannot manage a video of another user.' }) .end() return false } @@ -305,7 +395,7 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) { res.status(400) - .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` }) + .json({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` }) .end() return true } @@ -313,3 +403,17 @@ function areErrorsInVideoImageFiles (req: express.Request, res: express.Response return false } + +function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) { + if (req.body.scheduleUpdate) { + if (!req.body.scheduleUpdate.updateAt) { + res.status(400) + .json({ error: 'Schedule update at is mandatory.' }) + .end() + + return true + } + } + + return false +}