From ac81d1a06d57b9ae86663831e7f5edcef57b0fa4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Feb 2018 18:17:05 +0100 Subject: Add ability to set video thumbnail/preview --- server/helpers/custom-validators/misc.ts | 27 ++++++++++++++++++++++++++- server/helpers/custom-validators/users.ts | 26 +++++++++----------------- server/helpers/custom-validators/videos.ts | 29 ++++++++++++++--------------- 3 files changed, 49 insertions(+), 33 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 3903884ea..8a270b777 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -1,3 +1,4 @@ +import 'multer' import * as validator from 'validator' function exists (value: any) { @@ -28,6 +29,29 @@ function isBooleanValid (value: string) { return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) } +function isFileValid ( + files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], + mimeTypeRegex: string, + field: string, + optional = false +) { + // Should have files + if (!files) return optional + if (isArray(files)) return optional + + // Should have a file + const fileArray = files[ field ] + if (!fileArray || fileArray.length === 0) { + return optional + } + + // The file should exist + const file = fileArray[ 0 ] + if (!file || !file.originalname) return false + + return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) +} + // --------------------------------------------------------------------------- export { @@ -37,5 +61,6 @@ export { isUUIDValid, isIdOrUUIDValid, isDateValid, - isBooleanValid + isBooleanValid, + isFileValid } diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 6ed60c1c4..e805313f8 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -1,9 +1,9 @@ -import * as validator from 'validator' import 'express-validator' - -import { exists, isArray } from './misc' -import { CONSTRAINTS_FIELDS } from '../../initializers' +import * as validator from 'validator' import { UserRole } from '../../../shared' +import { CONSTRAINTS_FIELDS } from '../../initializers' + +import { exists, isFileValid } from './misc' const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS @@ -37,20 +37,12 @@ function isUserRoleValid (value: any) { return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined } +const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME + .map(v => v.replace('.', '')) + .join('|') +const avatarMimeTypesRegex = `image/(${avatarMimeTypes})` function isAvatarFile (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 avatarfile = files['avatarfile'] - if (!avatarfile || avatarfile.length === 0) return false - - // The file should exist - const file = avatarfile[0] - if (!file || !file.originalname) return false - - return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype) + return isFileValid(files, avatarMimeTypesRegex, 'avatarfile') } // --------------------------------------------------------------------------- diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 0e8a2aab2..8ef3a3c64 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -8,12 +8,12 @@ import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, - VIDEO_LICENCES, + VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES, VIDEO_RATE_TYPES } from '../../initializers' import { VideoModel } from '../../models/video/video' -import { exists, isArray } from './misc' +import { exists, isArray, isFileValid } from './misc' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES @@ -68,20 +68,18 @@ 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 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 isFileValid(files, videoFileTypesRegex, 'videofile') +} - return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) +const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME + .map(v => v.replace('.', '')) + .join('|') +const videoImageTypesRegex = `image/(${videoImageTypes})` +function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { + return isFileValid(files, videoImageTypesRegex, field, true) } function isVideoPrivacyValid (value: string) { @@ -141,5 +139,6 @@ export { isVideoPrivacyValid, isVideoFileResolutionValid, isVideoFileSizeValid, - isVideoExist + isVideoExist, + isVideoImage } -- cgit v1.2.3