]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/videos.ts
Add concept of video state, and add ability to wait transcoding before
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
CommitLineData
8d468a16 1import { Response } from 'express'
fdbda9e3 2import 'express-validator'
79d5caf9 3import { values } from 'lodash'
1840c2f7 4import 'multer'
79d5caf9 5import * as validator from 'validator'
6200d8d9 6import { UserRight, VideoRateType } from '../../../shared'
3fd3ab2d
C
7import {
8 CONSTRAINTS_FIELDS,
9 VIDEO_CATEGORIES,
28be8916
C
10 VIDEO_LICENCES,
11 VIDEO_MIMETYPE_EXT,
3fd3ab2d 12 VIDEO_PRIVACIES,
2186386c
C
13 VIDEO_RATE_TYPES,
14 VIDEO_STATES
3fd3ab2d
C
15} from '../../initializers'
16import { VideoModel } from '../../models/video/video'
ac81d1a0 17import { exists, isArray, isFileValid } from './misc'
0f320037 18import { VideoChannelModel } from '../../models/video/video-channel'
6200d8d9 19import { UserModel } from '../../models/account/user'
65fcc311
C
20
21const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
22const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
0b697522 23
9d3ef9fe 24function isVideoCategoryValid (value: any) {
2186386c
C
25 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
26}
27
28function isVideoStateValid (value: any) {
29 return exists(value) && VIDEO_STATES[ value ] !== undefined
6e07c3de
C
30}
31
9d3ef9fe 32function isVideoLicenceValid (value: any) {
2186386c 33 return value === null || VIDEO_LICENCES[ value ] !== undefined
6f0c39e2
C
34}
35
9d3ef9fe
C
36function isVideoLanguageValid (value: any) {
37 return value === null ||
38 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
3092476e
C
39}
40
8e10cf1a
C
41function isVideoDurationValid (value: string) {
42 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
43}
44
9567011b
C
45function isVideoTruncatedDescriptionValid (value: string) {
46 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
47}
48
69818c93 49function isVideoDescriptionValid (value: string) {
f595d394 50 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
be587647
C
51}
52
2422c46b
C
53function isVideoSupportValid (value: string) {
54 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
55}
56
69818c93
C
57function isVideoNameValid (value: string) {
58 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
be587647
C
59}
60
0d0e8dd0
C
61function isVideoTagValid (tag: string) {
62 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
63}
64
69818c93 65function isVideoTagsValid (tags: string[]) {
2efd32f6
C
66 return tags === null || (
67 isArray(tags) &&
68 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
69 tags.every(tag => isVideoTagValid(tag))
70 )
be587647
C
71}
72
69818c93
C
73function isVideoAbuseReasonValid (value: string) {
74 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
e4c55619
C
75}
76
69818c93
C
77function isVideoViewsValid (value: string) {
78 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
9e167724
C
79}
80
69818c93 81function isVideoRatingTypeValid (value: string) {
57a49263 82 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
d38b8281
C
83}
84
ac81d1a0
C
85const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
86const videoFileTypesRegex = videoFileTypes.join('|')
2186386c 87
b60e5f38 88function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
ac81d1a0
C
89 return isFileValid(files, videoFileTypesRegex, 'videofile')
90}
f6f7dfee 91
ac81d1a0 92const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
2186386c
C
93 .map(v => v.replace('.', ''))
94 .join('|')
ac81d1a0 95const videoImageTypesRegex = `image/(${videoImageTypes})`
2186386c 96
ac81d1a0
C
97function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
98 return isFileValid(files, videoImageTypesRegex, field, true)
f6f7dfee
C
99}
100
d4f1e94c 101function isVideoPrivacyValid (value: string) {
2186386c 102 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
d4f1e94c
C
103}
104
93e1258c
C
105function isVideoFileInfoHashValid (value: string) {
106 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
107}
108
d4f1e94c
C
109function isVideoFileResolutionValid (value: string) {
110 return exists(value) && validator.isInt(value + '')
111}
112
113function isVideoFileSizeValid (value: string) {
114 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
8d468a16
C
115}
116
a2431b7d 117async function isVideoExist (id: string, res: Response) {
3fd3ab2d 118 let video: VideoModel
4e50b6a1
C
119
120 if (validator.isInt(id)) {
3fd3ab2d 121 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
4e50b6a1 122 } else { // UUID
3fd3ab2d 123 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
4e50b6a1
C
124 }
125
126 if (!video) {
127 res.status(404)
2186386c
C
128 .json({ error: 'Video not found' })
129 .end()
4e50b6a1
C
130
131 return false
132 }
133
134 res.locals.video = video
135 return true
136}
137
6200d8d9
C
138async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
139 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
140 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
141 if (!videoChannel) {
142 res.status(400)
143 .json({ error: 'Unknown video video channel on this instance.' })
144 .end()
145
146 return false
147 }
148
149 res.locals.videoChannel = videoChannel
150 return true
151 }
152
153 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
0f320037
C
154 if (!videoChannel) {
155 res.status(400)
156 .json({ error: 'Unknown video video channel for this account.' })
157 .end()
158
159 return false
160 }
161
162 res.locals.videoChannel = videoChannel
163 return true
164}
165
55fa55a9
C
166// ---------------------------------------------------------------------------
167
65fcc311 168export {
65fcc311
C
169 isVideoCategoryValid,
170 isVideoLicenceValid,
171 isVideoLanguageValid,
9567011b 172 isVideoTruncatedDescriptionValid,
65fcc311 173 isVideoDescriptionValid,
93e1258c 174 isVideoFileInfoHashValid,
65fcc311
C
175 isVideoNameValid,
176 isVideoTagsValid,
65fcc311 177 isVideoAbuseReasonValid,
65fcc311 178 isVideoFile,
2186386c 179 isVideoStateValid,
65fcc311 180 isVideoViewsValid,
65fcc311 181 isVideoRatingTypeValid,
8e10cf1a 182 isVideoDurationValid,
0d0e8dd0 183 isVideoTagValid,
8d468a16 184 isVideoPrivacyValid,
d4f1e94c
C
185 isVideoFileResolutionValid,
186 isVideoFileSizeValid,
ac81d1a0 187 isVideoExist,
2422c46b 188 isVideoImage,
0f320037 189 isVideoChannelOfAccountExist,
2422c46b 190 isVideoSupportValid
65fcc311 191}