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'
16 } from '../../initializers'
17 import { VideoModel } from '../../models/video/video'
18 import { exists, isArray, isFileValid } from './misc'
19 import { VideoChannelModel } from '../../models/video/video-channel'
20 import { UserModel } from '../../models/account/user'
21 import * as magnetUtil from 'magnet-uri'
22 import { VideoAbuseModel } from '../../models/video/video-abuse'
24 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
25 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
27 function isVideoCategoryValid (value: any) {
28 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
31 function isVideoStateValid (value: any) {
32 return exists(value) && VIDEO_STATES[ value ] !== undefined
35 function isVideoLicenceValid (value: any) {
36 return value === null || VIDEO_LICENCES[ value ] !== undefined
39 function isVideoLanguageValid (value: any) {
40 return value === null ||
41 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
44 function isVideoDurationValid (value: string) {
45 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
48 function isVideoTruncatedDescriptionValid (value: string) {
49 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
52 function isVideoDescriptionValid (value: string) {
53 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
56 function isVideoSupportValid (value: string) {
57 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
60 function isVideoNameValid (value: string) {
61 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
64 function isVideoTagValid (tag: string) {
65 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
68 function isVideoTagsValid (tags: string[]) {
69 return tags === null || (
71 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
72 tags.every(tag => isVideoTagValid(tag))
76 function isVideoViewsValid (value: string) {
77 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
80 function isVideoRatingTypeValid (value: string) {
81 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
84 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
85 const videoFileTypesRegex = videoFileTypes.join('|')
87 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
88 return isFileValid(files, videoFileTypesRegex, 'videofile', null)
91 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
92 .map(v => v.replace('.', ''))
94 const videoImageTypesRegex = `image/(${videoImageTypes})`
96 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
97 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
100 function isVideoPrivacyValid (value: number) {
101 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
104 function isScheduleVideoUpdatePrivacyValid (value: number) {
105 return validator.isInt(value + '') &&
107 value === VideoPrivacy.UNLISTED ||
108 value === VideoPrivacy.PUBLIC
112 function isVideoFileInfoHashValid (value: string | null | undefined) {
113 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
116 function isVideoFileResolutionValid (value: string) {
117 return exists(value) && validator.isInt(value + '')
120 function isVideoFPSResolutionValid (value: string) {
121 return value === null || validator.isInt(value + '')
124 function isVideoFileSizeValid (value: string) {
125 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
128 function isVideoMagnetUriValid (value: string) {
129 if (!exists(value)) return false
131 const parsed = magnetUtil.decode(value)
132 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
135 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
136 // Retrieve the user who did the request
137 if (video.isOwned() === false) {
139 .json({ error: 'Cannot manage a video of another server.' })
144 // Check if the user can delete the video
145 // The user can delete it if he has the right
146 // Or if s/he is the video's account
147 const account = video.VideoChannel.Account
148 if (user.hasRight(right) === false && account.userId !== user.id) {
150 .json({ error: 'Cannot manage a video of another user.' })
158 async function isVideoExist (id: string, res: Response) {
159 let video: VideoModel | null
161 if (validator.isInt(id)) {
162 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
164 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
167 if (video === null) {
169 .json({ error: 'Video not found' })
175 res.locals.video = video
179 async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
180 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
181 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
182 if (videoChannel === null) {
184 .json({ error: 'Unknown video video channel on this instance.' })
190 res.locals.videoChannel = videoChannel
194 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
195 if (videoChannel === null) {
197 .json({ error: 'Unknown video video channel for this account.' })
203 res.locals.videoChannel = videoChannel
207 // ---------------------------------------------------------------------------
210 isVideoCategoryValid,
211 checkUserCanManageVideo,
213 isVideoLanguageValid,
214 isVideoTruncatedDescriptionValid,
215 isVideoDescriptionValid,
216 isVideoFileInfoHashValid,
219 isVideoFPSResolutionValid,
220 isScheduleVideoUpdatePrivacyValid,
222 isVideoMagnetUriValid,
225 isVideoRatingTypeValid,
226 isVideoDurationValid,
229 isVideoFileResolutionValid,
230 isVideoFileSizeValid,
233 isVideoChannelOfAccountExist,