X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideos.ts;h=214db17a125714cf2cddc6325158272ce5821f53;hb=f023a19c3eeeea2b014b47fae522a62eab320048;hp=dd207c7876d470cf2bada623eb9fb5d537919637;hpb=627621c1e8d37c33f7b3dd59f4c8907b12c630bc;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index dd207c787..214db17a1 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -3,24 +3,29 @@ import 'express-validator' import { values } from 'lodash' import 'multer' import * as validator from 'validator' -import { UserRight, VideoPrivacy, VideoRateType } from '../../../shared' +import { UserRight, VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' import { CONSTRAINTS_FIELDS, + MIMETYPES, VIDEO_CATEGORIES, VIDEO_LICENCES, - VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES, VIDEO_RATE_TYPES, VIDEO_STATES -} from '../../initializers' +} from '../../initializers/constants' import { VideoModel } from '../../models/video/video' -import { exists, isArray, isFileValid } from './misc' +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' +} + function isVideoCategoryValid (value: any) { return value === null || VIDEO_CATEGORIES[ value ] !== undefined } @@ -78,10 +83,15 @@ function isVideoRatingTypeValid (value: string) { return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 } -const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`) -const videoFileTypesRegex = videoFileTypes.join('|') +function isVideoFileExtnameValid (value: string) { + return exists(value) && 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) } @@ -106,6 +116,10 @@ function isScheduleVideoUpdatePrivacyValid (value: number) { ) } +function isVideoOriginallyPublishedAtValid (value: string | null) { + return value === null || isDateValid(value) +} + function isVideoFileInfoHashValid (value: string | null | undefined) { return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) } @@ -152,16 +166,10 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use return true } -async function isVideoExist (id: string, res: Response, fetchType: 'all' | 'only-video' | 'id' | 'none' = 'all') { - let video: VideoModel | null +async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { + const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined - if (fetchType === 'all') { - video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id) - } else if (fetchType === 'only-video') { - video = await VideoModel.load(id) - } else if (fetchType === 'id' || fetchType === 'none') { - video = await VideoModel.loadOnlyId(id) - } + const video = await fetchVideo(id, fetchType, userId) if (video === null) { res.status(404) @@ -175,7 +183,7 @@ async function isVideoExist (id: string, res: Response, fetchType: 'all' | 'only return true } -async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { +async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) if (videoChannel === null) { @@ -217,18 +225,21 @@ export { isVideoTagsValid, isVideoFPSResolutionValid, isScheduleVideoUpdatePrivacyValid, + isVideoOriginallyPublishedAtValid, isVideoFile, isVideoMagnetUriValid, isVideoStateValid, isVideoViewsValid, isVideoRatingTypeValid, + isVideoFileExtnameValid, isVideoDurationValid, isVideoTagValid, isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - isVideoExist, + doesVideoExist, isVideoImage, - isVideoChannelOfAccountExist, - isVideoSupportValid + doesVideoChannelOfAccountExist, + isVideoSupportValid, + isVideoFilterValid }