1 import { Response } from 'express'
2 import 'express-validator'
3 import { values } from 'lodash'
5 import * as validator from 'validator'
6 import { UserRight, VideoPrivacy, VideoRateType } from '../../../shared'
15 } from '../../initializers'
16 import { VideoModel } from '../../models/video/video'
17 import { exists, isArray, isFileValid } from './misc'
18 import { VideoChannelModel } from '../../models/video/video-channel'
19 import { UserModel } from '../../models/account/user'
20 import * as magnetUtil from 'magnet-uri'
21 import { fetchVideo, VideoFetchType } from '../video'
23 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
25 function isVideoCategoryValid (value: any) {
26 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
29 function isVideoStateValid (value: any) {
30 return exists(value) && VIDEO_STATES[ value ] !== undefined
33 function isVideoLicenceValid (value: any) {
34 return value === null || VIDEO_LICENCES[ value ] !== undefined
37 function isVideoLanguageValid (value: any) {
38 return value === null ||
39 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
42 function isVideoDurationValid (value: string) {
43 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
46 function isVideoTruncatedDescriptionValid (value: string) {
47 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
50 function isVideoDescriptionValid (value: string) {
51 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
54 function isVideoSupportValid (value: string) {
55 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
58 function isVideoNameValid (value: string) {
59 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
62 function isVideoTagValid (tag: string) {
63 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
66 function isVideoTagsValid (tags: string[]) {
67 return tags === null || (
69 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
70 tags.every(tag => isVideoTagValid(tag))
74 function isVideoViewsValid (value: string) {
75 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
78 function isVideoRatingTypeValid (value: string) {
79 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
82 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
83 const videoFileTypesRegex = videoFileTypes.join('|')
85 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
86 return isFileValid(files, videoFileTypesRegex, 'videofile', null)
89 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
90 .map(v => v.replace('.', ''))
92 const videoImageTypesRegex = `image/(${videoImageTypes})`
94 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
95 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
98 function isVideoPrivacyValid (value: number) {
99 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
102 function isScheduleVideoUpdatePrivacyValid (value: number) {
103 return validator.isInt(value + '') &&
105 value === VideoPrivacy.UNLISTED ||
106 value === VideoPrivacy.PUBLIC
110 function isVideoFileInfoHashValid (value: string | null | undefined) {
111 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
114 function isVideoFileResolutionValid (value: string) {
115 return exists(value) && validator.isInt(value + '')
118 function isVideoFPSResolutionValid (value: string) {
119 return value === null || validator.isInt(value + '')
122 function isVideoFileSizeValid (value: string) {
123 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
126 function isVideoMagnetUriValid (value: string) {
127 if (!exists(value)) return false
129 const parsed = magnetUtil.decode(value)
130 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
133 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
134 // Retrieve the user who did the request
135 if (video.isOwned() === false) {
137 .json({ error: 'Cannot manage a video of another server.' })
142 // Check if the user can delete the video
143 // The user can delete it if he has the right
144 // Or if s/he is the video's account
145 const account = video.VideoChannel.Account
146 if (user.hasRight(right) === false && account.userId !== user.id) {
148 .json({ error: 'Cannot manage a video of another user.' })
156 async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') {
157 const video = await fetchVideo(id, fetchType)
159 if (video === null) {
161 .json({ error: 'Video not found' })
167 if (fetchType !== 'none') res.locals.video = video
171 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
172 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
173 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
174 if (videoChannel === null) {
176 .json({ error: 'Unknown video `video channel` on this instance.' })
182 res.locals.videoChannel = videoChannel
186 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
187 if (videoChannel === null) {
189 .json({ error: 'Unknown video `video channel` for this account.' })
195 res.locals.videoChannel = videoChannel
199 // ---------------------------------------------------------------------------
202 isVideoCategoryValid,
203 checkUserCanManageVideo,
205 isVideoLanguageValid,
206 isVideoTruncatedDescriptionValid,
207 isVideoDescriptionValid,
208 isVideoFileInfoHashValid,
211 isVideoFPSResolutionValid,
212 isScheduleVideoUpdatePrivacyValid,
214 isVideoMagnetUriValid,
217 isVideoRatingTypeValid,
218 isVideoDurationValid,
221 isVideoFileResolutionValid,
222 isVideoFileSizeValid,
225 isVideoChannelOfAccountExist,