X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideos.ts;h=9e8177f77a58f001eb4dcb529285fe58695a49ae;hb=f713f36bdf6f696ab0fe8a309035a09e864a48ca;hp=1505632da8720a6baee20ff2077d17b0e4f1a8ed;hpb=8e13fa7d09e9925b4559cbba6c5d72c5ff1bd391;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 1505632da..9e8177f77 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -1,89 +1,56 @@ -import { values } from 'lodash' -import * as validator from 'validator' -import * as Promise from 'bluebird' -import * as express from 'express' -import 'express-validator' -import 'multer' - +import { UploadFilesForCheck } from 'express' +import magnetUtil from 'magnet-uri' +import validator from 'validator' +import { VideoFilter, VideoInclude, VideoPrivacy, VideoRateType } from '@shared/models' import { CONSTRAINTS_FIELDS, + MIMETYPES, VIDEO_CATEGORIES, VIDEO_LICENCES, - VIDEO_LANGUAGES, - VIDEO_RATE_TYPES, + VIDEO_LIVE, VIDEO_PRIVACIES, - database as db -} from '../../initializers' -import { isUserUsernameValid } from './users' -import { isArray, exists } from './misc' -import { VideoInstance } from '../../models' -import { logger } from '../../helpers' -import { VideoRateType } from '../../../shared' -import { isActivityPubUrlValid } from './activitypub/misc' + VIDEO_RATE_TYPES, + VIDEO_STATES +} from '../../initializers/constants' +import { exists, isArray, isDateValid, isFileValid } from './misc' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS -const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES -const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS - -function isVideoCategoryValid (value: number) { - return VIDEO_CATEGORIES[value] !== undefined -} - -// Maybe we don't know the remote category, but that doesn't matter -function isRemoteVideoCategoryValid (value: string) { - return validator.isInt('' + value) -} - -function isVideoUrlValid (value: string) { - return isActivityPubUrlValid(value) -} -function isVideoLicenceValid (value: number) { - return VIDEO_LICENCES[value] !== undefined -} - -function isVideoPrivacyValid (value: string) { - return VIDEO_PRIVACIES[value] !== undefined +function isVideoFilterValid (filter: VideoFilter) { + return filter === 'local' || filter === 'all-local' || filter === 'all' } -// Maybe we don't know the remote privacy setting, but that doesn't matter -function isRemoteVideoPrivacyValid (value: string) { - return validator.isInt('' + value) +function isVideoIncludeValid (include: VideoInclude) { + return exists(include) && validator.isInt('' + include) } -// Maybe we don't know the remote licence, but that doesn't matter -function isRemoteVideoLicenceValid (value: string) { - return validator.isInt('' + value) +function isVideoCategoryValid (value: any) { + return value === null || VIDEO_CATEGORIES[value] !== undefined } -function isVideoLanguageValid (value: number) { - return value === null || VIDEO_LANGUAGES[value] !== undefined +function isVideoStateValid (value: any) { + return exists(value) && VIDEO_STATES[value] !== undefined } -// Maybe we don't know the remote language, but that doesn't matter -function isRemoteVideoLanguageValid (value: string) { - return validator.isInt('' + value) +function isVideoLicenceValid (value: any) { + return value === null || VIDEO_LICENCES[value] !== undefined } -function isVideoNSFWValid (value: any) { - return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) +function isVideoLanguageValid (value: any) { + return value === null || + (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE)) } -function isVideoTruncatedDescriptionValid (value: string) { - return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION) +function isVideoDurationValid (value: string) { + return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) } function isVideoDescriptionValid (value: string) { - return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) + return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)) } -function isVideoDurationValid (value: string) { - // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration - return exists(value) && - typeof value === 'string' && - value.startsWith('PT') && - value.endsWith('S') && - validator.isInt(value.replace(/[^0-9]+/, ''), VIDEOS_CONSTRAINTS_FIELDS.DURATION) +function isVideoSupportValid (value: string) { + return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT)) } function isVideoNameValid (value: string) { @@ -94,98 +61,84 @@ function isVideoTagValid (tag: string) { return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) } -function isVideoTagsValid (tags: string[]) { - return isArray(tags) && - validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && - tags.every(tag => isVideoTagValid(tag)) +function areVideoTagsValid (tags: string[]) { + return tags === null || ( + isArray(tags) && + validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && + tags.every(tag => isVideoTagValid(tag)) + ) } -function isVideoThumbnailValid (value: string) { - return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) +function isVideoViewsValid (value: string) { + return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) } -function isVideoThumbnailDataValid (value: string) { - return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) +const ratingTypes = new Set(Object.values(VIDEO_RATE_TYPES)) +function isVideoRatingTypeValid (value: string) { + return value === 'none' || ratingTypes.has(value as VideoRateType) } -function isVideoAbuseReasonValid (value: string) { - return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) +function isVideoFileExtnameValid (value: string) { + return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined) } -function isVideoViewsValid (value: string) { - return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) +function isVideoFileMimeTypeValid (files: UploadFilesForCheck, field = 'videofile') { + return isFileValid({ + files, + mimeTypeRegex: MIMETYPES.VIDEO.MIMETYPES_REGEX, + field, + maxSize: null + }) } -function isVideoLikesValid (value: string) { - return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) -} +const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME + .map(v => v.replace('.', '')) + .join('|') +const videoImageTypesRegex = `image/(${videoImageTypes})` -function isVideoDislikesValid (value: string) { - return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) +function isVideoImageValid (files: UploadFilesForCheck, field: string, optional = true) { + return isFileValid({ + files, + mimeTypeRegex: videoImageTypesRegex, + field, + maxSize: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, + optional + }) } -function isVideoEventCountValid (value: string) { - return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) +function isVideoPrivacyValid (value: number) { + return VIDEO_PRIVACIES[value] !== undefined } -function isVideoRatingTypeValid (value: string) { - return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 +function isScheduleVideoUpdatePrivacyValid (value: number) { + return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL } -function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { - // Should have files - if (!files) return false - if (isArray(files)) return false - - // Should have videofile file - const videofile = files['videofile'] - if (!videofile || videofile.length === 0) return false - - // The file should exist - const file = videofile[0] - if (!file || !file.originalname) return false - - return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) +function isVideoOriginallyPublishedAtValid (value: string | null) { + return value === null || isDateValid(value) } -function isVideoFileSizeValid (value: string) { - return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) +function isVideoFileInfoHashValid (value: string | null | undefined) { + return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) } function isVideoFileResolutionValid (value: string) { return exists(value) && validator.isInt(value + '') } -function isVideoFileExtnameValid (value: string) { - return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 +function isVideoFPSResolutionValid (value: string) { + return value === null || validator.isInt(value + '') } -function isVideoFileInfoHashValid (value: string) { - return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) +function isVideoFileSizeValid (value: string) { + return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) } -function checkVideoExists (id: string, res: express.Response, callback: () => void) { - let promise: Promise - if (validator.isInt(id)) { - promise = db.Video.loadAndPopulateAccountAndServerAndTags(+id) - } else { // UUID - promise = db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) - } +function isVideoMagnetUriValid (value: string) { + if (!exists(value)) return false - promise.then(video => { - if (!video) { - return res.status(404) - .json({ error: 'Video not found' }) - .end() - } - - res.locals.video = video - callback() - }) - .catch(err => { - logger.error('Error in video request validator.', err) - return res.sendStatus(500) - }) + const parsed = magnetUtil.decode(value) + return parsed && isVideoFileInfoHashValid(parsed.infoHash) } // --------------------------------------------------------------------------- @@ -194,31 +147,26 @@ export { isVideoCategoryValid, isVideoLicenceValid, isVideoLanguageValid, - isVideoNSFWValid, - isVideoTruncatedDescriptionValid, isVideoDescriptionValid, - isVideoDurationValid, isVideoFileInfoHashValid, isVideoNameValid, - isVideoTagsValid, - isVideoThumbnailValid, - isVideoThumbnailDataValid, - isVideoFileExtnameValid, - isVideoAbuseReasonValid, - isVideoFile, + areVideoTagsValid, + isVideoFPSResolutionValid, + isScheduleVideoUpdatePrivacyValid, + isVideoOriginallyPublishedAtValid, + isVideoMagnetUriValid, + isVideoStateValid, + isVideoIncludeValid, isVideoViewsValid, - isVideoLikesValid, isVideoRatingTypeValid, - isVideoDislikesValid, - isVideoEventCountValid, - isVideoFileSizeValid, + isVideoFileExtnameValid, + isVideoFileMimeTypeValid, + isVideoDurationValid, + isVideoTagValid, isVideoPrivacyValid, - isRemoteVideoPrivacyValid, isVideoFileResolutionValid, - checkVideoExists, - isVideoTagValid, - isRemoteVideoCategoryValid, - isRemoteVideoLicenceValid, - isVideoUrlValid, - isRemoteVideoLanguageValid + isVideoFileSizeValid, + isVideoImageValid, + isVideoSupportValid, + isVideoFilterValid }