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