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'
22 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
24 function isVideoCategoryValid (value: any) {
25 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
28 function isVideoStateValid (value: any) {
29 return exists(value) && VIDEO_STATES[ value ] !== undefined
32 function isVideoLicenceValid (value: any) {
33 return value === null || VIDEO_LICENCES[ value ] !== undefined
36 function isVideoLanguageValid (value: any) {
37 return value === null ||
38 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
41 function isVideoDurationValid (value: string) {
42 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
45 function isVideoTruncatedDescriptionValid (value: string) {
46 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
49 function isVideoDescriptionValid (value: string) {
50 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
53 function isVideoSupportValid (value: string) {
54 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
57 function isVideoNameValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
61 function isVideoTagValid (tag: string) {
62 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
65 function isVideoTagsValid (tags: string[]) {
66 return tags === null || (
68 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
69 tags.every(tag => isVideoTagValid(tag))
73 function isVideoViewsValid (value: string) {
74 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
77 function isVideoRatingTypeValid (value: string) {
78 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
81 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
82 const videoFileTypesRegex = videoFileTypes.join('|')
84 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
85 return isFileValid(files, videoFileTypesRegex, 'videofile', null)
88 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
89 .map(v => v.replace('.', ''))
91 const videoImageTypesRegex = `image/(${videoImageTypes})`
93 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
94 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
97 function isVideoPrivacyValid (value: number) {
98 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
101 function isScheduleVideoUpdatePrivacyValid (value: number) {
102 return validator.isInt(value + '') &&
104 value === VideoPrivacy.UNLISTED ||
105 value === VideoPrivacy.PUBLIC
109 function isVideoFileInfoHashValid (value: string | null | undefined) {
110 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
113 function isVideoFileResolutionValid (value: string) {
114 return exists(value) && validator.isInt(value + '')
117 function isVideoFPSResolutionValid (value: string) {
118 return value === null || validator.isInt(value + '')
121 function isVideoFileSizeValid (value: string) {
122 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
125 function isVideoMagnetUriValid (value: string) {
126 if (!exists(value)) return false
128 const parsed = magnetUtil.decode(value)
129 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
132 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
133 // Retrieve the user who did the request
134 if (video.isOwned() === false) {
136 .json({ error: 'Cannot manage a video of another server.' })
141 // Check if the user can delete the video
142 // The user can delete it if he has the right
143 // Or if s/he is the video's account
144 const account = video.VideoChannel.Account
145 if (user.hasRight(right) === false && account.userId !== user.id) {
147 .json({ error: 'Cannot manage a video of another user.' })
155 async function isVideoExist (id: string, res: Response) {
156 let video: VideoModel | null
158 if (validator.isInt(id)) {
159 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
161 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
164 if (video === null) {
166 .json({ error: 'Video not found' })
172 res.locals.video = video
176 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
177 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
178 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
179 if (videoChannel === null) {
181 .json({ error: 'Unknown video `video channel` on this instance.' })
187 res.locals.videoChannel = videoChannel
191 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
192 if (videoChannel === null) {
194 .json({ error: 'Unknown video `video channel` for this account.' })
200 res.locals.videoChannel = videoChannel
204 // ---------------------------------------------------------------------------
207 isVideoCategoryValid,
208 checkUserCanManageVideo,
210 isVideoLanguageValid,
211 isVideoTruncatedDescriptionValid,
212 isVideoDescriptionValid,
213 isVideoFileInfoHashValid,
216 isVideoFPSResolutionValid,
217 isScheduleVideoUpdatePrivacyValid,
219 isVideoMagnetUriValid,
222 isVideoRatingTypeValid,
223 isVideoDurationValid,
226 isVideoFileResolutionValid,
227 isVideoFileSizeValid,
230 isVideoChannelOfAccountExist,