X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideos.ts;h=5f75ec27c8a0b43f29ffbaf1897619d5cd722e40;hb=83002a823465fe03a8d82833cb2e073a383405a8;hp=dd04aa5f65c4346aac6fa04d44efd2d72bb9d37b;hpb=b718fd22374d64534bcfe69932cf562894abed6a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index dd04aa5f6..5f75ec27c 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -1,40 +1,39 @@ -import { Response } from 'express' -import 'express-validator' -import { values } from 'lodash' -import 'multer' -import * as validator from 'validator' -import { UserRight, VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' +import { UploadFilesForCheck } from 'express' +import { decode as magnetUriDecode } from 'magnet-uri' +import validator from 'validator' +import { VideoFilter, VideoInclude, VideoPrivacy, VideoRateType } from '@shared/models' import { - CONSTRAINTS_FIELDS, MIMETYPES, + CONSTRAINTS_FIELDS, + MIMETYPES, VIDEO_CATEGORIES, VIDEO_LICENCES, + VIDEO_LIVE, VIDEO_PRIVACIES, VIDEO_RATE_TYPES, VIDEO_STATES -} from '../../initializers' -import { VideoModel } from '../../models/video/video' +} from '../../initializers/constants' import { exists, isArray, isDateValid, isFileValid } from './misc' -import { VideoChannelModel } from '../../models/video/video-channel' -import { UserModel } from '../../models/account/user' -import * as magnetUtil from 'magnet-uri' -import { fetchVideo, VideoFetchType } from '../video' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS function isVideoFilterValid (filter: VideoFilter) { - return filter === 'local' || filter === 'all-local' + return filter === 'local' || filter === 'all-local' || filter === 'all' +} + +function isVideoIncludeValid (include: VideoInclude) { + return exists(include) && validator.isInt('' + include) } function isVideoCategoryValid (value: any) { - return value === null || VIDEO_CATEGORIES[ value ] !== undefined + return value === null || VIDEO_CATEGORIES[value] !== undefined } function isVideoStateValid (value: any) { - return exists(value) && VIDEO_STATES[ value ] !== undefined + return exists(value) && VIDEO_STATES[value] !== undefined } function isVideoLicenceValid (value: any) { - return value === null || VIDEO_LICENCES[ value ] !== undefined + return value === null || VIDEO_LICENCES[value] !== undefined } function isVideoLanguageValid (value: any) { @@ -46,10 +45,6 @@ function isVideoDurationValid (value: string) { return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) } -function isVideoTruncatedDescriptionValid (value: string) { - return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION) -} - function isVideoDescriptionValid (value: string) { return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)) } @@ -66,7 +61,7 @@ function isVideoTagValid (tag: string) { return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) } -function isVideoTagsValid (tags: string[]) { +function areVideoTagsValid (tags: string[]) { return tags === null || ( isArray(tags) && validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && @@ -78,20 +73,22 @@ function isVideoViewsValid (value: string) { return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) } +const ratingTypes = new Set(Object.values(VIDEO_RATE_TYPES)) function isVideoRatingTypeValid (value: string) { - return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 + return value === 'none' || ratingTypes.has(value as VideoRateType) } function isVideoFileExtnameValid (value: string) { - return exists(value) && MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined + return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined) } -function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { - const videoFileTypesRegex = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT) - .map(m => `(${m})`) - .join('|') - - return isFileValid(files, videoFileTypesRegex, 'videofile', null) +function isVideoFileMimeTypeValid (files: UploadFilesForCheck, field = 'videofile') { + return isFileValid({ + files, + mimeTypeRegex: MIMETYPES.VIDEO.MIMETYPES_REGEX, + field, + maxSize: null + }) } const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME @@ -99,20 +96,22 @@ const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME .join('|') const videoImageTypesRegex = `image/(${videoImageTypes})` -function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { - return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true) +function isVideoImageValid (files: UploadFilesForCheck, field: string, optional = true) { + return isFileValid({ + files, + mimeTypeRegex: videoImageTypesRegex, + field, + maxSize: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, + optional + }) } function isVideoPrivacyValid (value: number) { - return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined + return VIDEO_PRIVACIES[value] !== undefined } function isScheduleVideoUpdatePrivacyValid (value: number) { - return validator.isInt(value + '') && - ( - value === VideoPrivacy.UNLISTED || - value === VideoPrivacy.PUBLIC - ) + return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL } function isVideoOriginallyPublishedAtValid (value: string | null) { @@ -138,107 +137,36 @@ function isVideoFileSizeValid (value: string) { function isVideoMagnetUriValid (value: string) { if (!exists(value)) return false - const parsed = magnetUtil.decode(value) + const parsed = magnetUriDecode(value) return parsed && isVideoFileInfoHashValid(parsed.infoHash) } -function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { - // Retrieve the user who did the request - if (video.isOwned() === false) { - res.status(403) - .json({ error: 'Cannot manage a video of another server.' }) - .end() - return false - } - - // Check if the user can delete the video - // The user can delete it if he has the right - // Or if s/he is the video's account - const account = video.VideoChannel.Account - if (user.hasRight(right) === false && account.userId !== user.id) { - res.status(403) - .json({ error: 'Cannot manage a video of another user.' }) - .end() - return false - } - - return true -} - -async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') { - const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined - - const video = await fetchVideo(id, fetchType, userId) - - if (video === null) { - res.status(404) - .json({ error: 'Video not found' }) - .end() - - return false - } - - if (fetchType !== 'none') res.locals.video = video - return true -} - -async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { - if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { - const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) - if (videoChannel === null) { - res.status(400) - .json({ error: 'Unknown video `video channel` on this instance.' }) - .end() - - return false - } - - res.locals.videoChannel = videoChannel - return true - } - - const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) - if (videoChannel === null) { - res.status(400) - .json({ error: 'Unknown video `video channel` for this account.' }) - .end() - - return false - } - - res.locals.videoChannel = videoChannel - return true -} - // --------------------------------------------------------------------------- export { isVideoCategoryValid, - checkUserCanManageVideo, isVideoLicenceValid, isVideoLanguageValid, - isVideoTruncatedDescriptionValid, isVideoDescriptionValid, isVideoFileInfoHashValid, isVideoNameValid, - isVideoTagsValid, + areVideoTagsValid, isVideoFPSResolutionValid, isScheduleVideoUpdatePrivacyValid, isVideoOriginallyPublishedAtValid, - isVideoFile, isVideoMagnetUriValid, isVideoStateValid, + isVideoIncludeValid, isVideoViewsValid, isVideoRatingTypeValid, isVideoFileExtnameValid, + isVideoFileMimeTypeValid, isVideoDurationValid, isVideoTagValid, isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - isVideoExist, - isVideoImage, - isVideoChannelOfAccountExist, + isVideoImageValid, isVideoSupportValid, isVideoFilterValid }