1 import { Response } from 'express'
2 import 'express-validator'
3 import { values } from 'lodash'
5 import * as validator from 'validator'
6 import { UserRight, VideoFilter, 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 isVideoFilterValid (filter: VideoFilter) {
26 return filter === 'local' || filter === 'all-local'
29 function isVideoCategoryValid (value: any) {
30 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
33 function isVideoStateValid (value: any) {
34 return exists(value) && VIDEO_STATES[ value ] !== undefined
37 function isVideoLicenceValid (value: any) {
38 return value === null || VIDEO_LICENCES[ value ] !== undefined
41 function isVideoLanguageValid (value: any) {
42 return value === null ||
43 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
46 function isVideoDurationValid (value: string) {
47 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
50 function isVideoTruncatedDescriptionValid (value: string) {
51 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
54 function isVideoDescriptionValid (value: string) {
55 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
58 function isVideoSupportValid (value: string) {
59 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
62 function isVideoNameValid (value: string) {
63 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
66 function isVideoTagValid (tag: string) {
67 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
70 function isVideoTagsValid (tags: string[]) {
71 return tags === null || (
73 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
74 tags.every(tag => isVideoTagValid(tag))
78 function isVideoViewsValid (value: string) {
79 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
82 function isVideoRatingTypeValid (value: string) {
83 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
86 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
87 const videoFileTypesRegex = videoFileTypes.join('|')
89 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
90 return isFileValid(files, videoFileTypesRegex, 'videofile', null)
93 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
94 .map(v => v.replace('.', ''))
96 const videoImageTypesRegex = `image/(${videoImageTypes})`
98 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
99 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
102 function isVideoPrivacyValid (value: number) {
103 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
106 function isScheduleVideoUpdatePrivacyValid (value: number) {
107 return validator.isInt(value + '') &&
109 value === VideoPrivacy.UNLISTED ||
110 value === VideoPrivacy.PUBLIC
114 function isVideoFileInfoHashValid (value: string | null | undefined) {
115 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
118 function isVideoFileResolutionValid (value: string) {
119 return exists(value) && validator.isInt(value + '')
122 function isVideoFPSResolutionValid (value: string) {
123 return value === null || validator.isInt(value + '')
126 function isVideoFileSizeValid (value: string) {
127 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
130 function isVideoMagnetUriValid (value: string) {
131 if (!exists(value)) return false
133 const parsed = magnetUtil.decode(value)
134 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
137 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
138 // Retrieve the user who did the request
139 if (video.isOwned() === false) {
141 .json({ error: 'Cannot manage a video of another server.' })
146 // Check if the user can delete the video
147 // The user can delete it if he has the right
148 // Or if s/he is the video's account
149 const account = video.VideoChannel.Account
150 if (user.hasRight(right) === false && account.userId !== user.id) {
152 .json({ error: 'Cannot manage a video of another user.' })
160 async function isVideoExist (id: string, res: Response, fetchType: VideoFetchType = 'all') {
161 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
163 const video = await fetchVideo(id, fetchType, userId)
165 if (video === null) {
167 .json({ error: 'Video not found' })
173 if (fetchType !== 'none') res.locals.video = video
177 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
178 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
179 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
180 if (videoChannel === null) {
182 .json({ error: 'Unknown video `video channel` on this instance.' })
188 res.locals.videoChannel = videoChannel
192 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
193 if (videoChannel === null) {
195 .json({ error: 'Unknown video `video channel` for this account.' })
201 res.locals.videoChannel = videoChannel
205 // ---------------------------------------------------------------------------
208 isVideoCategoryValid,
209 checkUserCanManageVideo,
211 isVideoLanguageValid,
212 isVideoTruncatedDescriptionValid,
213 isVideoDescriptionValid,
214 isVideoFileInfoHashValid,
217 isVideoFPSResolutionValid,
218 isScheduleVideoUpdatePrivacyValid,
220 isVideoMagnetUriValid,
223 isVideoRatingTypeValid,
224 isVideoDurationValid,
227 isVideoFileResolutionValid,
228 isVideoFileSizeValid,
231 isVideoChannelOfAccountExist,